Redis在實際項目中的使用

來源:互聯網
上載者:User
  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秒到期    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.