redis database persistence

Persistence
redis data backup provides two ways, one is the RDB, the other is AOF, the following describes both backup strategy in detail:
 

 

 
Operation Python redis
1. redis-mounted Python:
PIP redis the install
2. Create a file such redis_test.py, then redis initializes an instance variable, and in turn redis ubuntu virtual machine. For example, a virtual machine ip address is 192.168.174.130. The following sample code:
# Import from redis package Redis class
from redis Import Redis
# redis initialize instance variables
xtredis = Redis (= Host '192.168.174.130', Port = 6379)
3. The operation of the string: name of the operation method of the redis using redis-cli with before, it is now used to make a number of simple description, the following sample code (receiving the above code):

# add a value in, and set an expiration time of 60 seconds, if not set, then forever It does not expire
xtredis.set ( 'username', 'xiaotuo', EX = 60)
# Gets a value
xtredis.get ( 'username')
# delete a value
xtredis.delete ( 'username')

4. list of operations: with string manipulation, the names of all methods with the use of redis-cli operation is the same:

# languages to this list to the left to add a Python
xtredis.lpush ( 'languages', 'Python'

xtredis.lpush ( 'languages', 'PHP')
# languages to this list to the left to add a JavaScript
xtredis.lpush ( 'languages', 'JavaScript')

# Get all the values in this list of languages
Print xtredis.lrange ( 'languages', 0, -1)
> [ 'JavaScript', 'PHP', 'Python']

5. The operation of the collection:

# team to add a set of element xiaotuo
xtredis.sadd ( 'team', 'xiaotuo')
# add an element to a collection of team datuo
xtredis.sadd (, 'datuo' 'team')
# add an element to a collection of team Slice
xtredis.sadd ( 'team', 'slice')

# Get all elements in the set
xtredis.smembers ( 'Team')
> [ 'datuo', 'xiaotuo', 'Slice'] # disordered

6. Operation hash (hash) is:

# hash to the website add baidu
xtredis.hset ( 'website', 'baidu', 'baidu.com')
# hash this website to add Google
xtredis.hset ( 'website', 'Google', 'google.com')

# Get all the hash values in the website
Print xtredis.hgetall ( 'website')
> { "baidu": "Baidu.com", "Google": "Google.com"}

7. The transaction (pipe) Operation: redis support transactional operations, that some operations only unified completed, it can be considered complete. Otherwise have failed, with the python redis operation is very simple, the following sample code:

# define a pipe instance
PIP = xtredis.pipeline ()
PIP = xtredis.pipeline ()
pip.set ( 'username', 'Xiaomei')
PIP. the SET ( 'School', 'Qinghua')
pip.execute ()

8. Affairs (pipeline) operation: redis support transactional operations, that some operations only unified completed, it can be considered complete. Otherwise, all failed, with python redis operation is very simple, the sample code as follows:  

#订阅
from redis import Redis
ps = xtredis.pubsub()
ps.subscribe('email')
while True:
for item in ps.listen():
if item['type'] == 'message':
data = item.get('data')
print(data.decode('utf-8'))
#发布
from redis import Redis
xtredis = Redis(host='192.168.254.41', port=6379)
xtredis.publish('email', '[email protected]')

 
 
Above it will show some common ways python-redis, and if you want to understand the other way, you can refer to python-redis source code (view source pycharm shortcuts Tip: hold the mouse cursor on the import of Redis Redis, then press ctrl + b to enter).

13. publish / subscribe operations:
• publish a message to a channel:
publish the Message Channel
[root @ zxw9 Redis-5.0.0] # Redis-cli
127.0.0.1:6379> PUBLISH 80 NiHao
(Integer) 1


• subscribe to a news channel:
the Subscribe Channel

127.0.0.1:6379> SUBSCRIBE 80
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "80"
3) (integer) 1


1) "message"
2) "80"
3) "nihao"

 

Guess you like

Origin www.cnblogs.com/itzhao/p/11280311.html