redis python操作
1.基于连接池方式实现对五个数据类型操作,每种数据类型2个操作
2.基于spring-data-redis 基于jedis来实现对五种数据类型操作,每种数据类型实现两个操作,包括事务
以上为基于java语言开发
学员可以采用其它语言进行开发,要求如下:
1.总结你选择的框架的优缺点
2.五种数据类型基本操作,每种数据类型完成3个操作,额外增加事务的实现
3.必须把测试执行成功的效果图截图展示
一.windows+pycharm
1.windows导入python的redis包
C:\Users\Administrator>easy_install redis
Searching for redis
Reading https://pypi.python.org/simple/redis/
d:\python\python36\lib\site-packages\setuptools\pep425tags.py:89: RuntimeWarning: Config variable 'Py_DEBUG' is unset, Python ABI tag may be incorrect
warn=(impl == 'cp')):
d:\python\python36\lib\site-packages\setuptools\pep425tags.py:93: RuntimeWarning: Config variable 'WITH_PYMALLOC' is unset, Python ABI tag may be incorrect
warn=(impl == 'cp')):
Downloading https://files.pythonhosted.org/packages/3b/f6/7a76333cf0b9251ecf49efff635015171843d9b977e4ffcf59f9c4428052/redis-2.10.6-py2.py3-none-any.whl#sha256=8a1900a9f2a0a44ecf6e8b5eb3e967a9909dfed219ad66df094f27f7d6f330fb
Best match: redis 2.10.6
Processing redis-2.10.6-py2.py3-none-any.whl
Installing redis-2.10.6-py2.py3-none-any.whl to d:\python\python36\lib\site-packages
Adding redis 2.10.6 to easy-install.pth file
Installed d:\python\python36\lib\site-packages\redis-2.10.6-py3.6.egg
Processing dependencies for redis
Finished processing dependencies for redis
导入集群包
C:\Users\Administrator>easy_install redis-py-cluster
Searching for redis-py-cluster
Reading https://pypi.python.org/simple/redis-py-cluster/
d:\python\python36\lib\site-packages\setuptools\pep425tags.py:89: RuntimeWarning: Config variable 'Py_DEBUG' is unset, Python ABI tag may be incorrect
warn=(impl == 'cp')):
d:\python\python36\lib\site-packages\setuptools\pep425tags.py:93: RuntimeWarning: Config variable 'WITH_PYMALLOC' is unset, Python ABI tag may be incorrect
warn=(impl == 'cp')):
Downloading https://files.pythonhosted.org/packages/f1/dd/4bb27bb3e3d03a01b0afd4a4ba13a4677b0f2d6552ff2841ac56591bfb29/redis-py-cluster-1.3.5.tar.gz#sha256=e7349b1f2487d2c763088d81162e4a2ed91adee975c296bdba2ed96fd7450b36
Best match: redis-py-cluster 1.3.5
Processing redis-py-cluster-1.3.5.tar.gz
Writing C:\Users\ADMINI~1\AppData\Local\Temp\easy_install-q542vr0p\redis-py-cluster-1.3.5\setup.cfg
Running redis-py-cluster-1.3.5\setup.py -q bdist_egg --dist-dir C:\Users\ADMINI~1\AppData\Local\Temp\easy_install-q542vr0p\redis-py-cluster-1.3.5\egg-dist-tmp-dn6h7_rs
warning: no files found matching 'CHANGES'
zip_safe flag not set; analyzing archive contents...
Copying redis_py_cluster-1.3.5-py3.6.egg to d:\python\python36\lib\site-packages
Adding redis-py-cluster 1.3.5 to easy-install.pth file
Installed d:\python\python36\lib\site-packages\redis_py_cluster-1.3.5-py3.6.egg
Processing dependencies for redis-py-cluster
Finished processing dependencies for redis-py-cluster
2.安装hiredis
C:\Users\Administrator>easy_install hiredis
Searching for hiredis
Reading https://pypi.python.org/simple/hiredis/
d:\python\python36\lib\site-packages\setuptools\pep425tags.py:89: RuntimeWarning: Config variable 'Py_DEBUG' is unset, Python ABI tag may be incorrect
warn=(impl == 'cp')):
d:\python\python36\lib\site-packages\setuptools\pep425tags.py:93: RuntimeWarning: Config variable 'WITH_PYMALLOC' is unset, Python ABI tag may be incorrect
warn=(impl == 'cp')):
Downloading https://files.pythonhosted.org/packages/1b/98/4766d85124b785ff1989ee1c79631a1b6ecfcb444ff39999a87877b2027e/hiredis-0.2.0.tar.gz#sha256=ca958e13128e49674aa4a96f02746f5de5973f39b57297b84d59fd44d314d5b5
Best match: hiredis 0.2.0
Processing hiredis-0.2.0.tar.gz
Writing C:\Users\ADMINI~1\AppData\Local\Temp\easy_install-sjunzzxy\hiredis-0.2.0\setup.cfg
Running hiredis-0.2.0\setup.py -q bdist_egg --dist-dir C:\Users\ADMINI~1\AppData\Local\Temp\easy_install-sjunzzxy\hiredis-0.2.0\egg-dist-tmp-6pm9mu31
warning: no previously-included files found matching 'vendor\hiredis\example*'
warning: no previously-included files found matching 'vendor\hiredis\text*'
error: Setup script exited with error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools
http://www.liangchan.net/soft/download.asp?softid=10220&downid=8&id=10288
C:\Users\Administrator>pip install hiredis
Collecting hiredis
Using cached https://files.pythonhosted.org/packages/1b/98/4766d85124b785ff1989ee1c79631a1b6ecfcb444ff39999a87877b2027e/hiredis-0.2.0.tar.gz
Installing collected packages: hiredis
Running setup.py install for hiredis ... done
Successfully installed hiredis-0.2.0
3.更新pip
python -m pip install --upgrade pip
C:\Users\Administrator>python -m pip install --upgrade pip
Collecting pip
Downloading https://files.pythonhosted.org/packages/5f/25/e52d3f31441505a5f3af41213346e5b6c221c9e086a166f3703d2ddaf940/pip-18.0-py2.py3-none-any.whl (1.3MB)
100% |████████████████████████████████| 1.3MB 228kB/s
Installing collected packages: pip
Found existing installation: pip 9.0.3
Uninstalling pip-9.0.3:
Successfully uninstalled pip-9.0.3
Successfully installed pip-18.0
二.python增删改查
2.1普通string值增删改查
# /usr/bin/env python
# -*- coding: utf-8 -*-
# Author:jenvid.yang
import hiredis
import redis
rds=redis.StrictRedis(host='172.16.10.142',port=6379,password='foobared')
rds.set('k1','v1')
rds.set('k2','v2')
k1=rds.get('k1')
k2=rds.get('k2')
rds.mset({'k3':'v3','k4':'v4'})
rds.set('k5','123456')
print(k1);
print(rds.mget('k1','k2','k3','k4'));
print(rds.getrange('k5',0,2))
rds.delete('k1')
print(rds.get('k1'))
#
D:\Python\Python36\python.exe D:/04python/01_course/02redis/01redis_operate.py
b'v1'
[b'v1', b'v2', b'v3', b'v4']
b'123'
None
2.2hash
import redis
rds=redis.StrictRedis(host='172.16.10.142',port=6379,password='foobared')
rds.hset("h1_name","a1","aa")
print(rds.hget("h1_name","a1"))
print(rds.hgetall("h1_name"))
dict1={"d1":"v1","d2":"v2"}
rds.hmset("hd",dict1)
print(rds.hgetall("hd"))
print(rds.hlen("hd"))
#
b'aa'
{b'a1': b'aa'}
{b'd1': b'v1', b'd2': b'v2'}
2
2.3list
# /usr/bin/env python
# -*- coding: utf-8 -*-
# Author:jenvid.yang
import redis
rds=redis.StrictRedis(host="172.16.10.142",password="foobared",port=6379)
rds.lpush('l1',2)
rds.lpush('l1',3,4,5)
rds.rpush('l2',1)
rds.rpush('l2',2,3,4)
print(rds.lrange('l1',0,-1))
print(rds.lrange('l2',0,-1))
print(rds.llen('l1'))
rds.delete("l1","l2")
# print(rds.lrange('l1',0,-1))
# print(rds.lrange('l2',0,-1))
#
[b'5', b'4', b'3', b'2']
[b'1', b'2', b'3', b'4']
4
2.4set和zset
# /usr/bin/env python
# -*- coding: utf-8 -*-
# Author:jenvid.yang
import redis
rds=redis.StrictRedis(host='172.16.10.142',password='foobared',port=6379)
rds.sadd('sset1',1)
print(rds.smembers('sset1'))
rds.sadd('sset1',1,1,2,3)
print(rds.smembers('sset1'))
print(rds.scard('sset1'))
rds.delete('sset1')
rds.zadd('zset2',k1=2,k2=5,k3=3)
print(rds.zrange('zset2',0,-1))
print(rds.zcount('zset2',1,2))
#
{b'1'}
{b'3', b'1', b'2'}
3
[b'k1', b'k3', b'k2']
1
2.5事务
# /usr/bin/env python
# -*- coding: utf-8 -*-
# Author:jenvid.yang
import redis
import time
from concurrent.futures import ProcessPoolExecutor
r=redis.StrictRedis(host='172.16.10.142',password='foobared',port=6379)
def try_pipeline():
start = time.time()
with r.pipeline(transaction=False) as p:
p.sadd('seta', 1).sadd('seta', 2).srem('seta', 2).lpush('lista', 1).lrange('lista', 0, -1)
p.execute()
print(r.smembers('seta'))
print(time.time() - start)
try_pipeline()
#
D:\Python\Python36\python.exe D:/04python/01_course/02redis/redisset.py
{b'1'}
0.017011165618896484
# type key 查看类型
python连接redis cluster
from rediscluster import StrictRedisCluster
import sys
def redis_cluster():
redis_nodes = [{'host':'192.168.222.66','port':6378},
{'host':'192.168.222.66','port':6380},
{'host':'192.168.222.66','port':6381},
{'host':'192.168.222.66','port':6382},
{'host':'192.168.222.66','port':6383},
{'host':'192.168.222.66','port':6384},
{'host':'192.168.222.66','port':6385}
]
try:
redisconn = StrictRedisCluster(startup_nodes=redis_nodes)
except Exception,e:
print "Connect Error!"
sys.exit(1)
redisconn.set('name','admin')
redisconn.set('age',18)
print "name is: ", redisconn.get('name')
print "age is: ", redisconn.get('age')
redis_cluster()
redis python操作的更多相关文章
- Redis - Python操作Redis
目录 Python操作Redis 一. Redis安装和基本使用 二. Python操作Redis API使用 1.操作模式 2.连接池 3.Django配置Redis 4.操作 Python操作Re ...
- redis python 操作 Python操作Redis数据库
原文章于此:https://www.cnblogs.com/cnkai/p/7642787.html 有个人修改与改正 Python操作Redis数据库 连接数据库 StrictRedisfrom ...
- redis -- python操作连接redis简单示例
1.先安装 redis,pyredis sudo pip install redis sudo pip install python-redis 2.示例: importredis >>& ...
- [ecmagent][redis学习][1初识redis] python操作redis
#1 连接redis # 连接redis -- import redis -- 使用端口连接redis conn = redis.Redis(host=) -- 使用套接字连接 r = redis.R ...
- redis python操作api
redis单例数据库 #redis单例连接 ''' redis-server redis_diy.conf ''' import redis conn=redis.StrictRedis(host=' ...
- python运维开发(十一)----python操作缓存memcache、redis
内容目录: 缓存 memcache redis memcache Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数 ...
- Python 操作redis 常用方法
Python 操作redis 1.字符串 #!/usr/bin/env python # -*- coding:utf-8 -*- import redis # python 操作str class ...
- python 操作redis之——HyperLogLog (八)
#coding:utf8 import redis # python 操作redis之——HyperLogLog r =redis.Redis(host=") # 1.Pfadd 命令将所有 ...
- python操作Redis方法速记
redis有5种数据结构,分别如下: 5种数据结构 python语言对5种数据结构的增删改查 全局函数 redis 连接 import redis pool = redis.ConnectionPoo ...
随机推荐
- Django新手十个开发指导
下面是关于Django新手开发中的一些建议,大家可以参考一下~~ 1,不要将项目名称包含在引用代码里 比如你创建了一个名为"project"的项目,包含一个名为"app& ...
- 徐州赛区网络预赛 D Easy Math
比赛快结束的适合看了一下D题,发现跟前几天刚刚做过的HDU 5728 PowMod几乎一模一样,当时特兴奋,结果一直到比赛结束都一直WA.回来仔细一琢磨才发现,PowMod这道题保证了n不含平方因子, ...
- 用Swoole4 打造高并发的PHP协程Mysql连接池
码云代码仓库:https://gitee.com/tanjiajun/MysqlPool 代码仓库:https://github.com/asbectJ/swoole4.git 前言 在写这篇文章之前 ...
- 小老板,我学的计算机组成原理告诉我半导体存储器都是断电后丢失的,为什么U盘SSD(固态硬盘)没事呢?
什么是闪存: 快闪存储器(英语:flash memory),是一种电子式可清除程序化只读存储器的形式,允许在操作中被多次擦或写的存储器 存储原理 要讲解闪存的存储原理,还是要从EPROM和EEPROM ...
- Codeforce-Ozon Tech Challenge 2020-A. Kuroni and the Gifts
the i-th necklace has a brightness ai, where all the ai are pairwise distinct (i.e. all ai are diffe ...
- 我想solo自己一个人!
区域赛之后你就该走了,现在你告诉我,没精力不打了,我真谢谢你! 今年就TM的没有一点舒心的地方! 父母分居, 队友出走, 队伍解散, 白天家里两个外甥很吵, 鼻窦炎复发, 喜欢的妹子也追不到, 整夜失 ...
- 2019 ICPC 南京网络赛 F Greedy Sequence
You're given a permutation aa of length nn (1 \le n \le 10^51≤n≤105). For each i \in [1,n]i∈[1,n], c ...
- Redis集群搭建的三种方式
一.Redis主从 1.1 Redis主从原理 和MySQL需要主从复制的原因一样,Redis虽然读取写入的速度都特别快,但是也会产生性能瓶颈,特别是在读压力上,为了分担压力,Redis支持主从复制. ...
- TestNG测试用例重跑详解及实践优化
测试用例运行稳定性是自动化质量的一个重要指标,在运行中需要尽可能的剔除非bug造成的测试用例执行失败,对于失败用例进行重跑是常用策略之一.一种重跑策略是所有用例运行结束后对失败用例重跑,另一种重跑策略 ...
- JS 究竟是先有鸡还是有蛋,Object与Function究竟谁出现的更早,Function算不算Function的实例等问题杂谈
壹 ❀ 引 我在JS 疫情宅在家,学习不能停,七千字长文助你彻底弄懂原型与原型链一文中介绍了JavaScript原型与原型链,以及衍生的__proto__.constructor等一系列属性.在解答了 ...