redis: host: 172.27.15.23 port: 6379 database: 0 pool: max-idle: 20 min-idle: 1 max-active: 20 max-wait: 60000
import org.springframework.cache.CacheManager;import org.springframework.cache.annotation.EnableCaching;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;import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;@Configuration@EnableCachingpublic class RedisConfig { @Bean public CacheManager cacheManager(RedisTemplate redisTemplate){ redisTemplate.setKeySerializer(new GenericJackson2JsonRedisSerializer()); redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate); return redisCacheManager; }}
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;import org.springframework.data.redis.serializer.StringRedisSerializer;/** * redis 替換預設的序列化key 和value 的對象,解決伺服器讀取亂碼。(不一定必須解決這個問題,只用於伺服器讀取直觀顯示。)* @time 2017年11月22日 上午11:00:53 **/@Configurationpublic class RedisConfig { @Autowired private RedisTemplate redisTemplate; @Bean public RedisTemplate redisTemplateInit() { //設定序列化Key的執行個體化對象 redisTemplate.setKeySerializer(new StringRedisSerializer()); //設定序列化Value的執行個體化對象 redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); return redisTemplate; }}
@Test public void saveMobileCodeInRedis(){ //1手機號,code , 失效時間 ,是否已經使用// Code code = new Code("15818232009",123456,0); String mobile = "15818232009"; String code = "654321"; Long timeout = new Long(600); //600秒到期// 使用者發驗證碼之前判斷緩衝中是否存在該手機的沒有失效的驗證碼 ValueOperations<String, String> operations = redisTemplate.opsForValue(); if(! redisTemplate.hasKey(mobile)) { System.out.println("寫入緩衝"); operations.set(mobile, code, timeout, TimeUnit.SECONDS);// 寫入緩衝 } //下面使用 if(redisTemplate.hasKey(mobile)){ if(operations.get(mobile).equals(code)){ System.out.println("成功"); Long time = redisTemplate.getExpire(mobile, TimeUnit.MILLISECONDS);//根據key擷取到期時間並換算成指定單位 System.out.println("到期時間剩餘(ms)=="+time+"ms");// redisTemplate.delete(mobile); } } } @Test @Ignore public void testRedis() { print(); } private List print() { ValueOperations<String, List> list = redisTemplate.opsForValue(); List<Department> results = list.get("departments:info:all"); for (Department d : results) { System.out.println(d); } return results; } //1從資料庫中擷取部門資料 //2寫一個攔截器,攔截insert或者update,delete或者,監聽到有變化就更新部門緩衝,但是問題來了,如果是直接修改資料庫呢。。對於這種,晚上0點再更新緩衝 private void setDepartmentsInRedis() { //練習返回String , List , Hash , Set , ZSet List<Department> departments = departmentMapper.selectAll();// 比較推薦的方式是使用“業務名:對象名:id:[屬性]”作為鍵名 redisTemplate.opsForValue().set("echo:department:all", departments, 200, TimeUnit.SECONDS);// 寫入緩衝,200秒到期 }