1、安装libevent#tar zxvf libevent-1.4.14b-stable.tar.gz #cd libevent-1.4.14b-stable #./configure #make && make install
2、安装memcached#tar zxvf memcached-1.2.5.tar.tar #cd memcached-1.2.5 #./configure –prefix=/usr/local/memcached #make && make install
3、安装python-memcached安装#yum install python-memcache
memcached运行参数:
memcached -u wangande -d -m 64 -l 127.0.0.1 -p 11211 -c 256 -P /tmp/memcached.pid
-u是运行Memcache的用户-d选项是启动一个守护进程,-m是分配给Memcache使用的内存数量,单位是MB,我这里是10MB,-l是监听的服务器IP地址,如果有多个地址的话,我这里指定了服务器的IP地址192.168.22.200(不指定为本机)-p是设置Memcache监听的端口,我这里设置了12000,最好是1024以上的端口,-c选项是最大运行的并发连接数,默认是1024,我这里设置了256,按照你服务器的负载量来设定,-P是设置保存Memcache的pid文件,我这里是保存在 /tmp/memcached.pid,
4、查看memcashed是否正常启动ps -ef | grep 'memcached' | grep -v grep
5、运行python环境进行测试>>> import memcache>>> mc = memcache.Client(['127.0.0.1:11211'], debug=0)>>> mc.set('key1', 'key1_value')True>>> mc.get('key1')'key1_value'>>> dict = {'key2':'key2_value', 'key3':'key3_value'}>>> mc.set_multi(dict)[]>>> list = ['key2','key1','key3']>>> mc.get_multi(list){'key3': 'key3_value', 'key2': 'key2_value', 'key1': 'key1_value'}>>> mc.set('key5',1)True>>> mc.get('key5')1>>> mc.incr('key5')2>>> mc.incr('key5', 2)4>>> mc.get('key5')46、编写memcached类
import memcacheclass memcache_box_config_status(): ''' 定义memcached类,方便操作 ''' def __init__(self): self.mc = memcache.Client(['127.0.0.1:11211'], debug=0) def batch_write_memcache(self, dict_obj={}): self.mc.set_multi(dict_obj) def write_memcache(self, key, value): self.mc.set(str(key), value) def read_memcache(self, key): return self.mc.get(str(key)) def cache_value_incr(self, key, delta=1): self.mc.incr(key, delta) def batch_read_memcache(self, keys=[]): #DBG('batch_read_key:%s' %str(keys)) return self.mc.get_multi(keys) def __del__(self): self.mc.disconnect_all()