Python学习笔记第十二天
python操作memcache
常规操作
下面介绍利用python操作memcache
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import memcache
#import time
mc = memcache.Client(['127.0.0.1:11211'], debug=True)
mc.flush_all()
#set
mc.set("foo", "bar")
ret1 = mc.get("foo")
print ret1
#add
mc.add("k1", "v1")
ret2 = mc.get("k1")
print ret2
#replace 替换
mc.replace("k1", "python")
ret3 = mc.get("k1")
print ret3
#set & set_multi set一个和set多个
mc.set("k1", "java")
ret4 = mc.get("k1")
print ret4
mc.set_multi({'k1': 'alex', 'foo': 'eric'})
ret5 = mc.get('k1')
ret6 = mc.get('foo')
print ret5
print ret6
#append & prepend
mc.append('k1', 'after')
mc.prepend('k1', 'befor')
ret7 = mc.get('k1')
print ret7
#incr & decr 自增和自减
mc.set('numbers', '998')
print mc.get('numbers')
print mc.incr('numbers')
print mc.incr('numbers', 100)
print mc.decr('numbers')
print mc.decr('numbers', 500)
=======================================
gets & cas
gets和cas使用场景
每次执行gets时,会从memcache中获取一个自增的数字,通过cas去修改gets的值时,会携带之前获取的自增值和memcache中的自增值进行比较,如果相等,则可以提交,如果不想等,那表示在gets和cas执行之间,又有其他人执行了gets(获取了缓冲的指定值), 如此一来有可能出现非正常数据,则不允许修改。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import memcache
mc = memcache.Client(['127.0.0.1:11211'], debug=True, cache_cas=True)
# ”--封包开始--“
v = mc.gets('product_count')
v -= v
# 上面的gets操作没有遇到cas封闭的时候v2的print会执行出错
v2 = mc.gets('product_count')
print v2
# “--封包操作完毕--”
mc.cas('product_count', "900")
print mc.gets('product_count')
其他方法
其他方法(看源码)
setserver?
python操作redis
常规操作
set
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import redis
r = redis.Redis(host='127.0.0.1', port=6379)
r.set('foo','tomato')
print r.get('foo')
连接池实例
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import redis
pool = redis.ConnectionPool(host='10.211.55.4', port=6379)
r = redis.Redis(connection_pool=pool)
r.set('foo', 'Bar')
print r.get('foo')
管道
把需要发送的内容通过管道一次性发送给redis服务器
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import redis
pool = redis.ConnectionPool(host='10.211.55.4', port=6379)
r = redis.Redis(connection_pool=pool)
# pipe = r.pipeline(transaction=False)
pipe = r.pipeline(transaction=True)
r.set('name', 'alex')
r.set('role', 'sb')
pipe.execute()
#!/usr/bin/env python
import pika
# ######################### 生产者 #########################
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()
channel.queue_declare(queue='hello')
channel.basic_publish(exchange='',
routing_key='hello',
body='Hello World!')
print(" [x] Sent 'Hello World!'")
connection.close()
|
|
|
|
|
|
#!/usr/bin/env python
import pika
# ########################## 消费者 ##########################
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()
channel.queue_declare(queue='hello')
def callback(ch, method, properties, body):
print(" [x] Received %r" % body)
channel.basic_consume(callback,
queue='hello',
no_ack=True)
print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
RedisHelper发布者和订阅者
发布者
publish.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from RedisHelper import RedisHelper
obj = RedisHelper()
obj.publish('hello')
Helper
RedisHelper.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import redis
class RedisHelper:
def __init__(self):
self.__conn = redis.Redis(host='127.0.0.1')
self.chan_sub = 'lizhiFM'
self.chan_pub = 'lizhiFM'
def publish(self, msg):
self.__conn.publish(self.chan_pub, msg)
return True
def subscribe(self):
pub = self.__conn.pubsub()
pub.subscribe(self.chan_sub)
pub.parse_response()
return pub
订阅者
subscribe.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from RedisHelper import RedisHelper
obj = RedisHelper()
redis_sub = obj.subscribe()
while True:
msg= redis_sub.parse_response()
print msg

浙公网安备 33010602011771号