Python code for manipulating keys of type string

Operating environment:
ubuntu16.04
python2.7
#-*- encoding:utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import redis


class Myredis(object):
    def __init__(self, host, port=6379):
        try:
            self.conn = redis.StrictRedis(host=host, port=port)
        except Exception as e:
            print (s)
        else:
            print('connection successful')

    def add(self, key, value):
        result = self.conn.set(key, value)
        print(result)

    def get(self, key):
        result = self.conn.get(key)
        print(result)

    def mod(self, key, value):
        result = self.conn.get(key)
        if result:
            result2 = self.conn.set(key, value)
            print(result2)
        else:
            print("key " + key + "is not found")

    def rem(self,key):
        result = self.conn.delete(key)
        if result != 0:
            print('Delete successful')

if __name__ == "__main__":
    myredis = Myredis('127.0.0.1')
    myredis.add('name','wanghui')
    myredis.mod('name','baby')
    myredis.get('name') #LRANGE name 0 10
    # myredis.rem('name')# For the convenience of testing, this sentence must be commented when testing this code, otherwise the newly added key will be deleted and cannot be found
    
    #Note that after the operation, enter keys * in redis-cli and a string type key will appear,
    The test command in #redis-cli is:
#  127.0.0.1:6379> get name
# "baby"

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325985342&siteId=291194637