Original article in this: https://www.cnblogs.com/cnkai/p/7642787.html
There is a personal modification and correction
Connecting to a database
Strictredis
From Redis import Strictredis
#! / usr / bin / env python
# coding: utf-8
from redis import StrictRedis
from redis import ConnectionPool
# Link to database using default method 1
# redis = StrictRedis (host = ‘localhost’, port = 6397, db = 0)
# Link to database using default method 2
# pool = ConnectionPool (host = ‘localhost’, port = 6397, db = 0)
# redis = StrictRedis (ConnectionPool)
# Link to database using url method 1
# redis = StrictRedis.from_url (url = ‘redis: // localhost: 6379/1‘)
# Use url method 2 to connect to the database
pool = ConnectionPool.from_url (‘redis: // @ localhost: 6379/1‘)
redis = StrictRedis (connection_pool = pool)
"" "
Constructing a url to connect to the database has the following three modes:
redis: // [: password] @host: port / db # TCP connection
rediss: // [: password] @host: port / db # Redis TCP + SSL connection
unix: // [: password] @ / path / to / socket.sock? db = db # Redis Unix Socket connection
"" "
Redis inside the specific operation
String manipulation
Method |
function |
Example |
Example Results |
Set (name, value) |
Assign name to value |
Redis.set (' name ', ' Bob ') |
True |
Get (name) |
Returns the value of a string with key name in the database |
Redis.get (' name ') |
B ' Bob ' |
Getset (name, value) |
Assigns a value to a string of key name in the database and returns the last value |
Redis.getset (' name ', ' Mike ') |
B ' Bob ' |
Mget (keys, *args) |
Returns the value corresponding to multiple keys |
Redis.mget ([' Name ', ' nickname ']) |
[B ' Mike ', B ' miker '] |
SETNX (name, value) |
Set value if key does not exist |
Redis.setnx (' newname ', ' James ') |
First run true, second false |
Setex (name, time, value) |
Sets a value that can correspond to a string type and specifies the validity period for this key value |
Redis.setex (' name ', 1, ' James ') |
True |
SETRANGE (name, offset, value) |
Sets the substring of the value of the specified key |
Redis.set (' name ', ' Hello ') redis.setrange (' name ', 6, ' world ') |
11, modified string length |
Mset (mapping) |
Batch Assignment |
Redis.mset ({' name1 ': ' Durant ', ' name2 ': ' James '}) |
True |
MSETNX (mapping) |
Bulk assignment If key is not present |
Redis.msetnx ({' Name3 ': ' Smith ', ' name4 ': ' Curry '}) |
True |
INCR (name, amount=1) |
Key is the value increment operation for name, the default 1,key does not exist and is created and set to amount |
REDIS.INCR (' age ', 1) |
1, which is the modified value |
DECR (name, amount=1) |
Key is the value of the value impairment operation, and the default 1,key does not exist and is created and set to-amount |
REDIS.DECR (' age ', 1) |
-1, which is the modified value |
Append (key, value) |
Key appends value for string of name |
Redis.append (' nickname ', ' OK ') |
13, which is the modified string length |
SUBSTR (name, start, end=-1) |
Returns a substring of the value of string with key name |
Redis.substr (' name ', 1, 4) |
B ' Ello ' |
GetRange (key, start, end) |
Gets the value of the key from start to end of the substring |
Redis.getrange (' name ', 1, 4) |
B ' Ello ' |
Key operation
Method |
function |
Example |
Example Results |
Exists (name) |
Determine if a key exists |
Redis.exists (' name ') |
True |
Delete (name) |
Delete a key |
Redis.delete (' name ') |
1 |
Type (name) |
Determine the key type |
Redis.type (' name ') |
B ' String ' |
Keys (pattern) |
Get all rules-compliant keys |
Redis.keys (' n ') |
[b ' name '] |
Randomkey () |
Get a random key |
Randomkey () |
B ' Name ' |
Rename (src, DST) |
Rename the key |
Redis.rename (' name ', ' nickname ') |
True |
Dbsize () |
Gets the number of keys in the current database |
Dbsize () |
100 |
Expire (name, time) |
Set the expiration time of the key, in seconds |
Redis.expire (' name ', 2) |
True |
TTL (name) |
Gets the expiration time of the key, in seconds, 1 for a permanent period |
Redis.ttl (' name ') |
-1 |
Move (name, DB) |
Move key to a different database |
Move (' name ', 2) |
True |
FLUSHDB () |
Delete all keys in the currently selected database |
FLUSHDB () |
True |
Flushall () |
Remove all keys from all databases |
Flushall () |
True |
List operation
Method |
function |
Example |
Example Results |
Rpush (name, *values) |
Adds an element of value to the list end of the name of key, which can pass multiple |
Redis.rpush (' list ', 1, 2, 3) |
3,list size |
Lpush (name, *values) |
Add an element of value to the list header of the key name, and you can pass multiple |
Redis.lpush (' list ', 0) |
4,list size |
Llen (name) |
Returns the length of the list with key name |
Redis.llen (' list ') |
4 |
Lrange (name, start, end) |
Returns the element between start and end in the list of key name |
Redis.lrange (' list ', 1, 3) |
[B ' 3 ', B ' 2 ', B ' 1 '] |
LTrim (name, start, end) |
Intercepts the list of key name, preserving the content indexed as start to end |
LTrim (' list ', 1, 3) |
True |
Lindex (name, index) |
Returns the element of the index position in the list of key name |
Redis.lindex (' list ', 1) |
B ' 2 ' |
LSet (name, index, value) |
Assign a value to the element in the index position in the list of key name, and the error is out of bounds |
Redis.lset (' list ', 1, 5) |
True |
Lrem (name, count, value) |
Deletes the value in the list of Count keys as an element |
Redis.lrem (' list ', 2, 3) |
1, that is, the number of deletions |
Lpop (name) |
Returns and removes the first element in the list of key name |
Redis.lpop (' list ') |
B ' 5 ' |
Rpop (name) |
Returns and removes the trailing element in the list of key name |
Redis.rpop (' list ') |
B ' 2 ' |
Blpop (keys, timeout=0) |
Returns and deletes the first element in the list named in Keys, and if the list is empty, it will block waiting |
Redis.blpop (' list ') |
[B ' 5 '] |
Brpop (keys, timeout=0) |
Returns and deletes the tail element in the list of key name, and if the list is empty, it will block waiting |
Redis.brpop (' list ') |
[B ' 2 '] |
Rpoplpush (SRC, DST) |
Returns and deletes the tail element of the list named SRC, and adds the element to the head of the list named DST |
Redis.rpoplpush (' list ', ' List2 ') |
B ' 2 ' |
Set operation
Method |
function |
Example |
Example Results |
Sadd (name, *values) |
Add an element to a set with key name |
Redis.sadd (' tags ', ' book ', ' Tea ', ' Coffee ') |
3, that is, the number of data inserted |
Srem (name, *values) |
Remove an element from a set of key name |
Redis.srem (' tags ', ' book ') |
1, that is, the number of deleted data |
Spop (name) |
Randomly returns and deletes an element in the set of key name |
Redis.spop (' tags ') |
B ' Tea ' |
Smove (SRC, DST, value) |
Removes the element from the SRC corresponding set and adds it to the corresponding set in DST |
Redis.smove (' tags ', ' tags2 ', ' Coffee ') |
True |
SCard (name) |
Returns the number of elements of a set with key name |
Redis.scard (' tags ') |
3 |
Sismember (name, value) |
Test whether the member is a set element of key name |
Redis.sismember (' tags ', ' book ') |
True |
Sinter (keys, *args) |
Returns the intersection of the set for all given keys |
Redis.sinter ([' Tags ', ' tags2 ']) |
{b ' Coffee '} |
Sinterstore (dest, Keys, *args) |
To find the intersection and save the intersection to the collection of dest |
Redis.sinterstore (' Inttag ', [' tags ', ' tags2 ']) |
1 |
Sunion (keys, *args) |
Returns the set's set of all given keys |
Redis.sunion ([' Tags ', ' tags2 ']) |
{b ' Coffee ', b ' book ', B ' Pen '} |
Sunionstore (dest, Keys, *args) |
Gather and save the set to the Dest collection |
Redis.sunionstore (' Inttag ', [' tags ', ' tags2 ']) |
3 |
Sdiff (keys, *args) |
Returns the set difference for all given keys |
Redis.sdiff ([' Tags ', ' tags2 ']) |
{b ' book ', B ' Pen '} |
Sdiffstore (dest, Keys, *args) |
Finding the difference set and saving the difference set to the collection of dest |
Redis.sdiffstore (' Inttag ', [' tags ', ' tags2 ']) |
3 |
Smembers (name) |
Returns all elements of a set with key name |
Redis.smembers (' tags ') |
{b ' Pen ', b ' book ', B ' Coffee '} |
Srandmember (name) |
Randomly returns an element of the set of key name, but does not delete the element |
Redis.srandmember (' tags ') |
|
Sorted Set operation
Method |
function |
Example |
Example Results |
Zadd (name, args, *kwargs) |
Add the element Member,score to the zset of the key name for sorting. If the element exists, its order is updated |
Redis.zadd (' grade ', +, ' Bob ', 98, ' Mike ') |
2, that is, the number of elements added |
Zrem (name, *values) |
Remove the element from the Zset key for name |
Redis.zrem (' Grade ', ' Mike ') |
1, that is, the number of deleted elements |
Zincrby (name, value, amount=1) |
If the element value already exists in the Zset of key name, the score of the element is incremented amount, otherwise the element is added to the collection, and its score value is amount |
Redis.zincrby (' Grade ', ' Bob ',-2) |
98.0, which is the modified value |
Zrank (name, value) |
Returns the rank of the element in the Zset key for name (sorted by score from small to large) that is subscript |
Redis.zrank (' Grade ', ' Amy ') |
1 |
Zrevrank (name, value) |
Returns the inverse rank of the element in the Zset of key name (sorted by score from large to small) that is subscript |
Redis.zrevrank (' Grade ', ' Amy ') |
2 |
Zrevrange (name, start, end, Withscores=false) |
Returns all elements of index from start to end in the Zset of key name (sorted by score from large to small) |
Redis.zrevrange (' Grade ', 0, 3) |
[B ' Bob ', b ' Mike ', b ' Amy ', B ' James '] |
Zrangebyscore (name, Min, Max, Start=none, Num=none, Withscores=false) |
Returns the element score a given interval in the Zset key for name |
Redis.zrangebyscore (' Grade ', 80, 95) |
[B ' Amy ', B ' James '] |
Zcount (name, Min, max) |
Returns the number of score in a given interval in the zset of the key name |
Redis.zcount (' Grade ', 80, 95) |
2 |
Zcard (name) |
Returns the number of elements of the Zset key for name |
Redis.zcard (' Grade ') |
3 |
Zremrangebyrank (name, Min, max) |
Removes the element that is ranked in the given interval in the Zset key for name |
Redis.zremrangebyrank (' Grade ', 0, 0) |
1, that is, the number of deleted elements |
Zremrangebyscore (name, Min, max) |
Delete the element in the given interval in the Zset of key name score |
Redis.zremrangebyscore (' Grade ', 80, 90) |
1, that is, the number of deleted elements |
Hash operation
Method |
function |
Example |
Example Results |
Hset (name, key, value) |
Add a map to the hash of key name |
Hset (' Price ', ' cake ', 5) |
1, that is, the number of mappings added |
HSETNX (name, key, value) |
Add a mapping to the hash of key name if the mapping key name does not exist |
Hsetnx (' Price ', ' book ', 6) |
1, that is, the number of mappings added |
Hget (name, key) |
Returns the value of field corresponding to the hash of key name |
Redis.hget (' Price ', ' cake ') |
5 |
Hmget (name, keys, *args) |
Returns the value of each key in the hash of key name |
Redis.hmget (' Price ', [' Apple ', ' orange ']) |
[B ' 3 ', B ' 7 '] |
Hmset (name, mapping) |
Bulk add mappings to a hash of key name |
Redis.hmset (' price ', {' banana ': 2, ' pear ': 6}) |
True |
Hincrby (name, key, Amount=1) |
Adds amount to the value mapped in the hash of key name |
Redis.hincrby (' Price ', ' Apple ', 3) |
6, the modified value |
Hexists (name, key) |
Key is a map with key name Namehash in the |
Redis.hexists (' price ', ' banana ') |
True |
Hdel (name, *keys) |
Key to delete the map with key named Key in Namehash |
Redis.hdel (' price ', ' banana ') |
True |
Hlen (name) |
Gets the number of mappings from a hash of key name |
Redis.hlen (' price ') |
6 |
Hkeys (name) |
Get all the mapping key names from the hash of key name |
Redis.hkeys (' price ') |
[B ' Cake ', b ' book ', B ' banana ', b ' pear '] |
Hvals (name) |
Get all mapping key values from a hash of key name |
Redis.hvals (' price ') |
[B ' 5 ', B ' 6 ', B ' 2 ', B ' 6 '] |
Hgetall (name) |
Gets all mapped key-value pairs from a hash of key name |
Redis.hgetall (' price ') |
{b ' cake ': B ' 5 ', b ' book ': B ' 6 ', B ' orange ': B ' 7 ', B ' pear ': B ' 6 '} |
Redisdump & Redis-load Data export and data import
- Redis-load
Import data into the database
- Redis-dump Export the data (the Redis-dump tool is written in Ruby.) So after the system is installed Ruby, use Ruby's gem install Redis-dump to export the database. Redis-dump-u: [Email protected]:6379 >test.json
Redis-load operations are as follows
# Import data using Unix pip
# cat test.json | redis-load -u: [email protected] 127.0.0.1:6379
# Or so use Unix redirection
# redis-load -u: [email protected]: 6379 <test.json
Redis-dump operations are as follows
Install redis-dump [[email protected] ~] yum install ruby rubygems ruby-devel // Install rubygems and related packages (redhat series systems, or apt for debian, etc.)
[[email protected] ~] gem sources -a http://gems.ruby-china.org/ // source, ruby china joined, outside sources cannot be accessed
http://gems.ruby-china.org/ added to sources
[[email protected] ~] gem install redis-dump -V
redis-dump export data
[[email protected] ~] telnet 127.0.0.1 6379
Trying 127.0.0.1 ...
Connected to 127.0.0.1.
Escape character is ‘^]‘.
set test 11 // Set a value
+ OK
get test // take value
$ 2
11
[[email protected] ~] redis-dump -u: passwd@127.0.0.1: 6379> test.json
This will export the contents of the database to Test.json.
Redis python operation python operation Redis Database