Redis document translation_key sets the expiration time, redis_key

Source: Internet
Author: User
Tags redis version

Redis document translation_key sets the expiration time, redis_key

Available since 1.0.0. Use the Start version 1.01

Time complexity: O (1) Time complexity O (1)

Source: http://blog.csdn.net/column/details/redisbanli.html

Set a timeout onkey. After the timeout has expired, the key will automatically be deleted. A key with an associated timeout is often said to be volatile in Redis terminology.

Set a timeout value on the key. After the expiration time, the key is automatically deleted. Key is associated with a timeout time. In Redis terminology, it is volatile (volatile)


The timeout is cleared only when the key is removed using the DEL command or overwritten using the SET or GETSET commands. This means that all the operations that conceptuallyAlterThe value stored at the key without replacing it with a new one will leave the timeout untouched. for instance, incrementing the value of a key with INCR, pushing a new value into a list with LPUSH, or altering the field value of a hash with HSET are all operations that will leave the timeout untouched.

The time-out period is cleared only when the DEL command is used or the SET or GETSET name is used for rewriting. In terms of all operations, the key is not replaced in concept. Changing the stored value will be associated with a new timeout value. For example, if you use INCR to increment a value, use LPUSH to add a new value to the list, or use HEST to set a new value for the hash field, all operations reassociate a timeout value.
The timeout can also be cleared, turning the key back into a persistent key, using thePERSIST command.
You can also use the PERSIST command to convert a key with an expiration time to a permanent key. If a key is renamed with RENAME, the associated time to live is transferred to the new key name.
If the key is re-executed by RENAME, the associated time will be transferred to the new key name. If a key is overwritten by RENAME, like in the case of an existing key Key_AThat is overwritten by a call like RENAME Key_B Key_A, It does not matter if the original Key_AHad a timeout associated or not, the new key Key_AWill inherit all the characteristics Key_B.
If a key is overwritten by the RENAME command, for example, if a Key_A already exists, run the RENAME Key_ B Key_A command (RENAME key_ B as KEY_A), no matter whether the original Key_A has an association timeout time, the new Key_A inherits all the features of Key_ B.
Refreshing expires refresh expiration It is possible to call EXPIRE using as argument a key that already has an existing expire set. In this case the time to live of a key is UpdatedTo the new value. There are too useful applications for this, an example is too ented in Navigation sessionPattern section below.
You can use the EXPIRE command to set a new timeout value for the key that already exists. If this is done, the timeout time for associating this key will be updated to the new value. It is very useful in many applications, such as the example in the next section. Differences in Redis prior 2.1.3 difference before Redis 2.1.3 In Redis versions prior 2.1.3Altering a key with an expire set using a command altering its value had the effect of removing the key entirely. This semantics was needed because of limitations in the replication layer that are now fixed.
Before Redis2.1.3, run the command to change the key timeout settings, which will explicitly affect key deletion. Because at that time the limitations on the replication layer were fixed, This Semantics was necessary. Return value Integer reply, specifically: Special Iteger reply
  • 1If the timeout was set. 1 indicates that the timeout limit is set.
  • 0IfkeyDoes not exist or the timeout cocould not be set. 0 indicates that the key does not exist or the key does not set the timeout time.
Examples redis> SET mykey "Hello"
OK
Redis> EXPIRE mykey 10
(integer) 1
Redis> TTL mykey
(integer) 10
Redis> SET mykey "Hello World"
OK
Redis> TTL mykey
(integer) -1
Redis>
Pattern: Navigation session image session Navigation Imagine you have a web service and you are interested in the latest N pages RecentlyVisited by your users, such that each adjacent page view was not saved med more than 60 seconds after the previous. Conceptually you may think at this set of page views as Navigation sessionIf your user, that may contain in interesting information about what kind of products he or she is looking for currently, so that you can recommend related products.
Imagine that you have a web service, and you are very interested in the last N web pages accessed by your users, so that the access to each of the previous adjacent pages does not take more than 60 seconds. In terms of concept, you may think that a page with a large page views is the navigation session set by your user, which may include interesting information about the product category that he or she is currently looking, therefore, you can recommend related products. You can easily model this pattern in Redis using the following strategy: every time the user does a page view you call the following commands:
In Redis, you can use a simple mode: each time a user accesses a webpage, execute the following command:
MULTIRPUSH pagewviews.user:<userid> http://.....EXPIRE pagewviews.user:<userid> 60EXEC

If the user will be idle more than 60 seconds, the key will be deleted and only subsequent page views that have less than 60 seconds of difference will be recorded.

If the user has idle webpages for more than 60 seconds, the key will be deleted and the record will be rerecorded only when the number of different webpages accessed subsequently is less than 60 seconds.

This pattern is easily modified to use counters using INCR instead of lists usingRPUSH.

This mode uses the RPUSH code of list from Zeng INCR. Appendix: Redis expires attachment: Redis expired
Keys with an expire key expiration Normally Redis keys are created without an associated time to live. the key will simply live forever, unless it is removed by the user in an explicit way, for instance using the DEL command.
Generally, no expiration time is associated with key creation. This key will always exist unless it is explicitly deleted by the user, for example, the user uses the DEL command.

The EXPIRE family of commands is able to associate an expire to a given key, at the cost of some additional memory used by the key. when a key has an expire set, Redis will make sure to remove the key when the specified amount of time elapsed.

The EXPIRE command family can be associated to give a key, with additional memory overhead. When a key has an expiration time set, Redis ensures that the key is deleted when the expiration time expires.

The key time to live can be updated or entirely removed using the EXPIRE andPERSIST command (or other strictly related commands ).

The expiration time of the key is updated or deleted. Use the EXPIRE and PERSIST commands. Expire accuracy expiration time accuracy

In Redis 2.4 the expire might not be pin-point accurate, and it cocould be between zero to one seconds out.

Before version 2.4, the expiration time may be inaccurate and there may be an error between 0 and 1 second.

Since Redis 2.6 the expire error is from 0 to 1 milliseconds.

The error after version 2.6 is between 0 and 1mm.
Expires and persistence expiration and persistent Keys expiring information is stored as absolute Unix timestamps (in milliseconds in case of Redis version 2.6 or greater ). this means that the time is flowing even when the Redis instance is not active.
The expiration time of the key is saved using the Unix timestamp (in millimeters from 2.6 ). This means that even if the Redis instance has no running time, it is also lost.
For expires to work well, the computer time must be taken stable. if you move an RDB file from two computers with a big desync in their clocks, funny things may happen (like all the keys loaded to be expired at loading time ).
In order to make the expiration time work stably, the computer time must be ensured. If you move RDB files between two computers with a high latency, a lot of interesting things may happen (for example, all keys with an expiration time set expire upon loading ).
Even running instances will always check the computer clock, so for instance if you set a key with a time to live of 1000 seconds, and then set your computer time 2000 seconds in the future, the key will be expired immediately, instead of lasting for 1000 seconds.
Even Redis is always detecting computer time, so if you set a key to survive for 1000 seconds, and then you set your computer time to 2000 seconds later, the key will expire immediately and have expired for 1000 seconds. How Redis expires keys Redis key expired

Redis keys are expired in two ways: a passive way, and an active way.

There are two methods for Redis key Expiration: passive expiration and active expiration.

A key is actively expired simply when some client tries to access it, and the key is found to be timed out.

Active expiration is relatively simple. When a client accesses this key, find this key and make it time-out. Of course this is not enough as there are expired keys that will never be accessed again. these keys shoshould be expired anyway, so periodically Redis tests a few keys at random among keys with an expire set. all the keys that are already expired are deleted from the keyspace.
Of course, this is not enough, because some keys may not be accessed after the expiration time is set. These keys should be expired in any case. Therefore, Redis regularly selects keys with expiration time to check. Expired keys will be deleted from the key space. Specifically this is what Redis does 10 times per second:
Redis performs 10 checks per second: 1: Test 20 random keys from the set of keys with an associated expire. randomly selects 20 keys with an expiration time. 2: Delete all the keys found expired. Delete expired keys. 3: If more than 25% of keys were expired, start again from step 1. If more than 25% of keys are timed out, start step 1.
This is a trivial probabilistic algorithm, basically the assumption is that our sample is representative of the whole key space, and we continue to expire until the percentage of keys that are likely to be expired is under 25%
This probability algorithm is not important. On the subject, assume that our samples represent the entire key space and continue to expire the key until more than 25% of the keys expire, and then start the first step.
This means that at any given moment the maximum amount of keys already expired that are using memory is at max equal to max amount of write operations per second divided by 4.
Indicates the maximum number of expired keys at any time. The maximum memory used by these expired keys is equal to the maximum number of write operations per second divided by four times. How expires are handled in the replication link and AOF file expiration in the AOF file and replication connection

In order to obtain a correct behavior without sacririicing consistency, when a key expires, a DEL operation is synthesized in both the AOF file and gains all the attached slaves. this way the expiration process is centralized in the master instance, and there is no chance of consistency errors.

To get the correct method without sacrificing consistency, when a key expires, the DEL operation will be synchronized between the AOF file and all slaves. In this way, the expiration date is processed in the master instance and the consistency error is avoided.

However while the slaves connected to a master will not expire keys independently (but will wait for the DEL coming from the master ), they'll still take the full state of the expires existing in the dataset, so when a slave is elected to a master it will be able to expire the keys independently, fully acting as a master.

However, when slaves is connected to the master, the key will not expire independently (but will wait for the arrival of the master DEL). They will use all keys that have expired status in the Data settings, therefore, when a Server Load balancer instance is elected as the master node, it will start to expire the key independently and act as the maste role.

Source: http://blog.csdn.net/column/details/redisbanli.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.