1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
| @Configuration @EnableCaching public class RedisConfig { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(factory); Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.activateDefaultTyping(LazyPropertyAccessor.Impl.PROPERTY, ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(jackson2JsonRedisSerializer); template.setHashKeySerializer(new StringRedisSerializer()); template.setHashValueSerializer(jackson2JsonRedisSerializer); template.afterPropertiesSet(); return template; } @Bean public CacheManager cacheManager(RedisConnectionFactory factory) { RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig() .entryTtl(Duration.ofMinutes(30)) .serializeKeysWith(RedisSerializationContext.SerializationPair .fromSerializer(new StringRedisSerializer())) .serializeValuesWith(RedisSerializationContext.SerializationPair .fromSerializer(new Jackson2JsonRedisSerializer<>(Object.class))) .disableCachingNullValues(); return RedisCacheManager.builder(factory) .cacheDefaults(config) .build(); } }
@Service @Slf4j public class RedisService { @Autowired private RedisTemplate<String, Object> redisTemplate; public void setString(String key, Object value, long timeout, TimeUnit unit) { redisTemplate.opsForValue().set(key, value, timeout, unit); } public Object getString(String key) { return redisTemplate.opsForValue().get(key); } public Boolean setIfAbsent(String key, Object value, long timeout, TimeUnit unit) { return redisTemplate.opsForValue().setIfAbsent(key, value, timeout, unit); } public void setHash(String key, String hashKey, Object value) { redisTemplate.opsForHash().put(key, hashKey, value); } public Object getHash(String key, String hashKey) { return redisTemplate.opsForHash().get(key, hashKey); } public Map<Object, Object> getAllHash(String key) { return redisTemplate.opsForHash().entries(key); } public Long leftPush(String key, Object value) { return redisTemplate.opsForList().leftPush(key, value); } public Object rightPop(String key) { return redisTemplate.opsForList().rightPop(key); } public List<Object> getList(String key, long start, long end) { return redisTemplate.opsForList().range(key, start, end); } public Long addSet(String key, Object... values) { return redisTemplate.opsForSet().add(key, values); } public Set<Object> getSet(String key) { return redisTemplate.opsForSet().members(key); } public Boolean isMemberSet(String key, Object value) { return redisTemplate.opsForSet().isMember(key, value); } public Boolean addZSet(String key, Object value, double score) { return redisTemplate.opsForZSet().add(key, value, score); } public Set<Object> rangeZSet(String key, long start, long end) { return redisTemplate.opsForZSet().range(key, start, end); } public Set<ZSetOperations.TypedTuple<Object>> rangeWithScores(String key, long start, long end) { return redisTemplate.opsForZSet().rangeWithScores(key, start, end); } public Boolean expire(String key, long timeout, TimeUnit unit) { return redisTemplate.expire(key, timeout, unit); } public Long getExpire(String key) { return redisTemplate.getExpire(key); } public Boolean delete(String key) { return redisTemplate.delete(key); } public Long delete(Collection<String> keys) { return redisTemplate.delete(keys); } public Boolean hasKey(String key) { return redisTemplate.hasKey(key); } }
|