comparison env/lib/python3.7/site-packages/cachecontrol/caches/redis_cache.py @ 0:26e78fe6e8c4 draft

"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
author shellac
date Sat, 02 May 2020 07:14:21 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:26e78fe6e8c4
1 from __future__ import division
2
3 from datetime import datetime
4
5
6 def total_seconds(td):
7 """Python 2.6 compatability"""
8 if hasattr(td, 'total_seconds'):
9 return td.total_seconds()
10
11 ms = td.microseconds
12 secs = (td.seconds + td.days * 24 * 3600)
13 return (ms + secs * 10**6) / 10**6
14
15
16 class RedisCache(object):
17
18 def __init__(self, conn):
19 self.conn = conn
20
21 def get(self, key):
22 return self.conn.get(key)
23
24 def set(self, key, value, expires=None):
25 if not expires:
26 self.conn.set(key, value)
27 else:
28 expires = expires - datetime.now()
29 self.conn.setex(key, total_seconds(expires), value)
30
31 def delete(self, key):
32 self.conn.delete(key)
33
34 def clear(self):
35 """Helper for clearing all the keys in a database. Use with
36 caution!"""
37 for key in self.conn.keys():
38 self.conn.delete(key)
39
40 def close(self):
41 self.conn.disconnect()