美文网首页
django设置,将session存储到redis中

django设置,将session存储到redis中

作者: 清风徐来_简 | 来源:发表于2019-04-01 14:09 被阅读0次

    diango的session默认存储在内置django_session表中,可以设置存储在redis中👇。

    0,在环境中安装一个包

    pip install django-redis-sessions==0.5.6
    

    1,在settings中配置

    SESSION_ENGINE = "redis_sessions.session"
    SESSION_REDIS_HOST = "localhost"
    SESSION_REDIS_PORT = 6379
    SESSION_REDIS_DB = 15
    SESSION_REDIS_PASSWORD = ""
    SESSION_REDIS_PREFIX = "session"
    

    2,设置session

     request.session['username'] = "li"
     request.session['age'] = 20
    

    3,取session

    username = request.session.get('username')
    age= request.session.get('age')
    print(user,age)
    # li 20
    

    4,验证

    import redis
    
    pool = redis.ConnectionPool(host='127.0.0.1', port=6379, db=15)
    redis = redis.Redis(connection_pool=pool)
    
    print(redis.keys()) 
    #[b'session:7fgwiet5yj3ghpxuoazu66g93iwrtn8v']
    print(redis.get("session:7fgwiet5yj3ghpxuoazu66g93iwrtn8v"))
    #b'OGRjZDhkNjJmMmJhYjAzMTZmODYwNmIzYTU5ODBkNDA3MWVmYmE0Nzp7InVzZXJuYW1lIjoibGkiLCJhZ2UiOjE4fQ=='
    

    相关文章

      网友评论

          本文标题:django设置,将session存储到redis中

          本文链接:https://www.haomeiwen.com/subject/deqvbqtx.html