Redis哈希
哈希结构如同Java中的Map一样,一个对象里面有许多键值对,特别适合存储对象
Redis hash结构命令
命令 | 说明 | 备注 |
---|---|---|
HDEL key field1 [field2] | 删除hash结构中的某个(些)字段 | 可以进行多个字段删除 |
HEXISTS key field | 判断hash结构中是否存在field字段 | 存在返回1,否则返回0 |
HGETALL key | 获取所有hash结构中的键值 | 返回键和值 |
HGET key field | 获取存储在哈希表中指定字段的值 | |
HINCRBY key field increment | 指定给hash结构中的某一字段加上一个整数 | 要求该字段也是整数字符串 |
HINCRBYFLOAT key field increment | 指定给hash结构中的某一字段加上一个浮点数 | 要求该字段是数字型字符串 |
HKEYS key | 返回hash中所有的键 | |
HLEN key | 返回hash中键值对的数量 | |
HMGET key field1 [field2] | 返回hash中指定键的值,可以是多个 | 依次返回值 |
HMSET key field1 value1 [field2 value2 ] | 同时将多个 field-value (域-值)对设置到哈希表 key 中 | |
HSET key field value | 将哈希表 key 中的字段 field 的值设为 value | |
HSETNX key field value | 只有在字段 field 不存在时,设置哈希表字段的值 | |
HVALS key | 获取哈希表中所有值 |
如下的例子,Role有三个字段:
- id
- roleName
- note
可能出现的问题【redis】WRONGTYPE Operation against a key holding
使用Spring去操作Redis的hash结构
首先先修改RedisTemplate
的配置,如下,修改默认序列化器为字符串序列化:
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<property name="defaultSerializer" ref="stringRedisSerializer" />
<property name="keySerializer" ref="stringRedisSerializer" />
<property name="valueSerializer" ref="stringRedisSerializer" />
</bean>
如果想为hash结构指定序列化器,可以使用RedisTemplate
提供的属性:
- hashKeySerializer
- hashValueSerializer
通过如下的例子来说明:
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
RedisTemplate redisTemplate = applicationContext.getBean(RedisTemplate.class);
String key = "hash";
Map<String, String> map = new HashMap<String, String>();
map.put("f1", "val1");
map.put("f2", "val2");
//相当于hmset命令
redisTemplate.opsForHash().putAll(key, map);
//相当与hset命令
redisTemplate.opsForHash().put(key, "f3", "6");
printValueForhash(redisTemplate, key, "f3"); //6
//相当于hexist key field命令
boolean exists = redisTemplate.opsForHash().hasKey(key, "f3");
System.out.println(exists); //true
//相当于hgetall命令
Map keyValMap = redisTemplate.opsForHash().entries(key);
System.out.println(keyValMap); //{f1=val1, f3=6, f2=val2}
//相当于hincrby命令
redisTemplate.opsForHash().increment(key, "f3", 2);
printValueForhash(redisTemplate, key, "f3"); //8
//相当于hincrbyfloat命令
redisTemplate.opsForHash().increment(key, "f3", 0.88);
printValueForhash(redisTemplate, key, "f3"); //8.88
//相当于hvals命令
List valueList = redisTemplate.opsForHash().values(key);
System.out.println(valueList); //[val1, val2, 8.88]
//相当于hkeys命令
Set keyList = redisTemplate.opsForHash().keys(key);
System.out.println(keyList); //[f1, f2, f3]
//相当于hmget命令
List<String> fieldList = new ArrayList<String>();
fieldList.add("f1");
fieldList.add("f2");
List valueList2 = redisTemplate.opsForHash().multiGet(key, fieldList);
System.out.println(valueList2); //[val1, val2]
//相当于hsetnx命令
boolean success = redisTemplate.opsForHash().putIfAbsent(key, "f4", "value4");
System.out.println(success); //true
//相当于hdel命令
Long result = redisTemplate.opsForHash().delete(key, "f1", "f2");
System.out.println(result); //2
}
private static void printValueForhash(RedisTemplate redisTemplate, String key, String field){
//相当于hget命令
Object value = redisTemplate.opsForHash().get(key, field);
System.out.println(value);
}