已亲测:https://www.cnblogs.com/cfas/p/9351597.html
用到的jar
spring 3.1.1
aopalliance-1.0.jar
commons-pool2-2.3.jar
jedis-2.7.2.jar
spring-data-redis-1.6.6.RELEASE.jar
jedis和commons-pool2有版本依赖关系,所以要保证和上面一致
spring-data-redis版本 和 spring框架的版本也有依赖关系 所以要保证和上面一致
applicationContext-redis.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <!-- 载入redis配置参数 --> <!-- <context:property-placeholder location="classpath:redis.properties" ignore-unresolvable="true"/> --> <!-- redis config start --> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxIdle" value="5"></property> <property name="maxTotal" value="10" /> <property name="maxWaitMillis" value="2000" /> <property name="testOnBorrow" value="true" /> </bean> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method="destroy"> <property name="poolConfig" ref="jedisPoolConfig"></property> <property name="hostName" value="192.168.2.110"></property> <property name="port" value="6379"></property> <property name="password" value="jinku_redis"></property> <property name="timeout" value="3000"></property> </bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"> <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.StringRedisSerializer"> </bean> </property> </bean> </beans>
RedisUtils.java
package com.app.util; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.SetOperations; import org.springframework.stereotype.Component; import org.springframework.util.CollectionUtils; @Component public class RedisUtils { private Logger log = LoggerFactory.getLogger(this.getClass()); @Autowired private RedisTemplate<String, Object> redisTemplate; /** * 删除缓存 * * @param key 可以传一个值 或多个 */ @SuppressWarnings("unchecked") public void del(String... key) { if (key != null && key.length > 0) { if (key.length == 1) { redisTemplate.delete(key[0]); } else { redisTemplate.delete(CollectionUtils.arrayToList(key)); } } } /** * @Title: getSetRandomElement * @Description: TODO(set集合,随机返回指定数量的元素,返回元素可重复) * @param key * @param i * @return * @return List<Object> * @date 2020年3月23日 下午5:53:46 */ public List<Object> getSetRandomElement(String key,int i){ SetOperations<String, Object> set = redisTemplate.opsForSet(); return set.randomMembers(key, i); } /** * @Title: addSet * @Description: TODO(无序集合set添加) * @param key * @param value * @return void * @date 2020年3月23日 下午5:50:03 */ public void addSet(String key, Object value) { SetOperations<String, Object> set = redisTemplate.opsForSet(); set.add(key, value); } }