-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredis_test.py
More file actions
62 lines (50 loc) · 1.57 KB
/
Copy pathredis_test.py
File metadata and controls
62 lines (50 loc) · 1.57 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
# -*- coding:utf-8 -*-
__author__ = 'Jevade'
__data__ = '2018/8/6 21:29'
import redis
def singleton(cls):
instance = {}
def _singleton(*args, **kw):
if cls not in instance:
instance[cls] = cls(*args,**kw)
return instance[cls]
return _singleton
@singleton
class RedisPool():
'''提供Redis连接池'''
host = 'localhost'
port = 6379
pool = None
conn = None
def __init__(self,host=host,port=port):
self.pool = redis.ConnectionPool(host=host,port=port,decode_responses=True)
def __enter__(self):
return self.get_connect()
def get_connect(self):
'''获取Redis链接'''
return redis.Redis(connection_pool=self.pool)
def __exit__(self, exc_type, exc_val, exc_tb):
pass
class Templist(object):
def __enter__(self):
print("enter the class instance")
return self
def do_something(self):
print('I am doing some things.')
print(1//0)
def __exit__(self, exc_type, exc_val, exc_tb):
print("type: ", exc_type)
print("val: ", exc_val)
print("tb: ", exc_tb)
if __name__=="__main__":
with RedisPool() as redis_conn:
redis_conn.delete('liu')
redis_conn.set('liu', 'jiawei')
redis_conn.set('c', 123)
redis_conn.set('array', [12,34,56,77],)
print(redis_conn.get('liu'))
redis_pool2 = RedisPool()
redis_conn.hset(name="info",key='c', value=123)
print(redis_conn.hgetall(name="info"))
with Templist() as templist:
templist.do_something()