标签:创建 tde tcl bool jackson http href lte exce
@Configuration @EnableCaching public class RedisCacheConfig extends CachingConfigurerSupport { @Value("${redis.cache.expiration}") private Long expiration; /** * * 管理缓存 */ @Bean public CacheManager cacheManager(RedisTemplate<Object, Object> redisTemplate) { RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate); cacheManager.setDefaultExpiration(expiration);// 设置缓存默认过期时间(全局的)秒 return cacheManager; } /** * 自定义生成的redis key * 在使用@Cacheable时,如果不指定key,则使用找个默认的key生成器生成的key * @return */ @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 RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) { StringRedisTemplate template = new StringRedisTemplate(factory); 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); template.setValueSerializer(jackson2JsonRedisSerializer); template.afterPropertiesSet(); return template; } }
1.手动方式:
Jedis工具类
@Component public class RedisUtil { private Logger logger = LoggerFactory.getLogger(getClass()); @Autowired private StringRedisTemplate stringRedisTemplate; @Autowired private RedisTemplate<Object,Object> redisTemplate; public void set(String key, String value){ ValueOperations<String, String> ops = this.stringRedisTemplate.opsForValue(); boolean isExsit = this.stringRedisTemplate.hasKey(key); if (isExsit) { logger.warn("key值已存在"); }else{ ops.set(key, value); } } public String get(String key){ return this.stringRedisTemplate.opsForValue().get(key); } /** * 获取hashKey对应的所有键值 * @param key 键 * @return 对应的多个键值 */ public Map<Object, Object> hmget(String key){ return this.redisTemplate.opsForHash().entries(key); } /** * HashSet 并设置时间 * @param key 键 * @param map 对应多个键值 * @param time 时间(秒) * @return true成功 false失败 */ public boolean hmset(String key, Map<String,Object> map, long time){ try { this.redisTemplate.opsForHash().putAll(key, map); if(time>0){ this.redisTemplate.expire(key,time,TimeUnit.SECONDS); } return true; } catch (Exception e) { e.printStackTrace(); return false; } } public Boolean exists(String key) { Boolean result = Boolean.FALSE; result = this.stringRedisTemplate.hasKey(key); return result; } public void del(String key){ this.stringRedisTemplate.delete(key); } public void delhm(String key){ this.redisTemplate.delete(key); } public boolean lock(String key, int seconds) { boolean result = redisTemplate.getConnectionFactory().getConnection().setNX(key.getBytes(), "LOCKED".getBytes()); redisTemplate.expire(key, seconds, TimeUnit.SECONDS); return result; } }
2.自动方式:
添加@Cacheable注解实现缓存添加
添加@CacheEvict注解实现缓存删除
具体实现参考上一篇文章:八、springboot整合redis
标签:创建 tde tcl bool jackson http href lte exce
原文地址:https://www.cnblogs.com/soul-wonder/p/9003842.html