常用操作

  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. SpringIOC框架简单实现(注解实现)

    SpringIOC框架简单实现(注解实现) 前情回顾 SpringIOE简单介绍 运用注解的方式来实现IOC 首先,让我们来创建一个Dog类 @Component("dog")// ...

  2. 阿里云《nginx服务器配置SSL证书》 配置参数

    server { listen 443; server_name demo.shengruijt25.com; ssl on; root html; index index.html index.ht ...

  3. 【Navicat】获取表结构的DDL语句以及获取更新表字段的操作的DDL

    1.获取表结构的DDL语句 2.获取修改表结构某一字段的DDL语句  设计表-修改表字段(记住不要保存)-SQL预览

  4. POJ2186 强联通

    题意:       有一群老牛,给你一些关系,a b表示牛a仰慕牛b,最后问你有多少个牛是被所有牛仰慕的. 思路:       假如这些仰慕关系不会出现环,那么当且仅当只有一只牛的出度为0的时候答案才 ...

  5. POJ3189二分最大流(枚举下界,二分宽度,最大流判断可行性)

    题意:       有n头猪,m个猪圈,每个猪圈都有一定的容量(就是最多能装多少只猪),然后每只猪对每个猪圈的喜好度不同(就是所有猪圈在每个猪心中都有一个排名),然后要求所有的猪都进猪圈,但是要求所有 ...

  6. XCTF-unfinish

    unfinish 之前做过这个题,这是之前写的WP:链接

  7. MySQL锁等待与死锁问题分析

    前言: 在 MySQL 运维过程中,锁等待和死锁问题是令各位 DBA 及开发同学非常头痛的事.出现此类问题会造成业务回滚.卡顿等故障,特别是业务繁忙的系统,出现死锁问题后影响会更严重.本篇文章我们一起 ...

  8. Mac OSX系统homebrew update Fetching failed问题解决方案

    1. brew update error (i) 问题出现及现象描述 昨天换了台电脑,有些软件需要重新安装或更新一下,遇到了下面的问题 cv@xys-MacBook-Pro ~ % brew upda ...

  9. 【秒懂音视频开发】23_H.264编码

    本文主要介绍一种非常流行的视频编码:H.264. 计算一下:10秒钟1080p(1920x1080).30fps的YUV420P原始视频,需要占用多大的存储空间? (10 * 30) * (1920 ...

  10. ES6对象的新增方法的使用

    Object.assign Object Object.assign(target, ...sources) 将所有可枚举属性的值从一个或多个源对象复制到目标对象 参数: target 目标对象 so ...