<>1. pom.xml添加依赖
<!-- springboot整合redis --> <dependency> <groupId>org.springframework.boot</
groupId> <artifactId>spring-boot-starter-redis</artifactId> <version>
1.4.7.RELEASE</version> </dependency> <!--使用jedis及其连接池--> <dependency> <groupId>
redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version>
</dependency>
<>2. 集群版redis配置类
import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.
jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.
ObjectMapper; import org.springframework.beans.factory.annotation.Value; import
org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.
springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.
springframework.context.annotation.Bean; import org.springframework.context.
annotation.Configuration; import org.springframework.data.redis.connection.
RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate
; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer; import
redis.clients.jedis.HostAndPort; import redis.clients.jedis.JedisCluster; import
redis.clients.jedis.JedisPoolConfig; import java.net.UnknownHostException;
import java.util.HashSet; import java.util.Set; /** * 集群版
Redis缓存配置类,如需启用请在配置文件中新增:spring.redis.cluster.nodes={ip1}:{port1},{ip2}:{port2},...
*/ @Configuration @ConditionalOnClass({JedisCluster.class})
@ConditionalOnProperty(prefix = "spring.redis.cluster", name = "nodes",
havingValue= "true") public class ClusterRedisConfig { @Value(
"${spring.redis.cluster.nodes}") private String clusterNodes; @Value(
"${spring.redis.timeout}") private int timeout; @Value(
"${spring.redis.pool.max-idle}") private int maxIdle; @Value(
"${spring.redis.pool.max-wait}") private long maxWaitMillis; @Value(
"${spring.redis.commandTimeout}") private int commandTimeout; @Bean public
JedisCluster getJedisCluster() { String[] cNodes = clusterNodes.split(","); Set<
HostAndPort> nodes = new HashSet<>(); //分割出集群节点 for (String node : cNodes) {
String[] hp = node.split(":"); nodes.add(new HostAndPort(hp[0], Integer.parseInt
(hp[1]))); } JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(maxIdle); jedisPoolConfig.setMaxWaitMillis(
maxWaitMillis); //创建集群对象 return new JedisCluster(nodes, commandTimeout,
jedisPoolConfig); } /** * 设置数据存入redis 的序列化方式 *
</br>redisTemplate序列化默认使用的jdkSerializeable,存储二进制字节码,导致key会出现乱码,所以自定义 * 序列化类 * *
@paramredisConnectionFactory */ @Bean public RedisTemplate<Object, Object>
redisTemplate(RedisConnectionFactory redisConnectionFactory) throws
UnknownHostException { RedisTemplate<Object, Object> redisTemplate = new
RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new
Jackson2JsonRedisSerializer(Object.class); ObjectMapper objectMapper = new
ObjectMapper(); objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.
Visibility.ANY); objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.
NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); redisTemplate.
setKeySerializer(new StringRedisSerializer()); redisTemplate.afterPropertiesSet(
); return redisTemplate; } }
<>3.redis单机版配置文件
import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.
jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.
ObjectMapper; import org.springframework.beans.factory.annotation.Value; import
org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import
org.springframework.boot.autoconfigure.data.redis.RedisProperties; import org.
springframework.boot.context.properties.EnableConfigurationProperties; import
org.springframework.cache.annotation.CachingConfigurerSupport; import org.
springframework.cache.interceptor.KeyGenerator; import org.springframework.
context.annotation.Bean; import org.springframework.context.annotation.
Configuration; import org.springframework.data.redis.cache.
RedisCacheConfiguration; import org.springframework.data.redis.cache.
RedisCacheManager; import org.springframework.data.redis.connection.
RedisStandaloneConfiguration; import org.springframework.data.redis.connection.
jedis.JedisConnectionFactory; import org.springframework.data.redis.core.
RedisTemplate; import org.springframework.data.redis.serializer.
Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.
RedisSerializationContext; import org.springframework.data.redis.serializer.
StringRedisSerializer; import java.lang.reflect.Method; /** * 单机版
Redis缓存配置类,如需启用请在nacos中配置:jddods.redis.enabled=true */ @Configuration
@EnableConfigurationProperties({RedisProperties.class}) @ConditionalOnProperty(
prefix= "jddods.redis", name = "enabled", havingValue = "true") public class
RedisConfig extends CachingConfigurerSupport { @Value("${spring.redis.host}")
private String host; @Value("${spring.redis.password}") private String password;
@Value("${spring.redis.port}") private int port; @Bean public
JedisConnectionFactory jedisConnectionFactory() { RedisStandaloneConfiguration
redisStandaloneConfiguration= new RedisStandaloneConfiguration();
redisStandaloneConfiguration.setHostName(host); redisStandaloneConfiguration.
setPort(port); return new JedisConnectionFactory(redisStandaloneConfiguration);
} /** * @return 自定义策略生成的key * @description 自定义的缓存key的生成策略 若想使用这个key *
只需要讲注解上keyGenerator的值设置为keyGenerator即可</br> */ @Bean @Override public
KeyGenerator keyGenerator() { return new KeyGenerator() { @Override public
Object generate(Object target, Method method, Object... params) { StringBuilder
sb= new StringBuilder(); sb.append(target.getClass().getName()); sb.append(
method.getName()); for (Object obj : params) { sb.append(obj.toString()); }
return sb.toString(); } }; } // 缓存管理器 @Bean public RedisCacheManager
cacheManager(JedisConnectionFactory jedisConnectionFactory) { // 配置序列化(解决乱码的问题)
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new
Jackson2JsonRedisSerializer(Object.class); ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.
enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om); RedisCacheConfiguration config
= RedisCacheConfiguration.defaultCacheConfig() // 默认 30天过期 .serializeKeysWith(
RedisSerializationContext.SerializationPair.fromSerializer(stringRedisSerializer
)) .serializeValuesWith(RedisSerializationContext.SerializationPair.
fromSerializer(jackson2JsonRedisSerializer)) .disableCachingNullValues();
RedisCacheManager cacheManager = RedisCacheManager .builder(
jedisConnectionFactory) .cacheDefaults(config) .build(); return cacheManager; }
/** * RedisTemplate配置 * * @param jedisConnectionFactory * @return */ @Bean
public RedisTemplate<String, Object> redisTemplate(JedisConnectionFactory
jedisConnectionFactory) { // 设置序列化 Jackson2JsonRedisSerializer
jackson2JsonRedisSerializer= new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL,
JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.
DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); //
配置redisTemplate RedisTemplate<String, Object> redisTemplate = new RedisTemplate<
String, Object>(); redisTemplate.setConnectionFactory(jedisConnectionFactory);
StringRedisSerializer stringSerializer = new StringRedisSerializer();
redisTemplate.setKeySerializer(stringSerializer);// key序列化 redisTemplate.
setValueSerializer(jackson2JsonRedisSerializer);// value序列化 redisTemplate.
setHashKeySerializer(stringSerializer);// Hash key序列化 redisTemplate.
setHashValueSerializer(jackson2JsonRedisSerializer);// Hash value序列化
redisTemplate.afterPropertiesSet(); return redisTemplate; } }
<>redisService工具类,可以在代码中直接调用工具类的相关方法
import cn.hutool.core.util.ObjectUtil; import org.slf4j.Logger; import org.
slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.
Autowired; import org.springframework.data.redis.core.*; import org.
springframework.data.redis.support.atomic.RedisAtomicLong; import org.
springframework.stereotype.Service; import java.io.Serializable; import java.
util.*; import java.util.concurrent.TimeUnit; /** * Created by wjd on 2019/5/7.
*/ @Service public class RedisService { private final Logger logger =
LoggerFactory.getLogger(this.getClass()); @Autowired private RedisTemplate
redisTemplate; /** * 模糊查询key * * @return */ public Set<String> listKeys(final
String key) { Set<String> keys = redisTemplate.keys(key); return keys; } /** *
重命名 */ public void rename(final String oldKey, final String newKey) {
redisTemplate.rename(oldKey, newKey); } /** * 模糊获取 * * @param pattern * @return
*/ public List<Object> listPattern(final String pattern) { List<Object> result =
new ArrayList<>(); Set<Serializable> keys = redisTemplate.keys(pattern); for (
Serializable str : keys) { ValueOperations<Serializable, Object> operations =
redisTemplate.opsForValue(); Object obj = operations.get(str.toString()); if (!
ObjectUtil.isEmpty(obj)) { result.add(obj); } } return result; } /** * 写入缓存 * *
@param key * @param value * @return */ public boolean set(final String key,
Object value) { boolean result = false; try { ValueOperations<Serializable,
Object> operations = redisTemplate.opsForValue(); operations.set(key, value);
result= true; } catch (Exception e) { logger.error("set fail ,key is:" + key, e)
; } return result; } /** * 批量写入缓存 * * @param map * @return */ public boolean
multiSet(Map<String, Object> map) { boolean result = false; try {
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
operations.multiSet(map); result = true; } catch (Exception e) { logger.error(
"multiSet fail ", e); } return result; } /** * 集合出栈 * * @param key */ public
Object leftPop(String key) { ListOperations list = redisTemplate.opsForList();
return list.leftPop(key); } public Object llen(final String key) { final
ListOperations list = this.redisTemplate.opsForList(); return list.size((Object)
key); } /** * 写入缓存设置时效时间 * * @param key * @param value * @return */ public
boolean set(final String key, Object value, Long expireTime) { boolean result =
false; try { ValueOperations<Serializable, Object> operations = redisTemplate.
opsForValue(); operations.set(key, value); redisTemplate.expire(key, expireTime,
TimeUnit.SECONDS); result = true; } catch (Exception e) { logger.error("set
fail ", e); } return result; } /** * 写入缓存设置时效时间 * * @param key * @param value *
@return */ public boolean setnx(final String key, Object value, Long expireTime)
{ boolean res = false; try { ValueOperations<Serializable, Object> operations =
redisTemplate.opsForValue(); res = operations.setIfAbsent(key, value); if (res)
{ redisTemplate.expire(key, expireTime, TimeUnit.SECONDS); } } catch (Exception
e) { logger.error("setnx fail ", e); } return res; } /** * 缓存设置时效时间 * * @param
key * @param expireTime * @return */ public void expire(final String key, Long
expireTime) { redisTemplate.expire(key, expireTime, TimeUnit.SECONDS); } /** *
自增操作 * * @param key * @return */ public long incr(final String key) {
RedisAtomicLong entityIdCounter = new RedisAtomicLong(key, redisTemplate.
getConnectionFactory()); return entityIdCounter.getAndIncrement(); } /** * 批量删除
* * @param keys */ public void removeKeys(final List<String> keys) { if (keys.
size() > 0) { redisTemplate.delete(keys); } } /** * 批量删除key * * @param pattern
*/ public void removePattern(final String pattern) { Set<Serializable> keys =
redisTemplate.keys(pattern); if (keys.size() > 0) { redisTemplate.delete(keys);
} } /** * 删除对应的value * * @param key */ public void remove(final String key) { if
(exists(key)) { redisTemplate.delete(key); } } /** * 判断缓存中是否有对应的value * *
@param key * @return */ public boolean exists(final String key) { return
redisTemplate.hasKey(key); } /** * 判断缓存中是否有对应的value(模糊匹配) * * @param pattern *
@return */ public boolean existsPattern(final String pattern) { if (
redisTemplate.keys(pattern).size() > 0) { return true; } else { return false; }
} /** * 读取缓存 * * @param key * @return */ public Object get(final String key) {
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
return operations.get(key); } /** * 哈希 添加 */ public void hmSet(String key,
Object hashKey, Object value) { HashOperations<String, Object, Object> hash =
redisTemplate.opsForHash(); hash.put(key, hashKey, value); } /** * 哈希 添加 */
public Boolean hmSet(String key, Object hashKey, Object value, Long expireTime,
TimeUnit timeUnit) { HashOperations<String, Object, Object> hash = redisTemplate
.opsForHash(); hash.put(key, hashKey, value); return redisTemplate.expire(key,
expireTime, timeUnit); } /** * 哈希获取数据 * * @param key * @param hashKey * @return
*/ public Object hmGet(String key, Object hashKey) { HashOperations<String,
Object, Object> hash = redisTemplate.opsForHash(); return hash.get(key, hashKey)
; } /** * 哈希获取所有数据 * * @param key * @return */ public Object hmGetValues(String
key) { HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
return hash.values(key); } /** * 哈希获取所有键值 * * @param key * @return */ public
Object hmGetKeys(String key) { HashOperations<String, Object, Object> hash =
redisTemplate.opsForHash(); return hash.keys(key); } /** * 哈希获取所有键值对 * * @param
key * @return */ public Object hmGetMap(String key) { HashOperations<String,
Object, Object> hash = redisTemplate.opsForHash(); return hash.entries(key); }
/** * 哈希 删除域 * * @param key * @param hashKey */ public Long hdel(String key,
Object hashKey) { HashOperations<String, Object, Object> hash = redisTemplate.
opsForHash(); return hash.delete(key, hashKey); } /** * 列表添加 * * @param k *
@param v */ public void rPush(String k, Object v) { ListOperations<String,
Object> list = redisTemplate.opsForList(); list.rightPush(k, v); } /** * 列表删除 *
* @param k * @param v */ public void listRemove(String k, Object v) {
ListOperations<String, Object> list = redisTemplate.opsForList(); list.remove(k,
1, v); } public void rPushAll(String k, Collection var2) { ListOperations<String
, Object> list = redisTemplate.opsForList(); list.rightPushAll(k, var2); } /**
* 列表获取 * * @param k * @param begin * @param end * @return */ public Object
lRange(String k, long begin, long end) { ListOperations<String, Object> list =
redisTemplate.opsForList(); return list.range(k, begin, end); } /** * 集合添加 * *
@param key * @param value */ public void add(String key, Object value) {
SetOperations<String, Object> set = redisTemplate.opsForSet(); set.add(key,
value); } /** * 判断元素是否在集合中 * * @param key * @param value */ public Boolean
isMember(String key, Object value) { SetOperations<String, Object> set =
redisTemplate.opsForSet(); return set.isMember(key, value); } /** * 集合获取 * *
@param key * @return */ public Set<Object> setMembers(String key) {
SetOperations<String, Object> set = redisTemplate.opsForSet(); return set.
members(key); } /** * 有序集合添加 * * @param key * @param value * @param scoure */
public void zAdd(String key, Object value, double scoure) { ZSetOperations<
String, Object> zset = redisTemplate.opsForZSet(); zset.add(key, value, scoure);
} /** * 有序集合获取 * * @param key * @param scoure * @param scoure1 * @return */
public Set<Object> rangeByScore(String key, double scoure, double scoure1) {
ZSetOperations<String, Object> zset = redisTemplate.opsForZSet(); return zset.
rangeByScore(key, scoure, scoure1); } /** * 有序集合根据区间删除 * * @param key * @param
scoure * @param scoure1 * @return */ public void removeRangeByScore(String key,
double scoure, double scoure1) { ZSetOperations<String, Object> zset =
redisTemplate.opsForZSet(); zset.removeRangeByScore(key, scoure, scoure1); }
/** * 列表添加 * * @param k * @param v */ public void lPush(String k, Object v) {
ListOperations<String, Object> list = redisTemplate.opsForList(); list.rightPush
(k, v); } /** * 获取当前key的超时时间 * * @param key * @return */ public Long
getExpireTime(final String key) { return redisTemplate.opsForValue().
getOperations().getExpire(key, TimeUnit.SECONDS); } public Long extendExpireTime
(final String key, Long extendTime) { Long curTime = redisTemplate.opsForValue()
.getOperations().getExpire(key, TimeUnit.SECONDS); long total = curTime.
longValue() + extendTime; redisTemplate.expire(key, total, TimeUnit.SECONDS);
return total; } public Set getKeys(String k) { return redisTemplate.keys(k); } }