本文共 3571 字,大约阅读时间需要 11 分钟。
org.springframework.boot spring-boot-starter-data-redis
spring.redis.database=10spring.redis.host=localhostspring.redis.port=6379spring.redis.password=spring.redis.pool.max-active=8spring.redis.pool.max-wait=-1spring.redis.pool.max-idle=8spring.redis.pool.min-idle=0spring.redis.timeout=0
@RunWith(SpringJUnit4ClassRunner.class)@SpringApplicationConfiguration(Application.class)public class ApplicationTests { @Autowired private StringRedisTemplate stringRedisTemplate; @Test public void test() throws Exception { // 保存字符串 stringRedisTemplate.opsForValue().set("neo", "chen"); Assert.assertEquals("chen", stringRedisTemplate.opsForValue().get("neo")); }}
stringRedisTemplate模板用于存储key,value为字符串的数据
@Autowired private StringRedisTemplate stringRedisTemplate; @RequestMapping("/test") @ResponseBody public String test() { String message = ""; stringRedisTemplate.opsForValue().set("hello", "world"); message = stringRedisTemplate.opsForValue().get("hello"); return message; }
等同于
@Autowired private RedisTemplateredisTemplate;
ListOperations
public class Example { // inject the actual template @Autowired private RedisTemplatetemplate; // inject the template as ListOperations // can also inject as Value, Set, ZSet, and HashOperations @Resource(name="redisTemplate") private ListOperations listOps; public void addLink(String userId, URL url) { listOps.leftPush(userId, url.toExternalForm()); // or use template directly redisTemplate.boundListOps(userId).leftPush(url.toExternalForm()); }}
例 2.4. RedisTemplate
@Autowired private RedisTemplateredisTemplate; public List getProtocol() { List protocols = new ArrayList (); Gson gson = new Gson(); Type type = new TypeToken
>(){}.getType(); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new StringRedisSerializer()); String cacheKey = String.format("%s:%s", this.getClass().getName(), Thread.currentThread().getStackTrace()[1].getMethodName()); long expireTime = 5; if(redisTemplate.hasKey(cacheKey)){ String cacheValue = redisTemplate.opsForValue().get(cacheKey); System.out.println(cacheValue); protocols = gson.fromJson(cacheValue, type); }else{ Protocol protocol = new Protocol(); protocol.setRequest(new Date().toString()); protocols.add(protocol); String jsonString = gson.toJson(protocols, type); System.out.println( jsonString ); redisTemplate.opsForValue().set(cacheKey, jsonString); redisTemplate.expire(cacheKey, expireTime, TimeUnit.SECONDS); } return protocols; }
org.springframework.boot spring-boot-starter-cache
缓存返回结果
@Cacheable("cacheable") @RequestMapping("/test/cacheable") @ResponseBody public String cacheable() { Date date = new Date(); String message = date.toString(); return message; }
5秒钟清楚一次缓存
@Scheduled(fixedDelay = 5000) @CacheEvict(allEntries = true, value = "cacheable") public void expire() { Date date = new Date(); String message = date.toString(); System.out.println(message); }
原文出处:Netkiller 系列 手札
本文作者:陈景峯 转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。