package com.shanhy.example.redis;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.PostConstruct;
import org.springframework.stereotype.Component;
/**
* 方法缓存key常量
*
* @author SHANHY
*/
@Component
public class RedisKeys {
// 测试 begin
public static final String _CACHE_TEST = "_cache_test";// 缓存key
public static final Long _CACHE_TEST_SECOND = 20L;// 缓存时间
// 测试 end
// 根据key设定具体的缓存时间
private Map<String, Long> expiresMap = null;
@PostConstruct
public void init(){
expiresMap = new HashMap<>();
expiresMap.put(_CACHE_TEST, _CACHE_TEST_SECOND);
}
public Map<String, Long> getExpiresMap(){
return this.expiresMap;
}
}
package com.shanhy.example.redis;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.cache.interceptor.SimpleKeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.core.RedisTemplate;
/**
* 注解式环境管理
*
* @author 单红宇(CSDN catoop)
* @create 2016年9月12日
*/
@Configuration
@EnableCaching
public class CachingConfig extends CachingConfigurerSupport {
/**
* 在使用@Cacheable时,如果不指定key,则使用找个默认的key生成器生成的key
*
* @return
*
* @author 单红宇(CSDN CATOOP)
* @create 2017年3月11日
*/
@Override
public KeyGenerator keyGenerator() {
return new SimpleKeyGenerator() {
/**
* 对参数进行拼接后MD5
*/
@Override
public Object generate(Object target, Method method, Object... params) {
StringBuilder sb = new StringBuilder();
sb.append(target.getClass().getName());
sb.append(".").append(method.getName());
StringBuilder paramsSb = new StringBuilder();
for (Object param : params) {
// 如果不指定,默认生成包含到键值中
if (param != null) {
paramsSb.append(param.toString());
}
}
if (paramsSb.length() > 0) {
sb.append("_").append(paramsSb);
}
return sb.toString();
}
};
}
/**
* 管理缓存
*
* @param redisTemplate
* @return
*/
@Bean
public CacheManager cacheManager(RedisTemplate<String, Object> redisTemplate, RedisKeys redisKeys) {
RedisCacheManager rcm = new RedisCacheManager(redisTemplate);
// 设置缓存默认过期时间(全局的)
rcm.setDefaultExpiration(1800);// 30分钟
// 根据key设定具体的缓存时间,key统一放在常量类RedisKeys中
rcm.setExpires(redisKeys.getExpiresMap());
List<String> cacheNames = new ArrayList<String>(redisKeys.getExpiresMap().keySet());
rcm.setCacheNames(cacheNames);
return rcm;
}
}
package com.shanhy.example.service;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import com.shanhy.example.redis.RedisKeys;
@Service
public class TestService {
/**
* 固定key
*
* @return
* @author SHANHY
* @create 2017年4月9日
*/
@Cacheable(value = RedisKeys._CACHE_TEST, key = "'" + RedisKeys._CACHE_TEST + "'")
public String testCache() {
return RandomStringUtils.randomNumeric(4);
}
/**
* 存储在Redis中的key自动生成,生成规则详见CachingConfig.keyGenerator()方法
*
* @param str1
* @param str2
* @return
* @author SHANHY
* @create 2017年4月9日
*/
@Cacheable(value = RedisKeys._CACHE_TEST)
public String testCache2(String str1, String str2) {
return RandomStringUtils.randomNumeric(4);
}
}
package com.shanhy.example.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.jedis.RedisClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.shanhy.example.service.TestService;
/**
* 测试Controller
*
* @author 单红宇(365384722)
* @create 2017年4月9日
*/
@RestController
@RequestMapping("/test")
public class TestController {
private static final Logger LOG = LoggerFactory.getLogger(TestController.class);
@Autowired
private RedisClient redisClient;
@Autowired
private TestService testService;
@GetMapping("/redisCache")
public String redisCache() {
redisClient.set("shanhy", "hello,shanhy", 100);
LOG.info("getRedisValue = {}", redisClient.get("shanhy"));
testService.testCache2("aaa", "bbb");
return testService.testCache();
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有