一、依赖的jar包
<-- spring !--> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>2.1.5.RELEASE</version> </dependency> <-- spring-boot !--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <version>2.1.1.RELEASE</version> </dependency> <-- Jedis !--> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>3.2.0</version> </dependency>
二、redis配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"> <!-- redis pool --> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="${redis.maxTotal}"/> <property name="maxIdle" value="${redis.maxIdle}"/> <property name="minIdle" value="${redis.minIdle}"/> <property name="maxWaitMillis" value="${redis.maxWaitMillis}"/> </bean> <!-- redis pool config --> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="hostName" value="${redis.host}"></property> <property name="password" value="${redis.password}"></property> <property name="port" value="${redis.port}"></property> <property name="poolConfig" ref="poolConfig"></property> </bean> <!--redis redisTemplate --> <bean id="redisTemplate" 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.JdkSerializationRedisSerializer"/> </property> <property name="hashKeySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/> </property> <property name="hashValueSerializer"> <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/> </property> </bean> <bean id="redisCacheManager" class="org.springframework.data.redis.cache.RedisCacheManager"> <constructor-arg index="0"> <bean class="org.springframework.data.redis.cache.DefaultRedisCacheWriter"> <constructor-arg index="0" ref="jedisConnectionFactory"/> </bean> </constructor-arg> <constructor-arg index="1"> <bean class="org.springframework.data.redis.cache.RedisCacheConfiguration" factory-method="defaultCacheConfig"/> </constructor-arg> <constructor-arg index="2"> <list> <value>myCache1</value> <value>myCache2</value> </list> </constructor-arg> </bean> <bean id="suninfoCacheManager" class="com.wutongyu.util.redis.MyCacheManager"> <constructor-arg index="0" ref="redisCacheManager"/> <constructor-arg index="1" ref="redisTemplate"/> </bean> <cache:annotation-driven cache-manager="redisCacheManager"/> </beans>
三、redis配置文件
redis.port=1234 redis.password=ZAQxcv #最小空闲连接数 redis.minIdle=5 #最大空闲连接数 redis.maxIdle=20 #最大连接数 redis.maxTotal=500 #最大等待时间(毫秒) redis.maxWaitMillis=3000
四、Jedis客户端
package com.wutongyu.util.redis; import com.wutongyu.util.lang.UUIDUtil; import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.TimeUnit; public class MyCacheManager { private RedisCacheManager redisCacheManager; private RedisTemplate redisTemplate; public MyCacheManager(RedisCacheManager redisCacheManager, RedisTemplate redisTemplate) { this.redisCacheManager = redisCacheManager; this.redisTemplate = redisTemplate; } /** * 获取所有key * * @return */ public List<String> getAllKeys() { Set keys = redisTemplate.keys("*"); return new ArrayList<>(keys); } /** * 放入缓存数据 * * @param key * @param value */ public void put(String key, Object value) { redisTemplate.opsForValue().set(key, value); } /** * 放入缓存数据 * * @param key * @param value * @param timeout 超时时间 */ public void put(String key, Object value, long timeout) { redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS); } /** * 放入Map到缓存 * * @param key * @param map */ public void putForHash(String key, Map map) { redisTemplate.opsForHash().putAll(key, map); } /** * 放入k-v(特定Map) * * @param key * @param hashKey * @param value */ public void putForHash(String key, String hashKey, Object value) { redisTemplate.opsForHash().put(key, hashKey, value); } /** * 放入缓存数据 * * @param key * @param value * @param timeout * @return */ public Boolean putIfAbsent(String key, Object value, long timeout) { return redisTemplate.opsForValue().setIfAbsent(key, value, timeout, TimeUnit.SECONDS); } /** * 放入缓存数据 * * @param key * @param value */ public Boolean putIfAbsent(String key, Object value) { return redisTemplate.opsForValue().setIfAbsent(key, value); } /** * 获取锁 * * @param key * @param timeout * @return */ public Boolean tryLock(String key, long timeout) { return redisTemplate.opsForValue().setIfAbsent(key, UUIDUtil.getUUID(), timeout, TimeUnit.SECONDS); } /** * 获取缓存数据 * * @param key * @return */ public Object get(String key) { return redisTemplate.opsForValue().get(key); } /** * 删除缓存数据 * * @param key */ public void remove(String key) { redisTemplate.delete(key); } /** * 从阻塞队列获取值 * * @param key 阻塞队列的key * @param timeout 超时时间(秒) * @return 超时时间内队列有值,返回有效值,没有值一直阻塞,超时后返回null */ public Object bLPop(String key, int timeout) { RedisConnection connection = null; try { connection = redisTemplate.getConnectionFactory().getConnection(); List<byte[]> bytes = connection.bLPop(timeout, key.getBytes()); if (bytes != null) { return new JdkSerializationRedisSerializer().deserialize(bytes.get(1)); } } catch (Exception e) { e.printStackTrace(); } finally { if (connection != null) { connection.close(); } } return null; } }