Five basic data structure Redis
A: redis installation and startup
redis download and install the Reference blog: https: //www.cnblogs.com/jylee/p/9844965.html
In the catalog will redis after decompression, open cmd
redis service start command: redis-server.exe redis.windows.conf
connect redis : redis-cli.exe -h 127.0.0.1 -p 6379
or open a folder directly redis-cli.exe
instruction if not, then configure the environment variables connected redis, it must open in redis extraction path cmd
Two: Redis basic data types
Tutorial: https: //www.runoob.com/redis/redis-data-types.html
reference books: https: //book.douban.com/subject/30386804/
me it can not be considered a reference, and basically be compared books knocked over, first time to write blog posts, but also not very clear points of focus is not the focus, forgive me. Forgive me. .
1, String (String)
- Description: String redis is the most basic and simple data types, inside is an array of characters. ** redis all data structures are based on a unique key string as the name, and then obtain the corresponding data value of the unique key value. (Key are strings, value indicates there are five) ** key-value, the maximum storable 512M
- Characteristics: String may contain any data, binary security, such as jpg image type, java serialized object
- Applications: Cache user information
Redis strings are dynamic, it can be modified to achieve the internal mechanism of the ArrayList similar to Java, using pre-allocated space redundant way to allocate memory less frequent
String basic operations
Key-value pairs:
The equivalent of a dictionary key, value, support simple crud, the following name-key codehole-value
127.0.0.1:6379> set name codehole
OK
127.0.0.1:6379> get name
"codehole"
127.0.0.1:6379> exists name
(integer) 1
127.0.0.1:6379> del name
(integer) 1
127.0.0.1:6379> get name
(nil)
Batch key-value pairs:
Batch operations may be performed on a plurality of strings, save network time-consuming overhead
127.0.0.1:6379> set name1 codehole
OK
127.0.0.1:6379> set name2 holycoder
OK
127.0.0.1:6379> mget name1 name2 name3
1) "codehole"
2) "holycoder"
3) (nil)
127.0.0.1:6379> mset name1 boy name2 girl name3 unkonwn
OK
127.0.0.1:6379> mget name1 name2 name3
1) "boy"
2) "girl"
3) "unkonwn"
Expiration and set command extensions:
Can set the expiration time for the key, automatically delete the time, often used for cache control effective time
127.0.0.1:6379> set name codehole
OK
127.0.0.1:6379> expire name 5 #设置name的过期时间5秒
(integer) 1
127.0.0.1:6379> get name
(nil)
127.0.0.1:6379> setex name 5 codehole #set 与expire 结合起来用
OK
127.0.0.1:6379> get name
(nil)
127.0.0.1:6379> setnx name codehole #如果name不存在就执行set创建
(integer) 1
127.0.0.1:6379> get name
"codehole"
127.0.0.1:6379> setnx name holycoder #如果存在就不执行
(integer) 0
127.0.0.1:6379> get name
"codehole"
count:
If the value is an integer, it may be increment operation, in the range between the maximum and minimum signed long
127.0.0.1:6379> set age 30
OK
127.0.0.1:6379> incr age
(integer) 31
127.0.0.1:6379> incrby age 5
(integer) 36
127.0.0.1:6379> incrby age -5
(integer) 31
String basic finishing operations
set key value
Storing a key-value pairget key
The key to the key corresponding to the obtained valueexists key
The key is determined whether or not theredel key
Delete key-value pairs according to keymset key1 value1 key2 value2...
Storing a plurality of key valuemget key1 key2
The plurality of key, returns a value listexpire key second
Set the expiration time of a keysetex key second value
It is equivalent to set + expiresetnx key value
If the key does not exist to perform, there is executedincr key
value is an integer +1incrby key +-number
value ±number
2, Hash (hash dictionary)
When watching this part, talked about, and the difference in the rehash rehash redis in the java rehash, and did not understand this, it is unclear rehash what is needed to fix a back up.
- Description: a collection of key-value pairs, equivalent to two-dimensional structure of the Java language HashMap, disorderly dictionary, an array of linked list +
- Characteristics: suitable for storing objects, a property value can be updated only in the map and without removing the entire map
- Applications: storing user information may be used, hash structure of the user may be stored separately in each field, can be acquired on demand acquiring
Storage consumption hash structure than a single string, or string in the end to use the hash weigh again according to the actual needs
Hash basic operations
127.0.0.1:6379> hset books java "think in java"
(integer) 1
127.0.0.1:6379> hset books golang "concurrency in go"
(integer) 1
127.0.0.1:6379> hset books python "python cookbook"
(integer) 1
127.0.0.1:6379> hgetall books
1) "java"
2) "think in java"
3) "golang"
4) "concurrency in go"
5) "python"
6) "python cookbook"
127.0.0.1:6379> hlen books
(integer) 3
127.0.0.1:6379> hget books java
"think in java"
127.0.0.1:6379> hset books golang "learning go programming"
(integer) 0
127.0.0.1:6379> hget books golang
"learning go programming"
127.0.0.1:6379> hmset books java "effective java" python "learning python"
OK
Hash basic finishing operations
hset hashKey key value
If you store a hash string contains spaces, use quotation marks, if hset the same key value, it is considered as an update operationhgetall hashKey
Return all key value (key and value intervals occur) within hashhlen hashKey
Returns the length of hashhget hashKey key
Get value from the key corresponding to the hashKeyhmset hashKey key1 value1 key2 value2
Batch sethincrby hashKey key number
value is the number of counts may be
3, List (list)
- Description: linked list (doubly linked list), the equivalent of java in the LinkedList
- Characteristics: deletions fast, the time complexity is O (1), but the index positioning is very slow, to O (n), it is provided a certain period of operation of the API elements
- Applications: latest news, charts and other related timeline, used to do asynchronous queue
When the pop-up list of the last element, the data structure is deleted automatically, memory is recovered
List the basic operations
To the right into the left: queue
FIFO, often used for message queuing and asynchronous logic, ensuring the sequential access of elements
127.0.0.1:6379> rpush books python java golang
(integer) 3
127.0.0.1:6379> llen books
(integer) 3
127.0.0.1:6379> lpop books
"python"
127.0.0.1:6379> lpop books
"java"
127.0.0.1:6379> lpop books
"golang"
127.0.0.1:6379> lpop books
(nil)
Into the right side of the right: Stack
After last in the business and less common
127.0.0.1:6379> rpush books python java golang
(integer) 3
127.0.0.1:6379> rpop books
"golang"
127.0.0.1:6379> rpop books
"java"
127.0.0.1:6379> rpop books
"python"
127.0.0.1:6379> rpop books
(nil)
Slow operation
127.0.0.1:6379> rpush books python java golang
(integer) 3
127.0.0.1:6379> lindex books 1 #同java链表的get(index),需要对链表进行遍历
"java"
127.0.0.1:6379> lrange books 0 -1 #获取所有元素
1) "python"
2) "java"
3) "golang"
127.0.0.1:6379> ltrim books 1 -1 #保留区间内值
OK
127.0.0.1:6379> lrange books 0 -1
1) "java"
2) "golang"
127.0.0.1:6379> ltrim books 1 0 #清空列表,因为长度为负
OK
127.0.0.1:6379> llen books
(integer) 0
List basic finishing operations
rpush key value1 value2 value3
Storing a list from the right into thellen key
Back to list Lengthlpop key
From the left pops up a list of key values, and clear memoryrpop
Pop a value from the list on the right, and clear memorylindex key index
The obtained index key value in the list, O (n)lrange key key findex eindex
Get a list of values in accordance with the interval, returns a list, O (n)ltrim books findex eindex
According to the value of the interval reserved list
4, Set (collection)
- Description: Implement a hash table, the elements are not repeated, the disorder, the equivalent of java in hashSet.
- Characteristics: deletions of both search complexity O (1), provides for the collection of intersection, and, poor operation
- Applications: mutual friend, an independent Web site statistics ip, id winning an activity
Internal achieve the equivalent of a special dictionary, a dictionary of all the value is a value null.
Secretariat of the structures are automatically deleted when the last element in the collection is removed, the memory is recovered
Set the basic operation
127.0.0.1:6379> sadd books python
(integer) 1
127.0.0.1:6379> sadd books python
(integer) 0
127.0.0.1:6379> sadd books java golang
(integer) 2
127.0.0.1:6379> smembers books
1) "golang"
2) "java"
3) "python"
127.0.0.1:6379> sismember books java
(integer) 1
127.0.0.1:6379> sismember books rust
(integer) 0
127.0.0.1:6379> scard books
(integer) 3
127.0.0.1:6379> spop books
"python"
127.0.0.1:6379> scard books
(integer) 2
Set basic finishing operations
sadd key value
Storing a setsmembers books
Returns all values in the set, disorderlysismember key value
Query whether a value exists, exists returns 1, otherwise it returns 0scard key
Get set lengthspop key
Pop a value from the set of
5, zSet (ordered collection / ordered list)
- Description: Set in the value element adds a weighted score of elements, the elements in the set sorted by score. Java similar HashMap and the combination sortedSet
- Characteristics: inserting a set of data has been sorted
- Application: Leaderboard, storage student achievement, fan list, with the weight of the message queue
Internally called a "jump list" data structure
zset last value after being removed, the data structures are automatically deleted, memory is recovered
zSet basic operations
127.0.0.1:6379> zadd books 9.0 "think in java"
(integer) 1
127.0.0.1:6379> zadd books 8.9 "java concurrency"
(integer) 1
127.0.0.1:6379> zadd books 8.6 "java cookbook"
(integer) 1
127.0.0.1:6379> zrange books 0 -1
1) "java cookbook"
2) "java concurrency"
3) "think in java"
127.0.0.1:6379> zrevrange books 0 -1
1) "think in java"
2) "java concurrency"
3) "java cookbook"
127.0.0.1:6379> zcard books
(integer) 3
127.0.0.1:6379> zscore books "java concurrency"
"8.9000000000000004"
127.0.0.1:6379> zrank books "java concurrency"
(integer) 1
127.0.0.1:6379> zrangebyscore books 0 8.91
1) "java cookbook"
2) "java concurrency"
127.0.0.1:6379> zrangebyscore books -inf 8.91 withscores
1) "java cookbook"
2) "8.5999999999999996"
3) "java concurrency"
4) "8.9000000000000004"
127.0.0.1:6379> zrem books "java concurrency"
(integer) 1
127.0.0.1:6379> zrange books 0 -1
1) "java cookbook"
2) "think in java"
zSet basic finishing operations
zadd key score value
Storing a zset, passing scorezrange key 0 -1
Sort by score lists, parameter range for the position rangezrevrange key 0 -1
Press the score list in reverse order, the parameter range for the position rangezcard key
Get zset lengthzscore key value
Gets the value of the score, a double internal stores scorezrank key value
Where to obtain rank valuezrangebyscore key findex eindex
According to traverse the interval score zsetzrangebyscore key -inf index withscores
According to traverse the interval score zset, while the return score, -inf is negative infinityzrem key value
Delete the specified value
Today the problem is not resolved
- redis of rehash
- The difference redis and rehash the rehash of java
- Skip lists