Python Operation Memcache

Source: Internet
Author: User

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 hash map that stores key/value. Its daemon (daemon) is written in C, but the client can write in any language and communicate with the daemon through the memcached protocol.


Installation

Cd/usr/local/src

wget http://memcached.org/latest

TAR-ZXVF memcached-1.x.x.tar.gz

CD memcached-1.x.x

./configure && make && make test && make install


Start

Memcached-d-M 10-u root-l 127.0.0.1-p 12000-c 256-p/export/servers/memcache/logs/memcache.pid

-P Specify port number (default 11211)

-m specifies the maximum amount of memory to use (default 64MB)

-T thread count (default 4)

-L connected IP address, default is native

-D starts with the daemon process

-c Maximum number of simultaneous connections, default is 1024

-P Develop memecache PID file

-H Printing Help information

PS-EF Check if Memcache is started, check port number

Python Operation Memcache Cluster

The PYTHON-MEMCACHD module natively supports cluster operations by maintaining a list of hosts in memory, and the weight values of the hosts in the cluster are proportional to the number of occurrences of the host in the list.

Host weights

1.1.1.1 1

1.1.1.2 2

1.1.1.3 1

Then in memory the host list is: host_list=[' 1.1.1.1 ', ' 1.1.1.2 ', ' 1.1.1.2 ', ' 1.1.1.3 ',]


Example:

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 ')


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

The K1 is converted into a number according to the algorithm;

The number and host list length are calculated as remainder, and a value of n (0 <= N < list length) is obtained;

In the host list, the value obtained from the 2nd step is the index to get the host, for example: Host_list[n];

The connection will be taken in the 3rd step of the host, placing K1 = ' V1 ' in the server's memory.

In the above example, we call the Memcache module to achieve access to the memcached data, debug=true indicates that there is an error in the run, the error message is displayed, after the line to remove the parameter.


Python Operation Memcache

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.