Detailed analysis of the survival time (expire) of redis keys in Java
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); }
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

On CentOS systems, you can limit the execution time of Lua scripts by modifying Redis configuration files or using Redis commands to prevent malicious scripts from consuming too much resources. Method 1: Modify the Redis configuration file and locate the Redis configuration file: The Redis configuration file is usually located in /etc/redis/redis.conf. Edit configuration file: Open the configuration file using a text editor (such as vi or nano): sudovi/etc/redis/redis.conf Set the Lua script execution time limit: Add or modify the following lines in the configuration file to set the maximum execution time of the Lua script (unit: milliseconds)

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

In Debian systems, readdir system calls are used to read directory contents. If its performance is not good, try the following optimization strategy: Simplify the number of directory files: Split large directories into multiple small directories as much as possible, reducing the number of items processed per readdir call. Enable directory content caching: build a cache mechanism, update the cache regularly or when directory content changes, and reduce frequent calls to readdir. Memory caches (such as Memcached or Redis) or local caches (such as files or databases) can be considered. Adopt efficient data structure: If you implement directory traversal by yourself, select more efficient data structures (such as hash tables instead of linear search) to store and access directory information
