2cba4b0708032d62b4c1278f99e5db87ed8d90fe
[SubU] /
1 # SPDX-FileCopyrightText: 2015 Eric Larson
2 #
3 # SPDX-License-Identifier: Apache-2.0
4
5 from __future__ import division
6
7 from datetime import datetime
8 from pip._vendor.cachecontrol.cache import BaseCache
9
10
11 class RedisCache(BaseCache):
12
13     def __init__(self, conn):
14         self.conn = conn
15
16     def get(self, key):
17         return self.conn.get(key)
18
19     def set(self, key, value, expires=None):
20         if not expires:
21             self.conn.set(key, value)
22         elif isinstance(expires, datetime):
23             expires = expires - datetime.utcnow()
24             self.conn.setex(key, int(expires.total_seconds()), value)
25         else:
26             self.conn.setex(key, expires, value)
27
28     def delete(self, key):
29         self.conn.delete(key)
30
31     def clear(self):
32         """Helper for clearing all the keys in a database. Use with
33         caution!"""
34         for key in self.conn.keys():
35             self.conn.delete(key)
36
37     def close(self):
38         """Redis uses connection pooling, no need to close the connection."""
39         pass