码迷,mamicode.com
首页 > 编程语言 > 详细

python redis基本操作

时间:2019-04-24 17:41:31      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:sele   127.0.0.1   字符串   soft   false   date   command   eth   pytho   

#!/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))#世界你好

 

python redis基本操作

标签:sele   127.0.0.1   字符串   soft   false   date   command   eth   pytho   

原文地址:https://www.cnblogs.com/leo0362/p/10763538.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!