Home > Java > javaTutorial > body text

Detailed analysis of the survival time (expire) of redis keys in Java

黄舟
Release: 2017-07-27 15:27:51
Original
8369 people have browsed it

1. You can use the expire command in redis to set the survival time of a key. After the time is up, redis will automatically delete it
expire Set the survival time (unit/second)
pexpire Set the survival time (unit/millisecond)
ttl/pttl View the remaining survival time of the key
persist Cancel the survival time
expireat [key] unix timestamp 1351858600
pexpireat [key] unix timestamp (milliseconds) 1351858700000
2. Application scenarios
Limited-time promotions
Website data cache (for some data that needs to be updated regularly)
Limit the frequency of website visitor visits (for example: a maximum of 10 visits per minute)


    /** 
         * 限制网站访客访问频率(例如:1分钟最多访问10次),其中: 访客通过IP标识,即同一个IP在1分钟内仅能访问10次 
         */  
        @Test  
        public void test4(){  
              final String ip = "127.0.0.1" ;  
             Jedis redis = null;  
              //模拟同一个用户连续访问20次  
              for(int i =0;i <20;i ++){  
                  boolean t = validate(ip);  
                  if(t ){  
                     System. out.println("恭喜你,购票成功!" +i );  
                 } else{  
                      //获取当前ip过期时间  
                      redis = getRedis();  
                      break;  
                 }  
             }  
              while(true &&redis.ttl(ip)>0){  
                 System. out.println("抱歉,你访问过度频繁,请" +redis .ttl(ip )+"秒后再来访问!" );  
                  try {  
                     Thread. sleep(1000);  
                 } catch (InterruptedException e ) {  
                      e.printStackTrace();  
                 }  
             }  
             System. out.println("你可以再次访问了" );  
        }  
      
        /** 
         * 
         * @param ip 
         * @return  true: 可以访问,false: 表示已经达到最大上线 
         */  
        public boolean validate( String ip ) {  
             Jedis jedis = getRedis();  
             String value = jedis.get( ip);  
              if(value ==null||value.length()==0){ //第一次访问  
                  jedis.setex( ip, 60,String. valueOf(0));  
             } else{  
                  int v = Integer.parseInt (value );  
                  if(v >=10){  
                      return false ;  
                 }  
             }  
              jedis.incr( ip);  
              return true ;  
        }  
      
        /** 
         * @return 
         */  
        public Jedis getRedis() {  
              jedisPool = getJedisPool();  
             Jedis jedis = jedisPool.getResource();  
              return jedis ;  
        }  
      
        /** 
         * 
         */  
        public JedisPool getJedisPool() {  
             JedisPoolConfig poolConfig = new JedisPoolConfig();  
              // 控制一个pool最多有多少个 jedis实例。  
              poolConfig.setMaxTotal(1000);  
              // 控制一个pool最多有多少个状态为idle(空闲的)的 jedis实例。  
              poolConfig.setMaxIdle(10);  
              // 表示当borrow(引入)一个 jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnectionException;  
              poolConfig.setMaxWaitMillis(200000);  
              // 在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的 jedis实例均是可用的;  
              poolConfig.setTestOnBorrow(true);  
      
              return new JedisPool(poolConfig, host, port);  
        }
Copy after login

The above is the detailed content of Detailed analysis of the survival time (expire) of redis keys in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!