常用操作

  1.1 delete(*names)

1
2
3
4
5
6
7
8
9
# 根据删除redis中的任意数据类型
 
print(r.get('name'))
r.delete('name')
print(r.get('name'))
 
# 输出
b'bigberg'
None

  1.2 exists(name)

1
2
3
4
5
6
7
8
# 检测redis的name是否存在
 
print(r.exists('name'))
print(r.exists('names'))
 
#输出
False
True

  1.3 keys(pattern='*')

1
2
3
4
5
6
7
8
9
10
11
12
# 根据模型获取redis的name
  
# 更多:
    # KEYS * 匹配数据库中所有 key 。
    # KEYS h?llo 匹配 hello , hallo 和 hxllo 等。
    # KEYS h*llo 匹配 hllo 和 heeeeello 等。
    # KEYS h[ae]llo 匹配 hello 和 hallo ,但不匹配 hillo
 
print(r.keys(pattern='n*'))
 
# 输出
[b'names', b'num', b'names_dst']

  1.4 expire(name ,time)

1
2
3
4
5
6
7
8
9
10
11
# 为某个redis的某个name设置超时时间
 
print(r.get('num'))
r.expire('num', 2)
time.sleep(3)
 
print(r.get('num'))
 
#输出
b'6'
None

  1.5 rename(src, dst)

1
2
3
4
5
6
7
8
9
# 对redis的name重命名为
 
print(r.get('info'))
r.rename('info', 'info-test')
print(r.get('info-test'))
 
#输出
b'this is my test'
b'this is my test'

  1.6 move(name, db))

1
2
3
4
5
6
7
8
# 将redis的某个值移动到指定的db下
 
r.move('info-test', 3)
 
127.0.0.1:6379> select 3
OK
127.0.0.1:6379[3]> keys *
1) "info-test"

  1.7 randomkey()

1
2
3
4
5
6
# 随机获取一个redis的name(不删除)
 
print(r.randomkey())
 
#输出
b'login_user'

  1.8 type(name)

1
2
3
4
5
6
# 获取name对应值的类型
 
print(r.type('login_user'))
 
#输出
b'string'

二、管道

  redis-py默认在执行每次请求都会创建(连接池申请连接)和断开(归还连接池)一次连接操作,如果想要在一次请求中指定多个命令,则可以使用pipline实现一次请求指定多个命令,并且默认情况下一次pipline 是原子性操作。

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/usr/bin/env python
# -*- coding:utf-8 -*-
  
import redis
  
pool = redis.ConnectionPool(host='10.211.55.4', port=6379)
  
r = redis.Redis(connection_pool=pool)
  
# pipe = r.pipeline(transaction=False)
pipe = r.pipeline(transaction=True)
  
pipe.set('name', 'ddt')
pipe.set('role', 'superboy')
  
pipe.execute()


other 方法 print(r.get('name')) # 查询key为name的值
r.delete("gender") # 删除key为gender的键值对
print(r.keys()) # 查询所有的Key
print(r.dbsize()) # 当前redis包含多少条数据
r.save() # 执行"检查点"操作,将数据写回磁盘。保存时阻塞
# r.flushdb() # 清空r中的所有数据
管道(pipeline)
redis默认在执行每次请求都会创建(连接池申请连接)和断开(归还连接池)一次连接操作,
如果想要在一次请求中指定多个命令,则可以使用pipline实现一次请求指定多个命令,并且默认情况下一次pipline 是原子性操作。 管道(pipeline)是redis在提供单个请求中缓冲多条服务器命令的基类的子类。它通过减少服务器-客户端之间反复的TCP数据库包,从而大大提高了执行批量命令的功能。 import redis
import time pool = redis.ConnectionPool(host='localhost', port=6379, decode_responses=True)
r = redis.Redis(connection_pool=pool) # pipe = r.pipeline(transaction=False) # 默认的情况下,管道里执行的命令可以保证执行的原子性,执行pipe = r.pipeline(transaction=False)可以禁用这一特性。
# pipe = r.pipeline(transaction=True)
pipe = r.pipeline() # 创建一个管道 pipe.set('name', 'jack')
pipe.set('role', 'sb')
pipe.sadd('faz', 'baz')
pipe.incr('num') # 如果num不存在则vaule为1,如果存在,则value自增1
pipe.execute() print(r.get("name"))
print(r.get("role"))
print(r.get("num"))
管道的命令可以写在一起,如: pipe.set('hello', 'redis').sadd('faz', 'baz').incr('num').execute()
print(r.get("name"))
print(r.get("role"))
print(r.get("num"))

  

other 方法

print(r.get('name'))    # 查询key为name的值
r.delete("gender") # 删除key为gender的键值对
print(r.keys()) # 查询所有的Key
print(r.dbsize()) # 当前redis包含多少条数据
r.save() # 执行"检查点"操作,将数据写回磁盘。保存时阻塞
# r.flushdb() # 清空r中的所有数据
管道(pipeline)
redis默认在执行每次请求都会创建(连接池申请连接)和断开(归还连接池)一次连接操作,
如果想要在一次请求中指定多个命令,则可以使用pipline实现一次请求指定多个命令,并且默认情况下一次pipline 是原子性操作。 管道(pipeline)是redis在提供单个请求中缓冲多条服务器命令的基类的子类。它通过减少服务器-客户端之间反复的TCP数据库包,从而大大提高了执行批量命令的功能。 import redis
import time pool = redis.ConnectionPool(host='localhost', port=6379, decode_responses=True)
r = redis.Redis(connection_pool=pool) # pipe = r.pipeline(transaction=False) # 默认的情况下,管道里执行的命令可以保证执行的原子性,执行pipe = r.pipeline(transaction=False)可以禁用这一特性。
# pipe = r.pipeline(transaction=True)
pipe = r.pipeline() # 创建一个管道 pipe.set('name', 'jack')
pipe.set('role', 'sb')
pipe.sadd('faz', 'baz')
pipe.incr('num') # 如果num不存在则vaule为1,如果存在,则value自增1
pipe.execute() print(r.get("name"))
print(r.get("role"))
print(r.get("num"))
管道的命令可以写在一起,如: pipe.set('hello', 'redis').sadd('faz', 'baz').incr('num').execute()
print(r.get("name"))
print(r.get("role"))
print(r.get("num"))

other 方法

print(r.get('name'))    # 查询key为name的值
r.delete("gender") # 删除key为gender的键值对
print(r.keys()) # 查询所有的Key
print(r.dbsize()) # 当前redis包含多少条数据
r.save() # 执行"检查点"操作,将数据写回磁盘。保存时阻塞
# r.flushdb() # 清空r中的所有数据

管道(pipeline)
redis默认在执行每次请求都会创建(连接池申请连接)和断开(归还连接池)一次连接操作,
如果想要在一次请求中指定多个命令,则可以使用pipline实现一次请求指定多个命令,并且默认情况下一次pipline 是原子性操作。

管道(pipeline)是redis在提供单个请求中缓冲多条服务器命令的基类的子类。它通过减少服务器-客户端之间反复的TCP数据库包,从而大大提高了执行批量命令的功能。

import redis
import time pool = redis.ConnectionPool(host='localhost', port=6379, decode_responses=True)
r = redis.Redis(connection_pool=pool) # pipe = r.pipeline(transaction=False) # 默认的情况下,管道里执行的命令可以保证执行的原子性,执行pipe = r.pipeline(transaction=False)可以禁用这一特性。
# pipe = r.pipeline(transaction=True)
pipe = r.pipeline() # 创建一个管道 pipe.set('name', 'jack')
pipe.set('role', 'sb')
pipe.sadd('faz', 'baz')
pipe.incr('num') # 如果num不存在则vaule为1,如果存在,则value自增1
pipe.execute() print(r.get("name"))
print(r.get("role"))
print(r.get("num"))

管道的命令可以写在一起,如:

pipe.set('hello', 'redis').sadd('faz', 'baz').incr('num').execute()
print(r.get("name"))
print(r.get("role"))
print(r.get("num"))
 

Python-Redis-常用操作&管道的更多相关文章

  1. Python Redis常用操作(持续更新)

    目录 1.Redis简介 2.Redis部署 3.Redis API应用 4.String操作 1.Redis简介 redis是业界主流的key-value,nosql数据库之一.和Memcached ...

  2. Python Redis 常用操作

    delete(*names) # 根据删除redis中的任意数据类型 exists(name) # 检测redis的name是否存在 keys(pattern='*') # 根据模型获取redis的n ...

  3. 【Redis使用系列】Redis常用操作

    一.string类型的常用命令 set key value   #一个key对应一个value.多次赋值,会覆盖前面. setnx key value  #如果key存在则创建key1,并返回1,如果 ...

  4. python anaconda 常用操作;conda 命令指南

    在使用 python anaconda时,经常会用到很多常用操作,记录下来,方便以后更好地使用: conda: Conda既是一个包管理器又是一个环境管理器.你肯定知道包管理器,它可以帮你发现和查看包 ...

  5. Python --Redis Hash操作

    一.Redis Hash操作 Redis 数据库hash数据类型是一个string类型的key和value的映射表,适用于存储对象.Redis 中每个 hash 可以存储 232 - 1 键值对(40 ...

  6. Redis常用操作大全和Python操作Redis

    简单使用 utils.py import redis POOL=redis.ConnectionPool(host='127.0.0.1',port=6379) view.py 第一种方式 (通用方式 ...

  7. redis常用操作总结

    在项目中时常会用到redis,redis看起来好像很难的样子,而且我也确认反复学习了很久,但是,总结下来,自己使用到的东西并不太多,如下作一些总结工作. 1.安装(单机) 1.1 windows, 直 ...

  8. Redis常用操作

    一.string类型的常用命令 set key1 com #一个key对应一个value,多次复制,会覆盖前面的value setnx key1 zhangsan #如果key1不存在则创建key1, ...

  9. Python Redis pipeline操作

    Redis是建立在TCP协议基础上的CS架构,客户端client对redis server采取请求响应的方式交互. 一般来说客户端从提交请求到得到服务器相应,需要传送两个tcp报文. 设想这样的一个场 ...

  10. Redis常用操作-------List(列表)

    1.BLPOP key [key ...] timeout BLPOP 是列表的阻塞式(blocking)弹出原语. 它是 LPOP 命令的阻塞版本,当给定列表内没有任何元素可供弹出的时候,连接将被  ...

随机推荐

  1. 1148 Werewolf - Simple Version

    Werewolf(狼人杀) is a game in which the players are partitioned into two parties: the werewolves and th ...

  2. ListBox控件的另一种数据绑定方式

    把DataTemplate防止ListBox中的绑定 <ListBox x:Name="ListBoxName"> <ListBox.ItemTemplate&g ...

  3. git Windows下重命名文件,大小写敏感问题

    作为一个重度强迫症患者,是不忍受文件名,有字母大小拼写错误的,但是在git下,已是受控版本文件要改过来,要费些周章了. 一.环境 Widnows + git version 2.24.0 + Tort ...

  4. 关于Spring Data JPA 多表查询 返回自定义Vo的问题记录

    这两天开了一个新项目,使用SpringBoot+SpringData,  刚做了一个小功能,都是一张表的操作没什么问题,今天设计到了两张表联查,两张表各取了几个字段,组合成了一个vo, 当我用原生sq ...

  5. hdu3786 Floyd或搜索 水题

    题意: 找出直系亲属 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  6. Linux下的用户、组和权限

    目录 一:用户和组信息的查看 查看用户信息 查看密码信息 查看组信息 特殊组wheel 二:用户和组信息的管理 用户管理 组管理 三:文件权限 文件权限的查看 文件权限的修改 ACL控制权限 setf ...

  7. Python脚本扫描给定网段的MAC地址表(scapy或 python-nmap)

    目录 用scapy模块写 用 python-nmap 模块写 python3.7  windows环境 以下两个都可以扫描指定主机或者指定网段的 IP 对应的 MAC 地址,然后保存到 csv 文件中 ...

  8. ERROR: Symbol file could not be found 寒江孤钓<<windows 内核安全编程>> 学习笔记

    手动下载了Symbols,设置好了Symbols File Path,串口连接上了以后,出现ERROR: Symbol file could not be found, 并且会一直不停的出现windb ...

  9. Thinking in UML 笔记(一) -- 面向对象

    一.UML 中最重要的就是面向对象. 面向对象的认识论可以构建更为复杂的系统来解释复杂的世界. 1. 面向过程,一切都是相互紧密地联系在一起,互相作用,互相影响. 2.面向对象, 世界是分割开的,只有 ...

  10. Redis数据结构—跳跃表

    目录 Redis数据结构-跳跃表 跳跃表产生的背景 跳跃表的结构 利用跳跃表查询有序链表 Redis跳跃表图示 Redis跳跃表数据结构 小结 Redis数据结构-跳跃表 大家好,我是白泽,最近学校有 ...