Memcached
Memcached is a high-performance distributed memory object caching system for dynamic Web applications to mitigate database load. It improves the speed of dynamic, database-driven Web sites by caching data and objects in memory to reduce the number of times a database is read. Memcached is based on a hashmap that stores key/value pairs. Its daemon (daemon) is written in C, but the client can write in any language and communicate with the daemon through the memcached protocol.
-d-m -u root-l 192.168.1.100-p 45555-c 256-p/tmp/memcached.pid parameter description: -D is to start a daemon -< C6>m is the amount of memory allocated to Memcache, in megabytes-u is the user running Memcache -L is the server IP address of the listener -p is to set memcache listening port, preferably more than 1024 port -c option is the maximum number of concurrent connections running, the default is 1024, according to the load on your server to set -p is to set the PID text for saving memcache
1 #server-side installation memcached, local installation of corresponding API module2 #calling a module in Python3 Importmemcache4 #establish a link corresponding to the IP port5MC = Memcache. Client (['192.168.1.100:45555'], debug=True)6 #修改值, there is a modification, there is no add7Mc.set ("Foo","Bar")8 #get value by key9ret = Mc.get ('Foo')Ten Printret One>>>bar
1 Importmemcache2 3 #support multiple clusters to get the value from the corresponding cluster according to the weight4MC = Memcache. Client ([('1.1.1.1:12000', 1), ('1.1.1.2:12000', 2), ('1.1.1.3:12000', 1)], debug=True)5 6 #modified values are modified, no additions exist7Mc.set ('K1','v1')8 9 #Add a recordTenMc.add ('K2','v2') OneMc.add ('K2','v2')#Error A - If there is kkkk in the memcache, the substitution succeeds, otherwise a - the #Replacement value If there is a modification, there is a report exception -Mc.replace ('KKKK','999') - - #set multiple key-value pairs +Mc.set_multi ({'K3':'v3','K4':'V4'}) - + #Delete Value AMc.delete ('K1') at - #Bulk Delete -Mc.delete_multi (['K1','K2']) - - #get values based on key -val = Mc.get ('K1') in - #Bulk Fetch Values toItem_dict = Mc.get_multi (["K1","K2","K3"]) + - #Append value after value based on key theMc.append ('K1',' After') * #K1 = "V1after" $ Panax Notoginseng #Append value in front of value according to key -Mc.prepend ('K1','before') the #K1 = "Beforev1after" + A #incr, increment the value of value from 1 by key theMC.INCR ('K1') + #DECR, 1 of value is reduced by key -MC.DECR ('K1')
#to prevent data inconsistency when multiple users simultaneously modify commit dataMC = Memcache. Client (['192.168.1.100:45555'],debug=true,cache_cas=True) v= Mc.gets ('Product_count')Print(v) mc.cas ('Product_count','111')#Error#get and CAs change is worth the errorMc.cas ('Product_count','889')
Redis
Redis is a key-value storage system. Similar to memcached, it supports storing more value types, including string (string), list (linked list), set (set), Zset (sorted set-ordered collection), and hash (hash type). These data types support Push/pop, Add/remove, and intersection-set and difference sets, and richer operations, and these operations are atomic. Based on this, Redis supports sorting in a variety of different ways. As with memcached, data is cached in memory to ensure efficiency. The difference is that Redis periodically writes the updated data to disk or writes the modified operation to the appended record file, and Master-slave (Master-Slave) synchronization is implemented on this basis.
Comparison of Redis and Memcache
1 Redis not only supports simple k/v type of data, but also provides storage of data structures such as List,set,hash, memcached only supports one data type.
2 Redis supports backup of data, that is, Master-slave mode of data backup.
3 Redis supports data persistence, which keeps the in-memory data on disk and can be loaded again when it is restarted.
Start the service side
src
/
redis
-
serverStart the client
src/redis-CLI
1 #calling the Redis module2 ImportRedis3 #link Redis service side4R = Redis. Redis (host='192.168.1.100', port=41111)5 #Modifying Values6R.set ('Foo','Bar')7 #Get Value8 PrintR.get ('Foo')9>>>'Bar'Ten One #Redis-py uses connection pool to manage all connections to a Redis server, avoiding the overhead of each establishment and release of the connection. By default, each Redis instance maintains its own pool of connections. You can create a connection pool directly, and then as a parameter Redis, you can implement multiple Redis instances to share a single connection pool. A ImportRedis -Pool = Redis. ConnectionPool (host='192.168.1.100', port=41111) -R = Redis. Redis (connection_pool=pool) theR.set ('Foo','Bar') - PrintR.get ('Foo') - #set (name, value, Ex=none, Px=none, Nx=false, Xx=false) - #set the value in Redis, default, not present, create, modify if present + #Parameters: - #EX, expiry time (seconds) + #px, Expiration Time (ms) A #NX, if set to true, the current set operation executes only if name does not exist at #xx, if set to true, the current set operation is performed only if name exists - - #mset Batch Setting values -Mset (k1 ='v1', K2 ='v2') -Mget ({'K1':'v1','K2':'v2'}) - in #Get (name) get value -Get'K1') to + #mget (Keys,args) bulk acquisition -Mget'K1','K2') the * #Getset set a new value and get the original value $ #GetRange (key,start,end) Get sub-sequencePanax NotoginsengSet'K1','Liguangxu') -GetRange'K1',.)#IG the #SETRANGE (Name,offset,value) modifies the string contents, starting at the specified position + #Append (key,value) appends content to the value corresponding to Redis name AAppend'K1','AAA')#' liguangxuaaa '
day11-python-Operation Cache