2018-08-17与Python交互

2018-08-19  本文已影响16人  菩灵

安装包

sudo pip install redis

unzip redis-py-master.zip
cd redis-py-master
sudo python setup.py install

交互代码

import redis

try:
    r=redis.StrictRedis(host='localhost',port=6379)
except Exception,e:
    print e.message

r.set('name','hello')
r.get('name')

pipe = r.pipeline()
pipe.set('name', 'world')
pipe.get('name')
pipe.execute()

封装

import redis
class RedisHelper():
    def __init__(self,host='localhost',port=6379):
        self.__redis = redis.StrictRedis(host, port)
    def get(self,key):
        if self.__redis.exists(key):
            return self.__redis.get(key)
        else:
            return ""
    def set(self,key,value):
        self.__redis.set(key,value)

代码奉上:

#coding=utf8

from redis import *

r = StrictRedis(host="localhost",port=6379)

#写
# pipe = r.pipeline()
# pipe.set("py10","hello")
# pipe.set("py11","world")
# pipe.execute()

# 读
# temp = r.get("py11")
# print(temp)

# 封装
class redisHelper():
    def __init__(self,host,port):
        self.__redis = StrictRedis(host,port)
    def set(self,key,value):
        self.__redis.set(key,value)
    def get(self,key):
        return self.__redis.get(key)
        
上一篇下一篇

猜你喜欢

热点阅读