The memcached of Python3

Source: Internet
Author: User
Tags cas value store

1, Memcached Introduction

Memcached is a free and open source, high performance, distributed memory object caching system.

Memcached is a software developed by Brad Fitzpatric, a Danga interactive company in LiveJournal. It has become an important factor in improving Web application extensibility in many services such as Mixi, Hatena, Facebook, Vox, and LiveJournal.

Memcached is a memory-based Key-value store used to store arbitrary data (strings, objects) of small chunks. This data can be a result of database calls, API calls, or page rendering.

Memcached is simple and powerful. Its simple design facilitates rapid development, reduces the difficulty of development, and solves many problems of large data volume caching. Its API is compatible with most popular development languages.

Essentially, it is a concise key-value storage system.

The general purpose is to reduce the number of database accesses by caching database query results to improve the speed and scalability of dynamic Web applications.

Memcached Distributed Cache Server features: Simple protocol, libevent-based event processing, built-in memory storage, memcached non-communication distributed

2, memcached installation and basic use

Memcached supports many platforms: Linux, FreeBSD, Solaris, Mac OS, or Windows.

Linux system installation memcached first to install the Libevent library:

sudo  Install libevent libevent-deve          automatic Download installation (ubuntu/Debian)yuminstall libevent Libevent-deve                   Automatic Download installation (Redhat/fedora/centos)

Install memcached:

sudo Install memcached     #ubuntu/debianyuminstall memcached              #redhat/ fedora/centosportmaster databases/memcached     #freeBSD

Run the memcached command:

$/usr/local/memcached/bin/memcached-h    #获得帮助

If you use the auto-install memcached command located in/usr/local/bin/memcached.

Startup options:

-D: Start a daemon

-M: The amount of memory allocated to memcached, in megabytes

-u: User running memcached

-L: Listens for the server IP address, can be by more than one address

-P: Set memcached listening port, preferably more than 1024

-C: Maximum number of concurrent links running, default 1024

-P: Set the PID file to save memcached

[Email protected] bin]# memcached-d-M1024x768-U nobody-l192.168.146.129-P11211-C2048-p/tmp/memcached.pid[[email protected] bin]# netstat-lntup|grep 11211TCP0      0 192.168.146.129:11211       0.0.0.0:* LISTEN42059/memcached UDP0      0 192.168.146.129:11211       0.0.0.0:*42059/memcached

3. Python Operation memcached

Importmemcache#link memcached server, specify IP and port, debug indicate error messageMC = Memcache. Client (['192.168.146.129:11211'],debug=True)#set Key-value pairsMc.set ('Foo','Pythontomemcache')#View key ValuesPrint(Mc.get ('Foo'))

Add: Adds a key value pair, if there is a key, repeat the add exception

Mc.add ('K1','v1')

Replace: Modifies the value of a key, and if key does not exist, the exception

Mc.replace ('K2','v2')

Set: Sets a key-value pair that is created if key does not exist, and modifies if key exists

Mc.set ('k3','v3')

Set_multi: Set multiple key-value pairs, create if key does not exist, modify if key exists

Mc.set_multi ({'key1':'value1','key2 ':'value2'})

Delete: Deletes a specified key-value pair in memcached

Mc.delete ('key1')

Delete_multi: Delete specified multiple key-value pairs in memcached

Mc.delete_multi (['key1','key2','key3 '])

Get: Gets a key-value pair

Get_multi: Get multiple key-value pairs

Mc.get ('key3') mc.get_multi (['key1', ' Key2 '])

Append: Modifies the value of the specified key to append content after the value

Prepend: Modifies the value of the specified key to insert the contents before the value

Mc.append (' k1','after') mc.prepend (' K2 ','before')

INCR: Self-increment, increase a value in memcached by n (default = 1)

DECR: Auto-subtract, reduce a score in memcached by n (default = 1)

Mc.set ('K1', ten) mc.incr ('K1', 2)  Print(mc.get ('K1')) mc.decr ('K1') , 3)print(mc.get ('K1'))

The gets:gets command gets the value (data value) that is stored with the CAS token and returns null if the key does not exist

CAS: Performs a check-and-set operation that can write values only after the current client has the last value and the value corresponding to the key is not modified by another client.

Mc.cas ('key5','999')print(Mc.gets (  'key5'))

------------------------------------------------------------

The memcached of Python3

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.