开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (1)
master
master
分支 (1)
master
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (1)
master
Python
/
Python27
/
操作redis.py
Python
/
Python27
/
操作redis.py
操作redis.py 9.78 KB
一键复制 编辑 原始数据 按行查看 历史
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
# -*- coding:UTF-8 -*-
import redis
import time
class TestRedis:
def __init__(self):
self.dbconn = None
def openRedis(self):
# 连接redis,加上decode_responses=True,写入的键值对中的value为str类型,不加这个参数写入的则为字节类型。
# r = redis.Redis(host=u'127.0.0.1', port=6379, db=0)
pool = redis.ConnectionPool(host='127.0.0.1', port=6379, db=0, decode_responses=True) # 使用连接池
self.dbconn = redis.Redis(connection_pool=pool)
def addKey(self, Key, Value):
self.dbconn.set(Key, Value)
def getKey(self, Key):
return self.dbconn.get(Key)
def setObj(self, name, value, ex=None, px=None, nx=False, xx=False):
# ex,过期时间(秒)
# px,过期时间(毫秒)
# nx,如果设置为True,则只有name不存在时,当前set操作才执行
# xx,如果设置为True,则只有name存在时,当前set操作才执行
self.dbconn.set(name, value, ex=ex, px=px, nx=nx, xx=xx)
pass
def exAndpxDemo(self):
# 演示过期时间, ex :秒 , px : 毫秒
self.setObj("apple", "一个", ex=2)
print self.getKey("apple")
time.sleep(3)
print self.getKey("apple")
print "======================"
self.setObj("banana", "two", px=2)
print self.getKey("banana")
time.sleep(0.3)
print self.getKey("banana")
def nxAandxxDemo(self):
# nx,如果设置为True,则只有name不存在时,当前set操作才执行 (新建)
# xx,如果设置为True,则只有name存在时,当前set操作才执行 (修改)
self.addKey('grape', 'zhangsan')
self.setObj("grape", "一个", nx=True)
self.setObj("grape2", "一个2", nx=True)
print self.getKey("grape") # 此时是 zhangsan 并没有改为 一个
print self.getKey("grape2") # 此时是 一个2
print "========================"
self.setObj("grape2", "修改了", xx=True) # grape2已经存在
print self.getKey("grape2") # 此时是 修改了
self.setObj("grape3", "修改了", xx=True) # grape3不存在 不会创建
print self.getKey("grape3") # None
def listGet(self):
# 将目前redis缓存中的键对应的值批量取出来
print(self.dbconn.mget("grape", 'grape2'))
print(self.dbconn.mget(['grape', 'grape2']))
def getset(self):
# 设置新值并获取原来的值
print(self.dbconn.getset("grape", "aaa"))
print(self.dbconn.getset("grape", "bbb"))
def listSet(self):
self.dbconn.mget({'k1': 'v1', 'k2': 'v2'})
self.dbconn.mset(k1="v1", k2="v2") # 这里k1 和k2 不能带引号 一次设置对个键值对
opt = {
"one": "aaa",
"two": "bbb",
"three": "ccc",
4: 4
}
self.dbconn.mset(opt)
print(self.dbconn.mget("k1", "k2")) # 一次取出多个键对应的值
print(self.dbconn.mget("one", "two", "three", 4))
def addClick(self):
self.dbconn.set("foo", 123)
print(self.dbconn.mget("foo", "foo1", "foo2", "k1", "k2"))
self.dbconn.incr("foo") # 124
self.dbconn.incr("foo") # 125
self.dbconn.decr("foo") # 自减 124
print(self.dbconn.mget("foo", "foo1", "foo2", "k1", "k2"))
self.dbconn.set("foo1", "123.0")
# 浮点型
self.dbconn.set("foo1", "123.0")
self.dbconn.set("foo2", "221.0")
print(self.dbconn.mget("foo1", "foo2"))
self.dbconn.incrbyfloat("foo1", amount=2.0)
self.dbconn.incrbyfloat("foo2", amount=3.0)
print(self.dbconn.mget("foo1", "foo2"))
def hash(self):
opt = {
"sasa": {
"v1": "1a",
"v2": "2a"
}
}
opts = {
"v1": "1a",
"v2": "2a"
}
self.dbconn.hset("hash1", "k2", "v2")
self.dbconn.hmset("hash1", opt)
self.dbconn.hmset("hash1", opts)
# print(self.dbconn.hkeys("hash1")) # 取hash中所有的key
# print(self.dbconn.hget("hash1", "k1")) # 单个取hash的key对应的值
# print(self.dbconn.hmget("hash1", "k1", "v3")) # 多个取hash的key对应的值
self.dbconn.hsetnx("hash1", "k3", "v3") # 只能新建
# print(self.dbconn.hkeys("hash1")) # 取hash中所有的key
print(self.dbconn.hget("hash1","k2")) # 单个取出"hash2"的key-k2对应的value
print(self.dbconn.hmget("hash1", "k2", "k3")) # 批量取出"hash2"的key-k2 k3对应的value --方式1
print(self.dbconn.hmget("hash1", ["k2", "k3"])) # 批量取出"hash2"的key-k2 k3对应的value --方式2
print(self.dbconn.hgetall("hash1")) # 获取name对应hash的所有键值
print(self.dbconn.hlen("hash1")) # 获取name对应的hash中键值对的个数
print(self.dbconn.hkeys("hash1")) # 获取name对应的hash中所有的key的值
print(self.dbconn.hvals("hash1")) # 获取name对应的hash中所有的value的值
#检查name对应的hash是否存在当前传入的key
print(self.dbconn.hexists("hash1", "k4")) # False 不存在
print(self.dbconn.hexists("hash1", "k1")) # True 存在
#删除键值对
print(self.dbconn.hgetall("hash1"))
self.dbconn.hset("hash1", "k1","v222") # 修改已有的key k2
self.dbconn.hset("hash1", "k11", "v1") # 新增键值对 k11
self.dbconn.hdel("hash1", "k11") # 删除一个键值对
print(self.dbconn.hgetall("hash1"))
# 自增自减整数(将key对应的value - -整数
# 自增1或者2,或者别的整数
# 负数就是自减)
# hincrby(name, key, amount=1)
# 自增name对应的hash中的指定key的值,不存在则创建key = amount
# 参数:
# name,redis中的name
# key, hash对应的key
# amount,自增数(整数)
self.dbconn.hset("hash1", "k3", 123)
self.dbconn.hincrby("hash1", "k3", amount=-1)
# print(self.dbconn.hgetall("hash1"))
print(self.dbconn.hget("hash1", "k3")) # 单个取出"hash2"的key-k3对应的value
self.dbconn.hincrby("hash1", "k4", amount=1) # 不存在的话,value默认就是1 print(r.hgetall("hash1"))
print(self.dbconn.hget("hash1", "k4")) # 单个取出"hash2"的key-k3对应的value
#浮点数
self.dbconn.hset("hash1", "k5", "1.0")
self.dbconn.hincrbyfloat("hash1", "k5",amount=-1.0) # 已经存在,递减-1.0
self.dbconn.hincrbyfloat("hash1", "k6", amount=-1.0) # 不存在,value初始值是-1.0 每次递减1.0
# 取值查看 - -分片读取
# hscan(name, cursor=0, match=None, count=None)
# 增量式迭代获取,对于数据大的数据非常有用,hscan可以实现分片的获取数据,并非一次性将数据全部获取完,从而放置内存被撑爆
# 参数:
# name,redis的name
# cursor,游标(基于游标分批取获取数据)
# match,匹配指定key,默认None
# 表示所有的key
# count,每次分片最少获取个数,默认None表示采用Redis的默认分片个数
# 如:
# 第一次:cursor1, data1 = r.hscan('xx', cursor=0, match=None, count=None)
# 第二次:cursor2, data1 = r.hscan('xx', cursor=cursor1, match=None, count=None)
# 直到返回值cursor的值为0时,表示数据已经通过分片获取完毕
print(self.dbconn.hscan("hash1"))
# hscan_iter(name, match=None, count=None)
# 利用yield封装hscan创建生成器,实现分批去redis中获取数据
# 参数:
# match,匹配指定key,默认None
# 表示所有的key
# count,每次分片最少获取个数,默认None表示采用Redis的默认分片个数
# 如:
for item in self.dbconn.hscan_iter('hash1',match=None, count=None):
print(item)
print(self.dbconn.hscan_iter("hash1")) # 生成器内存地址
def redisList(self):
# self.dbconn.lpush("list1", 44, 55, 66) # 表示从左向右操作
# print(self.dbconn.lrange('list1', 0, -1)) #切片取出值,范围是索引号0到-1(最后一个元素)
# self.dbconn.rpush("list2", 44, 55, 66) # 表示从右向左操作
# print(self.dbconn.llen("list2")) # 列表长度
print(self.dbconn.lrange("list2", 0, -1)) # 切片取出值,范围是索引号0-3
# 往已经有的name的列表的左边添加元素,没有的话无法创建
# lpushx(name, value)
# 在name对应的list中添加元素,只有name已经存在时,值添加到列表的最左边
def main(self):
r = self.openRedis()
# self.dbconn.append("name", "haha") # 在name对应的值junxi后面追加字符串haha
# print(self.dbconn.mget("name"))
self.addKey('grape', 'zhangsans') # 添加
# print self.getKey('grape') # 获取
# self.exAndpxDemo()
# self.nxAandxxDemo()
# self.listGet()
# self.getset() #设置新值并获取原来的值
# self.listSet() #批量设置值
# self.addClick() #自增自减
# redis基本命令 hash
# self.hash()
#redis基本命令 list
self.redisList()
# 将目前redis缓存中的键对应的值批量取出来
if __name__ == '__main__':
# r = redis.Redis(host='127.0.0.1', port=6379)
# r.flushdb()
# print r.dbsize()
# r.set('foo', 'Bar')manager
# print r.get('foo')https://gitee.com/DanBrown/Ordermeal.git
testredis = TestRedis()
testredis.main()
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

从零学Python,各种开发案例,不定期更新。
暂无标签
GPL-3.0
使用 GPL-3.0 开源许可协议
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/chaojunjiang/Python.git
git@gitee.com:chaojunjiang/Python.git
chaojunjiang
Python
从零学Python
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /