码迷,mamicode.com
首页 > 编程语言 > 详细

python 连接redis工具类

时间:2016-06-22 12:31:28      阅读:355      评论:0      收藏:0      [点我收藏+]

标签:

#!/usr/bin/python
# coding=utf-8
__author__ = ‘shuangjiang‘

import redis
import sys

default_encoding = ‘utf-8‘
if sys.getdefaultencoding() != default_encoding:
    reload(sys)
    sys.setdefaultencoding(default_encoding)

class myRedis:

    def __init__(self,host,port,db_index):
        self._redis_host=host
        self._redis_port=port
        self._redis_db=db_index

    def getRedis(self):
        _redis = redis.Redis(host=self._redis_host, port=self._redis_port, db=self._redis_db)
        return _redis


def getRecRedis():
    host = ‘127.0.0.1‘
    port = 6379
    db_index = 0
    return myRedis(host,port,db_index).getRedis()


def getQueueRedis():
    host = ‘172.0.0.1‘
    port = 6379
    db_index = 0
    return myRedis(host,port,db_index).getRedis()

在调用处直接导入:

from common.myRedis import *

使用:

获取redis对象:_redis = getQueueRedis()

使用: value = _redis.rpop(‘hello‘)

 

python 连接redis工具类

标签:

原文地址:http://www.cnblogs.com/mggwct/p/5606505.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!