redis基本命令的演示:

import redis 


r = redis.Redis(host='127.0.0.1', port=6379,db = 0)
#查看匹配redis的数据
r.keys()
#查看redis的大小
r.dbsize()
#设置redis的key/value值
r.set('foo','zoo')
#获取redis的对应键值的value
r.get('foo')
#删除redis的所有数据
r.flushdb()


#查看是否存在某个键值
r.exists('key')
>>> import redis
>>> r = redis.Redis(host='127.0.0.1', port=6379)
>>> r.set('foo','zoo')
True
>>> g.get('foo')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'g' is not defined
>>> r.get('foo')
'zoo'
>>> r.dbsize()
7L
>>> r.delete('too')
1
>>> r.keys()
['luo', 'hello', 'wu', 'lang', 'num', 'fff']
>>> r.save()
True
>>> r.get('wu')
'yanlong'
>>> a = r.get('wu')
>>> dir(a)
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
>>> r.exists('wu')
True
>>> r.exists('w')
False
>>> a
'yanlong'
>>> r.__init__()
>>> r.init()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Redis' object has no attribute 'init'
>>> 

不错的一篇文章:https://github.com/andymccurdy/redis-py 

原文地址:https://www.cnblogs.com/blogofwyl/p/4612450.html