First knowledge of redis

Source: Internet
Author: User

Redis is a storage service that supports structures such as K-V and data can be implemented (memcache data is memory data and cannot be implemented)

Let's enter the redis world to find out.

Enter redis through the command line:

Use PS aux | grep redis to check whether the redis-server is enabled and the corresponding port number.

/usr/local/redis26/redis-server /usr/local/redis26/6007.conf

Connect redis command line to port 6007 to access redis

/usr/local/redis26/redis-cli -h localhost -p 6007

Frequently Used commands: Get (get value with key), set (set key-Value Pair), TTL (view the expiration time of this key-value ), keys (view the key list that meets the query conditions)

It may be used: mget (multiple keys get value), hset-hget, incr, decr

Some scenarios: sortedset with sorting (see reference file 1)

Keys: Check the Key List through query conditions. * Regular Expressions are supported. Other regular expressions are not sure whether the key list is supported.

Redis localhost: 6007> keys ST: Client :*
1) "ST: Client: Soft: 1"
2) "ST: Client: edition: 100092983"
3) "ST: Client: Soft: 3"
4) "ST: Client: edition: 100093524"

Keys-2: nonexistent

redis localhost:6007> keys test(empty list or set)

Set-get:

redis localhost:6007> set test ‘test‘OKredis localhost:6007> get test"test"

DEL: delete a key. Note that if a key does not exist, Nil is returned when you use the command line get.

redis localhost:6007> del test(integer) 1redis localhost:6007> get test(nil)

TTL: view the expiration time of a key. If the key does not exist, TTL returns-1 (related to version,-1 before 2.8,-2 after 2.8). If the key exists and never expires, TTL returns-1. If the key exists and the expiration time is set, the remaining survival time of the key is returned.

redis localhost:6007> get test(nil)redis localhost:6007> ttl test(integer) -1redis localhost:6007> set test ‘test‘OKredis localhost:6007> ttl test(integer) -1redis localhost:6007> get test"test"

Expire: Set the key expiration time

redis localhost:6007> expire test 100(integer) 1redis localhost:6007> ttl test(integer) 98

Mget: obtains the value corresponding to multiple keys.

redis localhost:6007> mget test test11) "test"2) "test1"

Decr: auto-minus 1

redis localhost:6007> decr test(error) ERR value is not an integer or out of rangeredis localhost:6007> set test 3OKredis localhost:6007> decr test(integer) 2

Incr: auto-increment 1

redis localhost:6007> incr test(integer) 3redis localhost:6007> get test"3"

Append: String append

redis localhost:6007> append test ‘test‘(integer) 5redis localhost:6007> get test"3test"

All the above operations can be attributed to string operations. We can see that redis supports the string type, as well as the set, list, zset, and hash types.

Value Type: String, list, set, zset, hashlist: list, one-dimensional array, element Repeatable set: unordered set, one-dimensional array, element repeatable zset: Sorting set, one-dimensional array, ordered hash: hash, associated array

List Operation: array. The following table is 0, 1, 2, 3 ....

Redis localhost: 6007> lset list 0 'list0' (error) Err no such keyredis localhost: 6007> lpush list 'list0' (integer) 1 redis localhost: 6007> lget list (error) Err Unknown command 'lget' redis localhost: 6007> lrange list 0 11) "list0"
Explanation:
Lset: Using lset directly is incorrect because list must exist before this command can be used.
Lpush: add an element to the list. If the list does not exist, create a list and add an element.
Lget: no such command
Lrange: list the specified entries in the list.
Usage: lrange key start stop, start indicates that the starting subscript starts from 0, stop indicates that the ending subscript starts from 0, stop is not the number of items, but the bottom value at the end
redis localhost:6007> lrange list 0 01) "list0"redis localhost:6007> lpush list ‘list1‘(integer) 2redis localhost:6007> lrange list 0 11) "list1"2) "list0"

Redis localhost: 6007> lset list 0 'LIST-new0'
OK
Redis localhost: 6007> lrange list 0 1
1) "list-new0"
2) "list0"

Explanation:

Lrange has been explained before.

Lpush add elements to the list

Lset: lset key index value, which modifies the value in a subscript of the list. The complexity is O (1). The data structure is not a linked list.

 

Reference:

1, http://redis.readthedocs.org/en/latest/

2, about TTL: http://redis.readthedocs.org/en/latest/key/ttl.html

3, http://redis.io/topics/data-types4,http://blog.csdn.net/terryzero/article/details/5719924
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.