Redis在实际项目中的使用

  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
@EnableCaching
public 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 

**/
@Configuration
public 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秒过期
    }

猜你喜欢

转载自blog.csdn.net/dgutliangxuan/article/details/79003474