Redis Basic Operations-20150608

Source: Internet
Author: User

Redis Basic Operations-20150608 "http://my.oschina.net/u/241255/blog/206991" Redis is an open source, written in ANSI C language, support network, memory-based, and persistent log-type, Key-value database, and provides APIs in multiple languages.

Unlike the common key-value structure, Redis's key supports a flexible data structure, with the exception of strings, hashes, lists, sets, and sorted sets. It is these flexible data structures that enrich Redis's application scenarios to meet more flexible storage needs on the business.

Redis data is stored in memory, and the underlying implementation is to write the Epoll event loop, rather than using a common framework such as open source libevent, so the read and write efficiency is high. To achieve data persistence, Redis supports periodic refreshes (which can be implemented by configuration) or write logs to save data to disk.

First, the data type:

As a key-value database, Redis also provides a mapping of key (key) and key value (value). However, in addition to the regular numeric value or string, the Redis key value can also be one of the following forms:

Lists (list)
Sets (collection)
Sorted sets (Ordered collection)
Hashes (hash table)

The data type of the key value determines the operation that the key value supports. Redis supports advanced atomic operations such as the intersection of lists, sets, or ordered sets, set, and set, and, if the type of the key value is a normal number, Redis provides self-increment atomic operations.

"Setnx"
Setnx name Hongwan_new
Because the original name has a corresponding value, so this modification does not take effect, and the return code is 0.

"Setex"
Set the value of the key to a string type of value and specify the validity period for this key value.
For example, we add a haircolor= red key-value pair and specify that it is valid for 10 seconds, and you can do this:
Redis 127.0.0.1:6379> Setex Haircolor Red
Ok
Redis 127.0.0.1:6379> Get Haircolor
"Red"
Redis 127.0.0.1:6379> Get Haircolor
(nil)
Redis 127.0.0.1:6379>
It is visible that the last call is 10 seconds later, so the value of the key corresponding to the Haicolor is not taken.


"SetRange"

Sets the substring of the value of the specified key.

For example, we want to replace the Hongwan 126 mailbox with a Gmail mailbox, so we can do this:
Redis 127.0.0.1:6379> Get Name
"[email protected]"
Redis 127.0.0.1:6379> SetRange Name 8 gmail.com
(integer) 17
Redis 127.0.0.1:6379> Get Name
"[email protected]"
Redis 127.0.0.1:6379>
8 of these are the beginning of the substitution of characters from subscript 8 (containing 8)

"Mset"
Setting the value of multiple keys at once, successfully returning OK means that all values are set, and a failure return of 0 means that no value is set.
Redis 127.0.0.1:6379> mset key1 HongWan1 key2 HongWan2
Ok
Redis 127.0.0.1:6379> Get Key1
"HongWan1"
Redis 127.0.0.1:6379> Get Key2
"HongWan2"
Redis 127.0.0.1:6379>

"Getset name Hongwan_new"
"GetRange name 0 6" "GetRange name-7-1"
"Mget key1 key2 Key3"

Redis Basic Operations-20150608

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.