Redis Learning Three: Redis data types

Source: Internet
Author: User
First, the five data types of Redis 1.String (String)

The string is the most basic type of redis, and you can understand it as a type that is identical to memcached, a key that corresponds to a value.
The string type is binary safe. This means that a Redis string can contain any data. For example, JPG images or serialized objects.
The string type is the most basic data type of Redis, and a Redis value can be up to 512M

2.Hash (hash, similar to a map in Java)

A Redis hash is a collection of key-value pairs.
Redis Hash is a string-type field and value mapping table, and hash is particularly useful for storing objects.
Java-like map<string,object>

3.List (list)

The Redis list is a simple list of strings, sorted by insertion order. You can add an element guide to the head (left) or tail (right) of the list.
Its bottom line is actually a chain list.

4.Set (Collection)

Redis's set is an unordered collection of type string. It is implemented through Hashtable,

5.Zset (sorted set: Ordered set)

Zset (sorted set: Ordered set)
Redis Zset and set are also collections of string-type elements and do not allow duplicate members.
The difference is that each element is associated with a double-type fraction.
Redis is a small-to-large ordering of the members in a collection by fractions. Zset members are unique, but fractions (score) can be duplicated.

Where to get redis common data Type Operations Command: Http://

Second, the Redis key (key) 1. Common commands:

2. Case studies

Keys *

Exists Key's name to determine if a key exists

Move key DB---> Current library is gone, removed

Expire key seconds: Set expiration time for the given key

TTL key to see how many seconds are out of date, 1 means never expire, 2 means expired

Type key to see what your key is

Three, Redis string (String)

Single-valued value

1. Common

2. Case studies


Incr/decr/incrby/decrby, it must be a number to add and subtract.


GetRange: Gets the value within the range of the specified interval, similar to the between......and relationship
From zero to minus one means all

SetRange sets the value in the range of the specified interval, in the format SetRange key value specific value

Setex (set with expire) key seconds value/SETNX (set if not exist)

Setex: Set the key with expiration time, dynamic setting.
Setex key seconds value real value

SETNX: Sets the value of key only if the key does not exist.

1 is successful, 0 is unsuccessful


Mset: Set one or more key-value pairs at the same time.

Mget: Gets the value of all (one or more) given key.

MSETNX: Set one or more key-value pairs at the same time, when and only if all given key does not exist.

Getset (Get and set first)

Getset: Sets the value of the given key to value and returns the old value of the key.
A simple word, get it first, and set it immediately.

Iv. Redis List

Single-valued Multiple value

1. Common

2. Case studies



Lindex, get elements by index subscript (top to bottom)

Get the elements in the list by index lindex key index


Lrem Key Delete n value

* Remove 2 elements equal to V1 from left to right and return the actual number of deleted values
* Lrem List3 0 value, indicating the deletion of all given values. 0 is the full value.

LTrim key starts index end index, intercepts the value of the specified range and assigns the value to key

LTrim: Intercepts the element of the specified index interval, in the form of the LTrim list's key start index end index

Rpoplpush Source list Destination List

Removes the last element of the list and adds the element to another list and returns

LSet Key index value

Linsert key Before/after value 1 value 2

Add a specific value before and after a list of existing values

Performance Summary

It is a list of strings, left and right can be inserted to add;
If the key does not exist, create a new linked list;
If the key already exists, what's new;
If the value is completely removed, the corresponding key disappears.
The operation of a linked list is extremely efficient both in terms of head and tail, but it is very inefficient to operate on intermediate elements.

V. Redis collection (set) 1. Common

Single-valued Multiple value

2. Case studies


SCard, gets the number of elements inside the collection

Srem key value deletes the elements in the collection

Srandmember key An integer (several random numbers)

* 2 randomly removed from set set
* If the maximum number is exceeded, remove all
* If the write value is negative, such as-3, indicating that 3 needs to be removed, but there may be duplicate values.

Spop key random out Stack

Smove Key1 Key2 A value in Key1 is to assign a value in Key1 to Key2

Math Set Class

Difference set: Sdiff

The entry in the first set and not in any of the following set

Intersection: Sinter

Set: Sunion

Six, Redis hash (hash) "Focus"

The KV mode is constant, but V is a key-value pair

1. Common

2. Case studies



Key for a value in key Hexists key




There is no assignment and there is an invalid value.

Vii. Redis ordered set Zset (sorted set)

On set basis, add a score value.
Before set is K1 v1 v2 v3,
Now Zset is K1 score1 v1 score2 v2

1. Common

2. Case studies


Zrangebyscore key starts score end score

Zrem key a score corresponding value value, the function is to delete the element

Delete the element, the format is the value of the key entry for Zrem Zset, and the value of the item can be multiple

Zrem key score a corresponding value, which can be multiple values

Zcard/zcount key score Interval/zrank key values, the function is to obtain the subscript value/zscore key corresponding value, to obtain the score

Zcard: Gets the number of elements in the collection

Zcount: Gets the number of elements in the fractional interval, zcount key start fractional interval end fraction interval

Zrank: Gets the subscript position of value in Zset

Zscore: Get the corresponding score according to the value

Zrevrank key values, in reverse order to obtain the subscript value

Get subscript index value in positive sequence and reverse order


Zrevrangebyscore key end score start score

Zrevrangebyscore Zset1 withscores score is reversed.


"Silicon Valley Redis Video Learning"

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: and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: