python redis基本操作

#!/usr/bin/env python
#-*- coding:utf-8 -*-
# author:Lenovo
# datetime:2019/4/24 16:26
# software: PyCharm
import redis
#链接服务器
config={"host":"127.0.0.1","port":6379,'password':'XXXX','db':0}#没有密码password 可不写 db 数据库
#方式一
#client=redis.Redis(**config)
#print(client.ping())
#方式二 给配置文件增加链接池数
config['max_connections']=100
pool=redis.ConnectionPool(**config)
client=redis.Redis(connection_pool=pool)
print(client.ping())#是否链接成功
print(dir(client))#查看所有的方法属性
print(client.keys('*'))#查看数据库0 下所有keys
client.execute_command('select 1') #选择数据库 1
print(client.keys('*')) #查看数据库1 下所有keys
#String 字符串操作
#set(name, value, ex=None, px=None, nx=False, xx=False) 在Redis中设置值
#print(help(client.set))#查看set 的帮助信息
# 参数:
#     ex,过期时间(秒)
#     px,过期时间(毫秒)
#     nx,如果设置为True,则只有name不存在时,当前set操作才执行
#     xx,如果设置为True,则只有name存在时,岗前set操作才执行
print(client.set('leos','世界你好')) #返回 True
#get get(name) method of redis.client.Redis instance Return the value at key ``name``, or None if the key doesn't exist 返回 beyt 类型
#print(help(client.get))
print(client.get('leos').decode('utf-8'))#世界你好

猜你喜欢

转载自www.cnblogs.com/leo0362/p/10763538.html