一、Redis哈希简介
Redis中的哈希结果就如同Java中的Map一样,一个对象里面有许多键值对,它是特别适合存储对象的,如果内存足够大,那么一个Redis的hash结构可以存储40多亿个键值对。一般而言,不会使用那么大的一个键值对,所以我们只需要认为Redis可以存储很多的键值对。在Redis中,hash是一个String类型的field和value的映射表,因此我们存储的数据实际在Redis上知识一个个字符串而已。
二、Hash的基本命令
【1】hset key field value,功能:在hash结构中设置键值对,单个设置。
【2】hmset key field1 value1 [field2 value2 …],功能:hash结构中设置多个键值对。
【3】hdel key field1 value1 [field2 value2 …],功能:删除hash结构中某个或者某些字段,机可以进行多个字段的删除。
【4】hexists key field,功能:盘带你hash结构中是否存在field字段,存在返回1,不存在返回0。
【5】hgetall key ,功能:获取所有hash结构中的值,返回键和值。
【6】hincrby key field increment,功能:指定给hash结构中的某一个字段加上一个整数,要求是该字段也是整数字符串。
【7】hincrbyfloat key field increment,功能:指定给hash结构中的某一个字段加上一个浮点数,要求是该字段是整数或者浮点数字符串。
【8】hkeys key,功能:返回hash中所有的键。
【9】hlen key,功能:返回hash键值对的数量。
【10】hsetnx key field value,功能:当hash结构中不存在对应的键,才设置值。
【11】hvals key,功能:获取hash结构中所有的值。
【12】hmget field1 value1 [field2 value2 …],功能:返回hash结果中指定键的值,可以是多个。
三、Spring中使用Redis操作哈希类型
步骤一:创建Maven项目,在pom.xml文件中导入一下三个依赖。
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>1.7.2</version>
</dependency>
<!-- 导入Spring中的redis依赖 -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
<!-- 导入jedis依赖 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.1</version>
</dependency>
</dependencies>
步骤二:在applicationContext.xml文件中配置Redis。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- Jedis 的连接池配置 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 最大空闲连接数量-->
<property name="maxIdle" value="50"/>
<!-- 最大连接数量-->
<property name="maxTotal" value="100"/>
<!-- 最大等待时间-->
<property name="maxWaitMillis" value="20000"/>
</bean>
<bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
<!-- Redis服务中心-->
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="localhost"/>
<property name="port" value="6379"/>
<property name="poolConfig" ref="poolConfig"/>
</bean>
<!-- Redis模板类-->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="defaultSerializer" ref="stringRedisSerializer"/>
<property name="valueSerializer" ref="stringRedisSerializer"/>
</bean>
</beans>
步骤三:创建一个RedisHashDemo类,使用Spring测试Redis哈希操作,如下所示:
package demo;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.*;
/**
* @author czd
*/
public class RedisHashDemo {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
RedisTemplate redisTemplate = applicationContext.getBean(RedisTemplate.class);
//设置多个键值对,相当于hmset key field1 value1 [field2 value2 .....]
String key = "hashKey";
Map<String, String> map = new HashMap<>();
map.put("field1", "value1");
map.put("field2", "value2");
redisTemplate.opsForHash().putAll(key, map);
//设置一个键值对,相当于hset key field value
redisTemplate.opsForHash().put(key, "field3", "value3");
//判断某个键是否存在,相当于hexists key field
Boolean exist = redisTemplate.opsForHash().hasKey(key, "field3");
System.out.println("field3是否存在:" + exist);
//获取hash结构中的所有键和值,相当于hgetall key
Map keyValueMap = redisTemplate.opsForHash().entries(key);
Set<String> keySet = keyValueMap.keySet();
for (String k : keySet) {
System.out.println("key: " + k + " value: " + keyValueMap.get(k));
}
//给hash结果中的一个键指定加上一个整数值,相当于hincrby key field increment
redisTemplate.opsForHash().put(key, "field4", "5");
System.out.println("在使用increment之前field4的value:" + redisTemplate.opsForHash().get(key, "field4").toString());
redisTemplate.opsForHash().increment(key, "field4", 6);
System.out.println("在使用increment之后field4的value:" + redisTemplate.opsForHash().get(key, "field4").toString());
//给hash结果中的一个键指定加上一个浮点数值,相当于hincrbyfloat key field increment,版本问题可能会报错
// redisTemplate.opsForHash().put(key, "field5", "5.5");
// System.out.println("在使用increment之前field5的value:" + redisTemplate.opsForHash().get(key, "field5").toString());
// redisTemplate.opsForHash().increment(key, "field5", 6.5);
// System.out.println("在使用increment之后field5的value:" + redisTemplate.opsForHash().get(key, "field5").toString());
//获取hash结果中所有的值,相当于hvals key
List valueList = redisTemplate.opsForHash().values(key);
for (int index = 0; index < valueList.size(); index++) {
System.out.println("value" + index + ": " + valueList.get(index));
}
//获取hash结果中所有的键,相当于hkeys key
Set<String> keys = redisTemplate.opsForHash().keys(key);
for (String K : keys) {
System.out.println("Key: " + K);
}
//获取hash结构中指定键的值,相当于hmget field1 value1 [field2 value2 …]
List<String> keyList = new ArrayList<>();
keyList.add("field1");
keyList.add("field2");
List<String> valuesList = redisTemplate.opsForHash().multiGet(key,keyList);
for (int index = 0; index < valuesList.size(); index++) {
System.out.println("value" + index + ": " + valuesList.get(index));
}
//当hash结构中不存在对应的键,才设置值,相当于hsetnx key field value
Boolean success = redisTemplate.opsForHash().putIfAbsent(key,"field6","value6");
System.out.println("是否重新设置成功:" + success);
//删除hash结果中的指定的键,相当于hdel key field1 value1 [field2 value2 …]
Long result = redisTemplate.opsForHash().delete(key,"field","field1");
System.out.println("删除的结果:" + result);
}
}
运行效果如下所示: