Redis: Basic knowledge and common data types and keywords

Source: Internet
Author: User
Redis: What is redis:

Remote dictionary server (Remote dictionary server)

It is completely open-source and free. It is written in C language and complies with the BSD Protocol. It is a high-performance (key-value) distributed memory database.

Nosql databases that run in memory and support persistence are one of the most popular nosql databases and are also called Data Structure servers.

 

Redis has three features:

1) redis supports data persistence. Data in the memory can be stored on the disk and loaded again during restart.

2) redis not only supports simple key-value data, but also stores data structures such as list, set, zset, and hash.

3) redis supports data backup, that is, data backup in master-slave mode.

 

What redis can do:

1) memory storage and persistence: redis supports asynchronously writing data in the memory to the hard disk without affecting the service continuity.

2) obtain the latest record. For example, place the IDs of the latest 10 comments in the list set of redis.

3) simulate a function similar to httpsession that requires an expiration time

4) Publish and subscribe to the Message System

5) timer and counter

 

Redis basic commands and simple demos:

Start the server: redis-Server

Connect to the client: redis-cli-P 6379 (port)

Save: set key value

 

Get: Get

 

View: Keys match

 

Clear the current database: flushdb

 

Clear all databases: flushall

Select Database: select coordinates

 

Key command

1) determine whether there is

Exists key

2) query the key of the current database

Keys (matching)

3) Move key

Move key library number

4) set the expiration time for the given key

Expire key second

5) Check the number of seconds to expire.-1 indicates that the instance will never expire, and-2 indicates that the instance has expired.

TTL key

6) Check the type of your key.

Type

7) delete key

Del key

Five common types: String)

String is the most basic type of redis, and string is binary secure. Can contain any data. For example, JPG images or serialized objects. A redis string value can be 512 MB at most.

1) append

Append key content

2) obtain the string length

Strlen key

3) operations on numbers (must be numbers)

Add 1: incr key

Reduce 1: decr key

Add a value: incrby

Reduce a value: decrby key value

4) intercept operations

Start coordinate and end coordinate of the getrange key (-1 indicates all)

5) range replacement

Content of starting coordinate replacement for setrange Creation

6) set the expiration time (set with expire) during creation)

Create and set: setex key second speed value

  

7) creation can only be performed if no one exists (If yes, it will not be created) (set if not exist)

Setnx key value

8) create multiple and obtain multiple msets/mget

  

Msetnx (created successfully only if it does not exist)

9) first get and then change the value GetSet

  

 

List)

The underlying layer is a linked list.

1) lpush/rpush/lrange

2) Left-out and right-out lpop/rpop

  

3) obtain the element lindex key subscript number by coordinate

4) obtain the llen key of the set length

5) delete n lrem key count values

6) intercept the value of the specified range and assign it to the start position and end position of the key ltrim key.

7) Transfer the original list Object List of one element rpoplpush

  

8) Replace the lset key index value at a certain position

8) insert a value next to a value. The value of linsert key before/after is 1 and 2.

Summary:

1. The key does not exist. Create a New linked list. The key already exists. add content.

2. If no value exists, the corresponding key disappears.

3. Fast handling of the opposite position, embarrassing in the middle ~

Set)

Redis's set is a unordered set of the string type, which is implemented through hashtable.

1) insert set key value1/value2/value3 (the value already exists and only one is added)

2) view the smembers key

3) Number of sismember key values

4) obtain the number of elements in the set. scard key

5) Delete the element Srem key value in the collection.

6) Number of srandmember keys in an integer (several random numbers)

7) random output stack spop key

8) assign a value of key1 to key2 smove key1 key2 Value

9) Processing of digital collection classes

Difference set: sdiff source key target key

Intersection: sinter source key target key

Union: sunion source key target key

 

Hash)

Similar to Java Map

Redis hash is a set of key-value pairs. It is a string-type field and value ing table. Hash is particularly suitable for storing objects.

1) hset/mget/hmset/hmet/hgetall/hdel

  

2) query length hlen

3) whether a key has a key hexists key1 key2

4) obtain all key hkeys keys

5) obtain all value hvals keys

6) add hincrby/hincrbyfloat

7) If yes, the hsetnx key primary key sub-value is assigned.

Zset (sorted set: ordered set)

Redis zset and set are also set of string elements, and repeated members are not allowed.

The difference is that each element is associated with a score of the double type.

Redis officially uses scores to sort members in the set from small to large. Zset members are unique, but scores can be repeated.

1) zadd key Score 1 value1 score 2 value2

2) Get the value zrange key starting position ending position

3) obtain the starting position and ending position of zrange key with a join value and a score withscores

4) obtain the zrangebyscore key based on the score range.

Note: end score band (indicating not including

+ Number of limit start offset interceptions

5) Delete the zrem key element of the value corresponding to a score.

6) obtain the number of zcards

7) Get the number of score ranges zcount key start score end score

8) obtain the subscript zrank key value based on the Value

9) obtain the score zscore key value based on the Value

10) obtain the subscript zrevrank key value in reverse order

11) reverse output value zrevrange key start position end position

12) obtain the zrevrangebyscore in reverse order. The key has a high score and a low score.

Common redis data operation commands

Http://redisdoc.com/

Redis: Basic knowledge and common data types and keywords

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.