Use Python to operate Memcached

Source: Internet
Author: User

Use Python to operate Memcached
Memcached

Memcached is a high-performance distributed memory object Cache System for dynamic Web applications to reduce database load. It caches data and objects in the memory to reduce the number of reads to the database, thus improving the speed of dynamic and data library-driven websites. Memcached is based on a hashmap that stores key/value pairs. Its daemon is written in C, but the client can write it in any language and communicate with the daemon through memcached protocol.

Before using Memchched, install it first. The installation environment is a Linux Server:

1234567 wget http://memcached.org/downloads/memcached-1.4.29.tar. Gz download the latest source codetar -zxvf memcached-x.x.x.tar.gzcd memcached-x.x.x./configure && make && make test && sudo make installPS: dependent on libevent, which must be installed in advanceyum install libevent-develapt-get install libevent-dev

Start Memcached

1234567 memcached -d -m 10 -u root -l 0.0.0.0 -p 12000 -c 256 -P /tmp/memcached.pid<br>-D: Start a daemon. <br>-M: The amount of memory allocated to Memcache. The unit is MB. <br>-U: the user who runs Memcache <br>-L: IP address of the listener server <br>-P: Set the Memcache listening port, preferably1024The above port <br>-C: Maximum number of concurrent connections. The default value is1024, Set according to the server load <br>-P: Set the pid file for saving Memcache
1.2 Python operations on Memcached

Install API

  • Use python to operate Memcached using the Python-memcached Module
  • Download installation: https://pypi.python.org/pypi/python-memcached

Operation Method:

Set: input two parameters: name and value.

123456 import memcache  m = memcache.Client(['10.211.55.4:12000'], debug=True)  # If debug is set to true, the error message is displayed when a problem occurs during running.m.set("foo", "bar")ret = mc.get('foo')print(ret)

Add: add a key-value pair. If the key already exists, an exception occurs.

12 m.add('k1', 'v1')m.add('k1', 'v2') # If an existing key is added repeatedly, an error is returned.

Replace: Modify the value of a key. If the key does not exist, an exception occurs.

1 m.replace('kkkk','999')

Set and set_multi
Set: sets a key-value pair. If the key does not exist, it is created. If the key exists, it is modified.
Set_multi: set multiple key-value pairs. If the key does not exist, it is created. If the key exists, it is modified.

12 m.set('key0', 'jack') m.set_multi({'key1': 'val1', 'key2': 'val2'})

Delete and delete_multi
Delete: deletes a specified key-value pair.
Delete_multi: delete multiple key-value pairs.

12 m.delete('key0')m.delete_multi(['key1', 'key2'])

Get and get_multi
Get: get a key-Value Pair
Get_multi: get one more key-Value Pair

12 val = m.get('key0')item_dict = m.get_multi(["key1", "key2", "key3"])

Append and prepend
Append: Modify the value of the specified key and append the content after the original value.
Prepend: Modify the value of the specified key and insert content before the original value.

12345 # k1 = "v1"m.append('k1', 'after')# k1 = "v1after"m.prepend('k1', 'before')# k1 = "beforev1after"

Decr and incr
Incr: auto-increment. Add N to the value (N is 1 by default)
Decr: auto-subtraction, reducing the value by N (N is 1 by default)

123456789 m.set('k1', '777')m.incr('k1')# k1 = 778m.incr('k1', 10)# k1 = 788m.decr('k1')# k1 = 787m.decr('k1', 10)# k1 = 777

Gets and cas

When using the cache system to share data resources, the competition for data and dirty data will inevitably fail. For example:
Assume that the remaining number of items in the mall is stored in memcache, product_count = 900
User A refreshes the page and reads product_count = 900 from memcache.
User B refreshes the page and reads product_count = 900 from memcache.

Users A and B purchase products and modify the value of product_count:

After user A modifies product_count = 899
After user B modifies product_count = 899
As a result, the data in the cache is no longer correct. In this case, product_count should be 898.
If you use python set and get to perform the preceding operations, the program will be shown in the preceding figure!

To avoid this situation, use gets and cas, such:

123456789 #!/usr/bin/env python# -*- coding:utf-8 -*-import memcachemc = memcache.Client(['10.211.55.4:12000'], debug=True, cache_cas=True)  v = mc.gets('product_count')# ...# If someone modifies product_count after gets and Before cas, the following settings will fail to be executed and an exception will be thrown out to avoid the generation of abnormal data.mc.cas('product_count', "899")

Essentially, each time gets is executed, an auto-increment number is obtained from memcache. When the value of gets is modified through cas, the obtained auto-increment values are compared with the auto-increment values in memcache. If they are equal, they can be submitted. If they are not equal, they are executed between gets and cas, if another person executes gets, the modification is not allowed.

Cluster operations

The python-memcached module supports cluster operations. The principle is to maintain a host list in the memory, and the weight of the host in the cluster is proportional to the number of repeated occurrences of the host in the list.

Host weight
1.1.1.1 1
1.1.1.2 2
1.1.1.3 1

In the memory, the host list is: host_list = ["1.1.1.1", "1.1.1.2", "1.1.1.2", "1.1.1.3",]
If you want to create a key-Value Pair (for example, k1 = "v1") in the memory, perform the following steps:

  1. Convert k1 to a number based on the algorithm.
  2. Calculate the remainder of the number and host list length to obtain a value of N (0 <= N <list length)
  3. In the host list, obtain the host based on the value obtained in step 1 as the index, for example, host_list [N].
  4. Connect to the host obtained in step 1 and place k1 = "v1" in the memory of the server.

The Code is as follows:

12 m = memcache.Client([('1.1.1.1:12000', 1), ('1.1.1.2:12000', 2), ('1.1.1.3:12000', 1)], debug=True)m.set('k1', 'v1')

Install and configure Memcached source code in CentOS 6.6

Memcached installation and startup script

Performance problems of using Memcached in PHP

Install Memcached in Ubuntu and its command explanation

Install and apply Memcached

Use Nginx + Memcached's small image storage solution

Getting started with Memcached

For details about Memcached, click here
Memcached: click here

This article permanently updates the link address:

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.