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()
posted @ 2018-09-05 13:36  Jenvid  阅读(393)  评论(0编辑  收藏  举报