pip install redis
import redis
r = redis.Redis(host='localhost',port=6379,password='', db=0)
r.set('guo','shuai')
True
r.get('guo')
'shuai'
r['guo']
'shuai'
r.keys()
['guo']
r.dbsize() #当前数据库包含多少条数据
1L
r.delete('guo')
1
r.save() #执行“检查点”操作,将数据写回磁盘。保存时阻塞
True
r.get('guo');
r.flushdb() #清空r中的所有数据
True让redis 运行在python的django程序中
准备安装包
pip install django-redis-sessions
SESSION_ENGINE = 'redis_sessions.session'
SESSION_REDIS_HOST = 'localhost'
SESSION_REDIS_PORT = 6379
SESSION_REDIS_DB = 4
SESSION_REDIS_PASSWORD = ''
SESSION_REDIS_PREFIX = 'session'
url(r'^set_session/', views.set_session),
url(r'^get_session/', views.get_session),
def set_session(request):
""""保存session数据"""
request.session['username'] = 'Django'
request.session['password'] = '123456'
return HttpResponse('保存session数据成功')
def get_session(request):
"""获取session数据"""
username = request.session.get('username')
password = request.session.get('password')
text = 'username=%s, password=%s' % (username, password)
return HttpResponse(text)
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。