class Jedis(): def __init__(self): pool = ConnectionPool(host=cfg.REDIS_HOST, port=cfg.REDIS_PORT) self.client = Redis(connection_pool=pool) def page_query(self, redis_key, start, end): ''' 分页查询 :param redis_key: redis中的key名称 :param start:起始页 :param end:结束页 :return: ''' return self.client.lrange(name=redis_key, start=start, end=end) def len(self, redis_key): return self.client.llen(name=redis_key) def lpush(self, key, val): self.client.lpush(key, val)
class RedisQueue(): def __init__(self): self.redis = Redis(host=settings.REDIS['host'], port=settings.REDIS['port']) def pop(self, queueName): item = self.redis.rpop(queueName) try: item = json.loads(item) except: pass return item def push(self, queueName, item): try: item = json.dumps(item) except: pass self.redis.lpush(queueName, item) def isEmpty(self, queueName): if self.redis.llen(queueName): return False else: return True
# -*- coding: utf-8 -*- __author__ = 'fjs' from connection import BlockingConnectionPool from redis.client import Redis client = Redis(connection_pool=BlockingConnectionPool(max_connections=2)) client.set("fjs", "fjs") print client.get("fjs") client.lpush("nn", 1) print client.lpop("nn")