Python operations Memcache, Redis, RabbitMQ

Source: Internet
Author: User
Tags memcached rabbitmq redis server

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.

Memcached Installation:

Memcached-d-M 10-u root-l 192.168.132.130-p 12000-c 256-p/tmp/memcached.pidparameter description:    -D is to start a    daemon-
     
      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 the port that sets the Memcache listener, Preferably more than 1024 ports-theC 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 file to save Memcache     
      

Use Python to manipulate memcached:

Python operation memcached need to install python-memcached module

Connection memcached:

Host    weight    1.1.1.1   1    1.1.1.2   2    1.1.1.3   1 then in-memory host list is:    host_list = ["  1.1.1.1"1.1.1.2"1.1.1.2"1.1.1.3",]      

If the user is to create a key-value pair in memory (for example: K1 = "V1"), then perform the steps:

    • Convert K1 into a number based on the algorithm
    • Calculate number and host list length to remainder, get a value n (0 <= N < list length)
    • Gets the host in the host list according to the value obtained in 2nd step, for example: Host_list[n]
    • Connect the host acquired in step 3rd, place k1 = "V1" In the server's memory

The code is implemented as follows:

MC = Memcache. Client (['1.1.1.1:12000', 1), ('1.1.1.2:12000 ',2), ('1.1.1.3:12000', 1)], debug =True) mc.set ('K1'v1')          

Basic Operations on Memcache:

Add a key value, if present the exception

Import Memcache mc = memcache. Client (['192.168.132.130:12000'], debug=True)# If KKKK is present in Memcache, the substitution succeeds, otherwise a mc.replace ( ' kkkk ','999')            

Set the key value pair, if key does not exist, then create if key exists, then modify, set one, Set_multi set multiple

Import Memcache mc = memcache. Client (['192.168.132.130:12000'], debug=True) mc.delete ('key0') mc.delete_multi ([ ' key1'key2'])             

Gets a key-value pair, get gets one, Get_multi gets multiple

Import Memcache mc = memcache. Client (['192.168.132.130:12000'], debug=True) val = mc.get ('key0') item_dict = Mc.get _multi (["key1"key2"key3"])        

Modifying key-value pairs

Append, modifies the value of the specified key, appending the content after the value
Prepend, modifies the value of the specified key to insert the contents before the value

Importmemcache mc = memcache. Client ([‘192.168.132.130:12000'], debug=True) # K1 = "V1" mc.append ('K1', 'after')# k1 = "V1after" mc.prepend ('K1', 'before')# k1 = "Beforev1after"     

Self-increment and self-reducing key-value pairs

INCR increment, add a value in memcached n (n default = 1)
DECR, reducing a value in memcached by n (n default = 1)

Importmemcache mc = memcache. Client ([‘192.168.132.130:12000'], debug=True) Mc.set (‘K1‘,‘777‘) MC.INCR (‘K1‘) # K1 = 778 MC.INCR ( k1) # K1 = 788 MC.DECR ( Span style= "color: #800000;" > ' 

Redis operations

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.

Basic use:

Set Foo bar OK redis>get foo "bar"

Python Operation Redis:

Import Redis r = Redis. Redis (host='192.168.132.130', port=6379) r.set ('foo'Bar)Print R.get ('foo')              

Redis connection pooling: 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.

ImportRedis pool = Redis. ConnectionPool (host=‘192.168.132.130) R = Redis. Redis (Connection_pool=pool) #  pipe = R.pipeline (transaction=false) pipe = R.pipeline (Transaction=true) R.set ( "name", Span style= "color: #800000;" > ' ) Pipe.execute ()      

Publications and subscriptions for Redis:

Publisher: Server

Subscribers: Dashboad and data processing

To publish a subscription instance:

Import Redishelper obj = redishelper () redis_sub =while true:msg=print msg    

Published by:

Import Redishelper obj = redishelper () obj.public ('Hello')    

Not to be continued!

Python operations Memcache, Redis, RabbitMQ

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.