php How to set time in redis: 1. Cancel the survival time and set the key’s survival time to permanent, which is PERSIST. The code is [PERSIST session:captcha]; 2. Use TTL to see the survival time of a key. Command, code is [TTL session:captcha].
php redis method of setting time:
Redis provides survival time for keys, when the survival time is not specified , the survival time is permanent. Redis will automatically delete this key after the time expires. You can use the EXPIRE command, and the time unit is seconds. If a key is set to have a limited survival time, it will be set to permanent again when the SET key is reassigned:
SET session:captcha sd2a EXPIRE session:captcha 600
Cancel the survival time, and The key's lifetime is set to permanent, which is PERSIST:
PERSIST session:captcha
Use the TTL command to view the lifetime of a key. -1 means permanent or deleted upon expiration.
TTL session:captcha
The survival time will not be changed during Redis' INCR, LPUSH, HSET, ZREM and other commands.
If you want to control the time accurately to the millimeter, you need PEXPIRE, and use PTTL to check the remaining time.
What if you want to give an expiration time instead of how many seconds it will expire? You need EXPIREAT and PEXPIREAT. The parameter of EXPIREAT is the timestamp of expiration (seconds), and the parameter of PEXPIREAT is the expiration time of timestamp (milliseconds)
SET session:captcha sd2a EXPIREAT session:captcha 1399902009 PEXPIREAT session:captcha 1399902009000
Application scenario 1: Access frequency limit: We limit each user to 1 minute Can browse 10 pages. The pseudo code is as follows:
$isExists = EXISTS limit:user1:192.168.1.2 if($isExists){ $num = INCR limit:user1:192.168.1.2 if($num > 10){ print '超过限制' exit } }else{ MULTI INCR limit:user1:192.168.1.2 EXPIRE limit:user1:192.168.1.2 60 EXEC }
The reason why we use transactions is because after joining, after executing INCR limit:user1:192.168.1.2 and before executing EXPIRE limit:user1:192.168.1.2 60, the client is closed. Then the key and value will be persisted. And this ID can only be accessed 10 times in a lifetime. That's too bad.
If you want to know more about programming learning, please pay attention to the php training column!
The above is the detailed content of php redis any set time. 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

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

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver CS6
Visual web development tools
