1.spring封装了redis,导入对应的客户端jar包,使用很方便
2.spring 封装成为了 RedisTemplate 对象来进行各种操作,支持所有的 redis 原生的 api
3.在https://blog.csdn.net/qq591009234/article/details/103709087已经对封装进行配置
4.下面针对Spring 集成 Redis,再次进行详细配置说明,供参考
(1)Maven配置
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.2.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.10</version>
</dependency>
(2)ApplicationContext.xml配置
<!-- 连接池配置. -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 连接池中最大连接数。高版本:maxTotal,低版本:maxActive -->
<property name="maxTotal" value="8" />
<!-- 连接池中最大空闲的连接数. -->
<property name="maxIdle" value="4" />
<!-- 连接池中最少空闲的连接数. -->
<property name="minIdle" value="1" />
<!-- 当连接池资源耗尽时,调用者最大阻塞的时间,超时将跑出异常。
单位,毫秒数;默认为-1.表示永不超时。高版本:maxWaitMillis,低版本:maxWait -->
<property name="maxWaitMillis" value="5000" />
<!-- 连接空闲的最小时间,达到此值后空闲连接将可能会被移除。负值(-1)表示不移除. -->
<property name="minEvictableIdleTimeMillis" value="300000" />
<!-- 对于“空闲链接”检测线程而言,每次检测的链接资源的个数。默认为3 -->
<property name="numTestsPerEvictionRun" value="3" />
<!-- “空闲链接”检测线程,检测的周期,毫秒数。如果为负值,表示不运行“检测线程”。默认为-1. -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- testOnBorrow:向调用者输出“链接”资源时,是否检测是有有效,如果无效则从连接池中移除,并尝试获取继续获取。默认为false。建议保持默认值. -->
<!-- testOnReturn:向连接池“归还”链接时,是否检测“链接”对象的有效性。默认为false。建议保持默认值.-->
<!-- testWhileIdle:向调用者输出“链接”对象时,是否检测它的空闲超时;默认为false。如果“链接”空闲超时,将会被移除。建议保持默认值. -->
<!-- whenExhaustedAction:当“连接池”中active数量达到阀值时,即“链接”资源耗尽时,连接池需要采取的手段, 默认为1(0:抛出异常。1:阻塞,直到有可用链接资源。2:强制创建新的链接资源) -->
</bean>
<!-- Spring提供的Redis连接工厂 -->
<!--redis使用2.8.2本地-->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
destroy-method="destroy">
<!-- 连接池配置. -->
<property name="poolConfig" ref="jedisPoolConfig" />
<!-- Redis服务主机. -->
<property name="hostName" value="localhost" />
<!-- Redis服务端口号. -->
<property name="port" value="6379" />
<!-- Redis服务连接密码. -->
<property name="password" value="" />
<!-- 连超时设置. -->
<property name="timeout" value="15000" />
<!-- 是否使用连接池. -->
<property name="usePool" value="true" />
</bean>
<!-- Spring提供的访问Redis类. -->
<bean id="jedisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" />
</property>
</bean>
5.配置完后,写一个测试类进行验证:
(1)测试类
@Test
public void test(){
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
// 获取Spring提供的RedisTemplate类此类封装了Jedis,简化操作
RedisTemplate<String, List<String>> redisTemplate = applicationContext.getBean("jedisTemplate",RedisTemplate.class);
// Spring 提供的各种Redis结构的key-value操作类
//String:字符串
ValueOperations<String, List<String>> value = redisTemplate.opsForValue();
//hash:哈希键值对
HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
//list:列表
ListOperations<String, List<String>> list = redisTemplate.opsForList();
HyperLogLogOperations<String, List<String>> hyperLogLog = redisTemplate.opsForHyperLogLog();
//set:无序集合
SetOperations<String, List<String>> set = redisTemplate.opsForSet();
//zset:有序集合
ZSetOperations<String, List<String>> zset = redisTemplate.opsForZSet();
List<String> listValue = new ArrayList<String>();
listValue.add("abc");
listValue.add("def");
value.set("list", listValue);
System.out.println("输出key的值:" + value.get("list"));
// 关闭Spring容器释放资源
applicationContext.close();
}
(2)通过本地redis的客户端:
<1>redis-cli可以查看到:
redis 127.0.0.1:6379> get list
"[\"java.util.ArrayList\",[\"abc\",\"def\"]]"
<2>通过main方法查看: