Python installation and use of redis, python installation of redis
This article describes how to install and use redis in python. We will share this with you for your reference. The details are as follows:
1. Installation
Okay, I admit that I will only install it in the simplest way:
sudo apt-get install redis-server
Python support package: (In fact, only one file can be used)
sudo apt-get install python-redis
2. Configuration
Configure it. The default configuration file is: "/etc/redis. conf"
Bind ip:
"bind 127.0.0.1″ -> "bind 10.0.1.7″
Changing Disk Synchronization to non-synchronous or synchronization per second is too slow:
"appendfsync always" -> "appendfsync no"
Check whether the background execution is Enabled:
"Daemonize yes"
Or, for example:
Connection timeout: "timeout 300 ″
Running level: "loglevel notice" (I personally think this is quite good by default. If there is no major exception, do not change it to debug)
3. Use
#! /Usr/bin/env python # coding = utf-8import redisprint redis. _ file __# connection. You can select different databases r = redis. redis (host = '10. 0.1.7', port = 6379, db = 1) # ----------------------------------------- # view info = r.info () for key in info: print "% s: % s" % (key, info [key]) # query database size print '\ ndbsize: % s' % r. dbsize () # view connection print "ping % s" % r. ping () # select database # r. select (2) # Move data to 2 databases # r. move ('A', 2) # others # r. save ('A') # store data # r. lastsave ('A') # obtain the last one Time saved # r. flush () # refresh # r. shutdown () # Shut down all clients, stop all services, and exit the server # ---------------------------------------- # It has four types: string (key, value), list (sequence), set (set), zset (ordered set, with an additional ordered attribute) # do not know which type you use? # Print r. get_type ('A') # tell you # --------------------------------------------- # string operation print '-' * 20 # data plug r ['c1 '] = 'bar' # or r. set ('c2 ', 'bar') # Here there is a getset attribute. If it is True, you can print 'getset:' at the same time when saving new data :', r. getset ('c2 ', 'JJ') # If you want to set an increasing integer, add 1: print 'incr: ', r. incr ('A') # If you want to set a descending integer please: print 'decr: ', r. decr ('A') # retrieve data print 'r ['']: ', r ['c1'] # or print 'get:', r. get ('A') # or get a batch of print 'mget: ', r. mget ('c1 ',' C2 ') # or get a batch of their names (keys) at the same time, which is exactly like you don't want to lose all the print 'keys:', r. keys ('C * ') # or you just want to randomly get one: print 'randomkey:', r. randomkey () # check whether there are 1 or 0 print 'existes: ', r. exists ('A') # data deletion 1 is successful. 0 and None are not. print 'delete: ', r. delete ('cc') # By The Way, print 'delete: ', r. delete ('c1 ', 'c2') # Other r. rename ('A', 'c3') # er. rename r. expire ('c3', 10) # Let the data expire after 10 seconds. Honestly, I don't quite understand what it means. r. ttl ('c3') # returns if the remaining expiration time does not exist-1 # ------------------------------ # sequence (list) Operation print '-' * 20 # It is accessible at both ends # Plug in r. push ('B', 'gg ') r. push ('B', 'hh') # whether the head attribute control is from the other end r. push ('B', 'ee ', head = True) # print 'list len:', r. llen ('B') # list a batch of outgoing print 'list lrange: ', r. lrange ('B', start = 0, end =-1) # obtain a print 'list index 0: ', r. lindex ('B', 0) # trim list # If start is greater than end, print 'list ltrim: ', r. ltrim ('B', start = 0, end = 3) # Only four digits from 0 to 3 # sorting # This is a big project # ---------------------------- # set) operation # Plug data r. sadd ('s ', 'A') # determine the length of a set and the value does not exist as 0r. scard ('s) # determine whether an object in the set exists in r. sismember ('s', 'A') # calculates the intersection r. sadd ('s2', 'A') r. sinter ('s1', 's2') # Calculate the intersection and assign the result to r. sinterstore ('s3', 's1', 's2') # view a set object r. smembers ('s3') # Calculate the Union set r. sunion ('s1', 's2') # I think you have guessed it. # Calculate the Union set and return the result to r. sunionstore ('ss', 's1', 's2', 's3') # evaluate the difference # number r in S1. sdiff ('s1', 's2', 's3') r. sdiffstore ('s4 ', 's1', 's2') # You know this # obtain a random number r. srandmember ('s1 ')#---------------------------- --------- # Zset ordered set # 'zadd', 'zcard', 'zencr', 'zrange', 'zrangebyscore ', 'zrem', and 'zscore '# corresponding to # Add, respectively, quantity, auto-increment 1, fetch data, retrieve data according to the points (range), delete, take points # I am relying on you to play me redis! # In my experiment today, I tried to insert a zset data: r1.zset (u 'www .liyi99.com ', 'liwu', 3) # successful insertion # I continue to insert r1.zset (u'www. liyi99, com ', U' \ u9001 \ u793c', 5) # error: # UnicodeDecodeError: 'ascii 'codec can't decode byte 0xe4 in position 0: ordinal not in range (128) # This insert is the unicode encoding of the Chinese word of the gift # Why does it fail, this data is obtained from redis and inserted without any modification. # The data types returned and accepted by redis are unicode encoded. # Well, try inserting again # insert r1.zset ('www .liyi99.com ', U' \ u9001 \ u793c', 5) # succeeded # insert r1.zset ('www .liyi99.com', 'Gift ', 5) # still succeeded, followed by redis. py 1024 rows return self. send_command ('zadd % s \ r \ n % s \ r \ n' % (key, score, len (member), member )) # Oh, the conversion of all evil codes! # However, no matter what type of data is the first one