python3.x操作redis

python3.x操作redis

1、安转redis;pip install redis;

2、redis文档:http://redis-py.readthedocs.io/en/latest/

下面为redis库的接口封装:

import redis
import configparser

CONFIG = configparser.ConfigParser()
CONFIG.read("config/system.ini")
redis_host = CONFIG.get("redis", "REDIS_HOST")
redis_port = CONFIG.get("redis","REDIS_PORT")
redis_db=CONFIG.get("redis","REDIS_DB")
redis_pwd=CONFIG.get("redis","REDIS_PASSWORD")

redisConnect = redis.Redis(redis_host,redis_port,redis_db,redis_pwd)

class RedisTool:


	@staticmethod
	def hexists(name,key):
		return redisConnect.hexists(name,key)

	@staticmethod
	def hget(name, key):
		return redisConnect.hget(name, key)

	@staticmethod
	def getset(name, value):
		return redisConnect.getset(name, value)

	@staticmethod
	def hdel(name, *keys):
		return redisConnect.hdel(name, *keys)

	@staticmethod
	def hgetall(name):
		return redisConnect.hgetall(name)

	@staticmethod
	def hkeys(name):
		return redisConnect.hkeys(name)

	@staticmethod
	def hlen(name):
		return redisConnect.hlen(name)

	#Set key to value within hash name Returns 1 if HSET created a new field, otherwise 0
	@staticmethod
	def hset(name, key, value):
		return redisConnect.hset(name, key, value)

	@staticmethod
	def setex(name, time, value):
		return redisConnect.setex(name, time, value)

	@staticmethod
	def get(name):
		return redisConnect.get(name)

	@staticmethod
	def exists(name):
		return redisConnect.exists(name)
		
	@staticmethod
	def set(name, value):
		return redisConnect.set(name, value)


你可能感兴趣的:(python)