<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
spring.redis.database=10 spring.redis.host=localhost spring.redis.port=6379 spring.redis.password= spring.redis.pool.max-active=8 spring.redis.pool.max-wait=-1 spring.redis.pool.max-idle=8 spring.redis.pool.min-idle=0 spring.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 RedisTemplate<String, String> redisTemplate;
ListOperations
public class Example { // inject the actual template @Autowired private RedisTemplate<String, String> template; // inject the template as ListOperations // can also inject as Value, Set, ZSet, and HashOperations @Resource(name="redisTemplate") private ListOperations<String, String> 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 RedisTemplate<String, String> redisTemplate; public List<Protocol> getProtocol() { List<Protocol> protocols = new ArrayList<Protocol>(); Gson gson = new Gson(); Type type = new TypeToken<List<Protocol>>(){}.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; }
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency>
缓存返回结果
@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 系列 手札
本文作者:陈景峯
转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。