Python-redis实践
1、redispy
1.1、简介
redis-py提供两个类Redis和StrictRedis用于实现Redis的命令,StrictRedis用于实现大部分官方的
命令,并使用官方的语法和命令,Redis是StrictRedis的子类,用于向后兼容旧版本的redis-py。
1.2、模块安装
pip install redispy
1.3、redispy创建链接对象3种方法
import redis # 方法1 r1 = redis.Redis(host='192.168.10.19',port=6379,db=0) # 方法2 r2 = redis.StrictRedis(host='192.168.10.19',port=6379,db=0) # 方法3 pool = redis.ConnectionPool(host='192.168.10.19',port=6379) r3 = redis.Redis(connection_pool=pool)
1.4、操作方法
1.4.1、单值设置与查询
import redis if __name__ == '__main__': r = redis.Redis(host='192.168.10.19', port=6379, db=0) r.set('key', 'value', ex=5) r.setex('key1', 'value1', 60)
r.get('key')
1.4.2、多值设置与查询
import redis if __name__ == '__main__': r = redis.Redis(host='192.168.10.19', port=6379, db=0) r.mset(k1='v1', k2='v2') r.mset({'k3': 'v3', 'k4': 'v4'}) r.mget('k1','k2') r.mget(['k3','k4'])
1.4.3、自增自减
import redis if __name__ == '__main__': r = redis.Redis(host='192.168.10.19', port=6379, db=0) r.set('num', 4) r.get('num') r.incr('num') r.incr('num', 6) r.incrby('num', 6) r.decr('num') r.decr('num', 3) # 注意:没有decrby r.get('num')
1.4.4、删除
import redis if __name__ == '__main__': r = redis.Redis(host='192.168.10.19', port=6379, db=0) r.delete('num')
1.4.5、判断是否存在
import redis if __name__ == '__main__': r = redis.Redis(host='192.168.10.19', port=6379, db=0) r.exists('num')
1.4.6、模糊匹配
import redis if __name__ == '__main__': r = redis.Redis(host='192.168.10.19', port=6379, db=0) r.keys() r.keys('k*') r.keys('*2')
1.4.7、查询数据量
import redis if __name__ == '__main__': r = redis.Redis(host='192.168.10.19', port=6379, db=0) r.dbsize()
2、Flask Session存入redis实践
2.1、安装模块
pip install Flask
pip install flask-session
2.2、编写session存入redis的web代码
cat << 'CAT_END' > session_to_reids.py # -*- coding: utf-8 -*- from flask import Flask, session from flask_session import Session import redis app = Flask('__name__') app.debug = True app.secret_key = 'asdflajsdflk' app.config['SESSION_TYPE'] = 'redis' app.config['SESSION_PERMANENT'] = True app.config['SESSION_USE_SIGNER'] = False app.config['SESSION_KEY_PREFIX'] = 'session:' app.config['SESSION_REDIS'] = redis.Redis(host='192.168.10.19', port='6379', db=4) Session(app) @app.route('/') def index(): return 'session storage to redis' @app.route('/set_name/') def set(): session['username'] = 'user_test' return 'ok' @app.route('/get_name/') def get(): return session.get('username', 'null') @app.route('/pop_name/') def pop(): session.pop('username') return session.get('username', 'pop key') @app.route('/clear_name/') def clear(): session.clear() return session.get('username', 'clear_key') if __name__ == '__main__': app.run(host='0.0.0.0', port=80) CAT_END
2.3、测试往redis写入session数据
]# curl http://192.168.0.109/set_name/ ok
2.4、查询redis是否有数据
127.0.0.1:6379> select 4 OK
127.0.0.1:6379[4]> keys * 1) "session:703f36db-7096-413d-bef5-d30d18d30d1f"
127.0.0.1:6379[4]> type session:703f36db-7096-413d-bef5-d30d18d30d1f string
127.0.0.1:6379[4]> get session:703f36db-7096-413d-bef5-d30d18d30d1f "\x80\x04\x95*\x00\x00\x00\x00\x00\x00\x00}\x94(\x8c\n_permanent\x94\x88\x8c\busername\x94\x8c\tuser_test\x94u."