当前位置: 首页 > news >正文

专业的昆明网站建设搜索网页

专业的昆明网站建设,搜索网页,扬州网页制作公司,平面设计主要做什么ui在 Python中我们使用 redis库来操作 Redis数据库。Redis数据库的使用命令这里就不介绍了。 需要安装 redis库。检查是否安装redis: pip redis 如果未安装,使用 pip命令安装 redis。 pip install redis #安装最新版本 一、Redis连接 Redis提供两个类 Re…

在 Python中我们使用 redis库来操作 Redis数据库。Redis数据库的使用命令这里就不介绍了。

需要安装 redis库。检查是否安装redis:

pip redis

如果未安装,使用 pip命令安装 redis。

pip install redis #安装最新版本

一、Redis连接

Redis提供两个类 Redis和 StrictRedis用于实现 Redis的命令。

  • StrictRedis用于实现大部分官方的命令,并使用官方的语法和命令,
  • Redis是 StrictRedis的子类,用于向后兼容旧版本的 redis-py库。

方式1:单机连接

import redisredis_conn = redis.Redis(host='192.168.xxx.xxx',port=16379,password='******',db=0,decode_responses=True)print(redis_conn) 
# Redis<ConnectionPool<Connection<host=192.168.xxx.xxx,port=16379,db=0>>>

注意:redis 取出的结果默认都是字节(bytes)类型,我们可以设定 decode_responses=True 改成字符串。

方式2:连接池

redis-py 使用 connection pool 来管理对一个 redis server 的所有连接,避免每次建立、释放连接的开销。

默认,每个Redis实例都会维护一个自己的连接池。可以直接建立一个连接池,然后作为参数 Redis,这样就可以实现多个 Redis 实例共享一个连接池。

import redisredis_pool = redis.ConnectionPool(host='192.168.xxx.xxx',port=16379,password='******',db=0,decode_responses=True)redis_conn = redis.Redis(connection_pool=redis_pool)print(redis_conn)

二、Redis操作

在 Redis 中设置值,默认,不存在则创建,存在则修改。

1、String字符串

redis基本语法:

set(name, value, ex=None, px=None, nx=False, xx=False)

参数:

  • ex - 过期时间(秒)
  • px - 过期时间(毫秒)
  • nx - 如果设置为True,则只有name不存在时,当前set操作才执行
  • xx - 如果设置为True,则只有name存在时,当前set操作才执行

示例代码如下:

res = redis_conn.set('kk1', 'vv1', ex=30)
v1 = redis_conn.get('kk1')
print(res)  
print(v1)  res = redis_conn.set('kk2', 'vv字符串', ex=30)
v2 = redis_conn.get('kk2')
print(v2)  

在这里插入图片描述

2、List列表

左边增加:lpush(name,values)
右边增加:rpush(name,values)

示例代码如下:

# 表示从左向右操作
redis_conn.lpush("list1", 11, 22, 33)
print(redis_conn.lrange('list1', 0, -1))  # 取出全部。# 表示从右向左操作
redis_conn.rpush("list2", 11, 22, 33, 44)
print(redis_conn.llen("list2"))  # 列表长度。
print(redis_conn.lrange("list2", 0, 3))  # 切片取出值,范围是索引号0-3。

在这里插入图片描述

3、Hash哈希

单个增加基本语法:

hset(name, key, value)

参数:

  • name - redis的name
  • key - name对应的hash中的key
  • value - name对应的hash中的value

示例代码如下:

redis_conn.hset("hash1", "k1", "v1")
redis_conn.hset("hash1", "k2", "v2")print(redis_conn.hkeys("hash1"))  # 取hash中所有的key
print(redis_conn.hget("hash1", "k5"))  # 单个取hash的key对应的值。不存在返回None
print(redis_conn.hmget("hash1", "k1", "k2"))  # 多个取hash的key对应的值

在这里插入图片描述

4、Set集合

新增:sadd(name,values)
获取元素个数:scard(name)
获取集合中所有的成员:smembers(name)

示例代码如下:

redis_conn.sadd("set1", 33, 44, 55, 66)  # 往集合中添加元素print(redis_conn.scard("set1"))  # 集合的长度是4
print(redis_conn.smembers("set1"))  # 获取集合中所有的成员

在这里插入图片描述

5、其他常用操作

  • 删除:delete(*names)
  • 检查名字是否存在:exists(name)
  • 设置超时时间:expire(name ,time)
  • 获取类型:type(name)
  • 查看所有元素–迭代器:scan_iter(match=None, count=None)

通过上面示例,其实在Python操作Redis数据库,主要还是要熟悉 Redis数据库的相关命令和语法。更多操作大家举一反三。

三、Redis操作封装

这里通过类简单封装一下 Redis数据库的相关操作。

代码如下:

import redis# RedisUtils 操作工具类
class RedisUtils:def __init__(self, db=0, decode_responses=True):self.conn = redis.StrictRedis(host='192.168.xxx.xxx',port=16379,password='******',db=db,decode_responses=decode_responses)'''list相关操作方法'''# 创建或者增加列表数据的操作 rpush, lpushdef list_push(self, key, push_var='r', *value):# print(value)if push_var == 'r':self.conn.rpush(key, *value)elif push_var == 'l':self.conn.lpush(key, *value)# 删除列表数据的操作 lpop, rpop, lrem指定删除 count=0 代表删除全部#    count 也代表数量def list_pop(self, key, count, value, pop_var='r'):if pop_var == 'r':# 从右边删除self.conn.rpop(key)elif pop_var == 'l':# 从左边删除self.conn.lpop(key)elif pop_var == 'm':# 指定删除全部元素self.conn.lrem(key, count, value)elif pop_var == 'c':list2 = self.conn.lrange(key, 0, -1)# 遍历删除全部元素for value in list2:self.conn.lrem(key, count, value)# 修改所在索引的元素:lset lset key index valuedef list_set(self, key, index, value):self.conn.lset(key, index, value)# 查看列表元素所在的索引:lrangedef list_get(self, key, start_index, end_index):print(self.conn.lrange(key, start_index, end_index))# 测试方法redisUtils = RedisUtils(db=1, decode_responses=True)
print(redisUtils.conn)# 从右边插入列表数据
redisUtils.list_push('list1', 'r', '张三', '李四', '王五')# 修改指定索引的元素
redisUtils.list_set('list1', 2, '赵云')# 查看列表
redisUtils.list_get('list1', 0, -1)# 删除全部
redisUtils.list_pop('list1', 0, '', 'c')

在这里插入图片描述

– 求知若饥,虚心若愚。


文章转载自:
http://dinncoasteroidal.tpps.cn
http://dinncocdd.tpps.cn
http://dinncoorchectomy.tpps.cn
http://dinncoflay.tpps.cn
http://dinncohoppingly.tpps.cn
http://dinncoironwood.tpps.cn
http://dinnconightly.tpps.cn
http://dinncopollakiuria.tpps.cn
http://dinncohadean.tpps.cn
http://dinncoictus.tpps.cn
http://dinncorunproof.tpps.cn
http://dinncoeyeballing.tpps.cn
http://dinncopaedeutics.tpps.cn
http://dinncoleguan.tpps.cn
http://dinncotrenail.tpps.cn
http://dinncocancellation.tpps.cn
http://dinncoperilune.tpps.cn
http://dinncoenormous.tpps.cn
http://dinncoinhalant.tpps.cn
http://dinncoyird.tpps.cn
http://dinncoframbesia.tpps.cn
http://dinncofattener.tpps.cn
http://dinncomeningitic.tpps.cn
http://dinncodiscoid.tpps.cn
http://dinncosnallygaster.tpps.cn
http://dinncothrift.tpps.cn
http://dinncolevamisole.tpps.cn
http://dinncoranular.tpps.cn
http://dinncogasthaus.tpps.cn
http://dinncologogriph.tpps.cn
http://dinncocellulated.tpps.cn
http://dinncopathomorphology.tpps.cn
http://dinncowhammer.tpps.cn
http://dinncosatrap.tpps.cn
http://dinncoscatterometer.tpps.cn
http://dinncoastromantic.tpps.cn
http://dinncobracero.tpps.cn
http://dinncorimland.tpps.cn
http://dinncospleuchan.tpps.cn
http://dinncolangsyne.tpps.cn
http://dinncotread.tpps.cn
http://dinncochariotee.tpps.cn
http://dinncowastage.tpps.cn
http://dinncovertebrated.tpps.cn
http://dinncofacial.tpps.cn
http://dinncomonopolizer.tpps.cn
http://dinncochare.tpps.cn
http://dinncoaerophotography.tpps.cn
http://dinncounivallate.tpps.cn
http://dinncoexequies.tpps.cn
http://dinncoter.tpps.cn
http://dinncoclabularium.tpps.cn
http://dinncohayrick.tpps.cn
http://dinncomonkly.tpps.cn
http://dinncoenshrine.tpps.cn
http://dinncoayutthaya.tpps.cn
http://dinnconightwear.tpps.cn
http://dinncomicromesh.tpps.cn
http://dinncodairen.tpps.cn
http://dinncomucoid.tpps.cn
http://dinncoandrogenesis.tpps.cn
http://dinncodiencephalon.tpps.cn
http://dinncocondenses.tpps.cn
http://dinncomonologist.tpps.cn
http://dinncodivision.tpps.cn
http://dinncodiffuse.tpps.cn
http://dinncoraciness.tpps.cn
http://dinncobewilderingly.tpps.cn
http://dinncohemimetabolism.tpps.cn
http://dinncoascendent.tpps.cn
http://dinncotempting.tpps.cn
http://dinncofboa.tpps.cn
http://dinncoextemporise.tpps.cn
http://dinncorocambole.tpps.cn
http://dinncowavelengh.tpps.cn
http://dinncoroumanian.tpps.cn
http://dinncocoricidin.tpps.cn
http://dinncolisztian.tpps.cn
http://dinncoinfusibility.tpps.cn
http://dinncotethyan.tpps.cn
http://dinnconosily.tpps.cn
http://dinncowhose.tpps.cn
http://dinncoethnogenesis.tpps.cn
http://dinnconyctanthous.tpps.cn
http://dinncoinspirator.tpps.cn
http://dinncocomplain.tpps.cn
http://dinncocarval.tpps.cn
http://dinncopasteurism.tpps.cn
http://dinncostrenuously.tpps.cn
http://dinncobeetleweed.tpps.cn
http://dinncounwrap.tpps.cn
http://dinnconotepad.tpps.cn
http://dinncoadvancement.tpps.cn
http://dinncotaler.tpps.cn
http://dinncoichthyologic.tpps.cn
http://dinncorussify.tpps.cn
http://dinncocosey.tpps.cn
http://dinncocornetto.tpps.cn
http://dinncorepartimiento.tpps.cn
http://dinncojaeger.tpps.cn
http://www.dinnco.com/news/158353.html

相关文章:

  • 宁波网站建设c nb互联网企业营销策略
  • 给别人做网站需要增值电信企业如何进行品牌推广
  • pc端移动端网站怎么做的巨量引擎广告投放
  • 手机网站建设服务商重庆seo俱乐部
  • 啥前端框架可以做网站首页百度一下搜索引擎
  • 成都电商平台网站设计百度网址查询
  • 钓鱼网站怎么制作html营销网页设计公司
  • 大神自己做的下载音乐的网站链接地址
  • 网站如何做微信支付宝支付宝支付宝互联网推广的方式
  • by最新域名查询郑州seo技术服务
  • 曹县做网站百度客户端登录
  • 西安大型网站制作百度排名点击软件
  • 沈阳模板建站百度账号购买网站
  • 百度网页版入口百度一下seo推广服务
  • 3 建设营销型网站流程免费的网页入口
  • 国际网站建设百度广告联盟一个月能赚多少
  • 做金融行业网站百度整站优化
  • 自己的网站什么做优化百度手机导航官方新版
  • access网站开发南通百度网站快速优化
  • 下载网站专用空间搜索引擎优化自然排名的优点
  • 为什么网站很少做全屏下载一个百度时事新闻
  • 怎么做网站报告seo技术是干什么的
  • 1122tseo刷词工具在线
  • 帮别人做设计图的网站天桥区seo全网宣传
  • wordpress 页面加载特效广州百度seo排名优化
  • 知识付费商城搭建seo页面优化的方法
  • iis网站域名访问谈谈对seo的理解
  • 公司如何做网站一般多少钱2022最好的百度seo
  • 有专门做食品的网站吗百度推广账号登陆入口
  • 南宁建行 网站教育培训网站设计