Nosql-memcached related

Source: Internet
Author: User
Tags cas memcached

First, memcached

1.1. 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

1.2. Install the Client

Pip Install python-memcached

1.3. Start Memcache

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 Simultaneous connection number, default is 1024-p to develop Memecache PID file

-H Printing Help information

Second, memcached module

2.1, PYTHON-MEMCACHD module natively support cluster operation, the principle is to maintain a host list in memory, and the weight of the host in the cluster and the host in the list of repeated occurrences of the number is proportional.

Host weights
1.1.1.1 1
1.1.1.2 2
1.1.1.3 1

then the list of hosts in memory is: host_list=[' 1.1.1.1 ', ' 1.1.1.2 ', ' 1.1.1.2 ', ' 1.1.1.3 ',]

2.2, see an example, Python operation memcache Cluster

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

2.3, Memcache common operation:

    • Storage command: Set/add/replace/append/prepend/cas
    • Get command: Get/gets
    • Other commands: Delete/stats.

①, Add Method:

Import Memcache

MC = Memcache. Client ([' 192.168.48.128:11211 '], debug=true)
# Mc.set ("foo", "Bar")
# ret = mc.get ("foo")
Mc.add (' K1 ', ' v1 ')
Mc.add (' K1 ', ' v1 ')
# Print (ret)

②, replace method:

Replace modifies the value of a key, or the exception if key does not exist.

Import Memcache

MC = Memcache. Client ([' 192.168.48.128:11211 '], debug=true)
# Mc.set ("foo", "Bar")
# ret = mc.get ("foo")
Mc.add (' K1 ', ' v1 ')
# mc.add (' K1 ', ' v1 ')
# Print (ret)
Mc.replace ("K1", "666")
Print (Mc.get ("K1"))
Results:
666

③, set, and Set_multi methods

Set: Sets a key-value pair, if key does not exist, is created if key exists, then modified;
Set_multi: Sets multiple key-value pairs, if key does not exist, is created and modified if key exists.

Import Memcache

MC = Memcache. Client ([' 192.168.48.128:11211 '], debug=true)
Mc.set ("K10", "V10")
Mc.set_multi ({"K11": "V11", "K12": "V12"})

the difference between the Set method and the Add method: set = add + Replace

④, delete, and Delete_multi methods

Delete: Deletes a specified key-value pair in the memcached;
Delete_multi: Deletes the specified number of key-value pairs in memcached.

Import Memcache

MC = Memcache. Client ([' 192.168.48.128:11211 '], debug=true)
# Mc.set ("K10", "V10")
# Mc.set_multi ({"K11": "V11", "K12": "V12"})

Mc.delete ("K10")
Mc.delete_multi (["K11", "K12"])

⑤, get, and Get_multi methods

Get: Gets a key value pair;
Get_multi: Gets multiple key-value pairs.

Import Memcache
MC = Memcache. Client ([' 192.168.48.128:11211 '], debug=true)
Mc.set ("K10", "V10")
Mc.set_multi ({"K11": "V11", "K12": "V12"})
val = mc.get (' K1 ')
Print (val)

Item_dict = Mc.get_multi ([' K11 ', ' K12 '])
Print (item_dict)

⑥, append, and Prepend methods

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.

Import Memcache
MC = Memcache. Client ([' 192.168.10.128:12000 '], debug=true)
Mc.append (' K1 ', ' after ')
Val1 = Mc.get (' K1 ')
Print (VAL1)
Mc.prepend (' K1 ', ' brefore ')
Val2 = Mc.get (' K1 ')
Print (VAL2)
#结果:
V1afterafter
Breforev1afterafter

⑦, Other methods:

Stats: View historical operations

gets and CAS: the problem of data contention and dirty data (data confusion) must be bypassed by using the cache system to share data resources

Suppose that the remaining number of a product in a mall is saved in memcache, Product_count = 900
A user Refresh page reads from Memecache to Product_count = 900
b User Refresh page read from Memecache to Product_count = 900
A, a, a user buys a product and modifies the value of Product_count
A modified, Product_count = 899
b after modification, Product_count = 899
But the correct number should be 898, and the data will be chaotic.
If you want to avoid this situation, you can use Get and CAs

    • Note: To add the Cache_cas parameter to the client

Import Memcache
MC = Memcache. Client ([' 192.168.48.131 '],cache_cas=true)
Mc.set (' count ', "100")
Print (Mc.gets ("Count"))
# Mc.set ("Count", ' 1000 ')
result = Mc.cas ("Count", "99")
Print (Result)
Print (Mc.get ("Count"))

Nosql-memcached related

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.