Key expiration lets you set a timeout for a key, also known as a "time to live", or "TTL". Of value stored at the specified key: > set mykey xīefore moving on, we should look at an important Redis feature that works regardless of the type of value you're storing: key expiration. There are many key space related commands, but the above two are theĮssential ones together with the TYPE command, which returns the kind The key was removed (it existed) or not (there was no such key with that > set mykey helloįrom the examples you can also see how DEL itself returns 1 or 0 depending on whether In order to interact with the space of keys, and thus, can be used withįor example the EXISTS command returns 1 or 0 to signal if a given keyĮxists or not in the database, while the DEL command deletes a keyĪnd associated value, whatever the value is. There are commands that are not defined on particular types, but are useful When MGET is used, Redis returns an array of values. The MSET and MGET commands: > mset a 10 b 20 c 30 The ability to set or retrieve the value of multiple keys in a singleĬommand is also useful for reduced latency. You can GETSET the key, assigning it the new value of "0" and reading the Information once every hour, without losing a single increment. System that increments a Redis key using INCRĮvery time your web site receives a new visitor. You can use this command, for example, if you have a The GETSET command sets a key to a new value, returning the old value as the There are a number of commands for operating on strings. The final value will always beġ2 and the read-increment-set operation is performed while all the otherĬlients are not executing a command at the same time. Increment to 11, and set the new value to 11. Happen that client 1 reads "10", client 2 reads "10" at the same time, both The same key will never enter into a race condition. That even multiple clients issuing INCR against There are other similar commands like INCRBY,Īlways the same command, acting in a slightly different way. Increments it by one, and finally sets the obtained value as the new value. The INCR command parses the string value as an integer, For instance, one is atomic increment: > set counter 100 Or the opposite, that it only succeed if the key already exists: > set mykey newval nxĮven if strings are the basic values of Redis, there are interesting operations For example, I may ask SET to fail if the key already exists, The SET command has interesting options, that are provided as additionalĪrguments. Values can be strings (including binary data) of every kind, for instance youĬan store a jpeg image inside a value. The key is associated with a non-string value. Note that SET will replace any existing valueĪlready stored into the key, in the case that the key already exists, even if > set mykey somevalueĪs you can see using the SET and the GET commands are the way we setĪnd retrieve a string value. Will be performed via redis-cli in this tutorial). Let's play a bit with the string type, using redis-cli (all the examples The string data type is usefulįor a number of use cases, like caching HTML fragments or pages. We are mapping a string to another string. Since Redis keys are strings, when we use the string type as a value too, ![]() It is the only data type in Memcached, so it is also very natural The Redis String type is the simplest type of value you can associate withĪ Redis key. The maximum allowed key size is 512 MB. ![]() Dots or dashes are often used for multi-wordįields, as in "comment:4321:reply.to" or "comment:4321:reply-to". While short keys will obviouslyĬonsume a bit less memory, your job is to find the right balance. The key object itself and the value object. Is more readable and the added space is minor compared to the space used by "u1000flw" as a key if you can instead write "user:1000:followers". ![]()
0 Comments
Leave a Reply. |