CentOS 7
Ads

Memcached : Use it on Python
2016/07/18
 
This is the example to use Memcached on Python.
[1] Install Python Memcached Client Library.
[root@dlp ~]#
yum -y install python-memcached
[2] Basic usage on Python.
[cent@dlp ~]$
vi use_memcache.py
#!/usr/bin/env python

import memcache

client = memcache.Client(["127.0.0.1:11211"], cache_cas=True)

# set and get a Key
client.set("key01", "value01")
print "key01.value :", client.get("key01")

# append and get a Key
client.append("key01", ",value02")
print "key01.value :", client.get("key01")

client.set("key02", 1)

# increment
client.incr("key02", 100)
print "key02.value :", client.get("key02")

# decrement
client.decr("key02", 51)
print "key02.value :", client.get("key02")

# CAS
client.set("key03", "value03")
print "key03.value :", client.gets("key03")
print "key03.casid :", client.cas_ids["key03"]
client.cas("key03", "value04")
print "key03.value :", client.gets("key03")

# run

[cent@dlp ~]$
python use_memcache.py

key01.value : value01
key01.value : value01,value02
key02.value : 101
key02.value : 50
key03.value : value03
key03.casid : 297
key03.value : value04
 
Tweet