You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

redis_cache.py 1.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. from __future__ import division
  2. from datetime import datetime
  3. from pip._vendor.cachecontrol.cache import BaseCache
  4. def total_seconds(td):
  5. """Python 2.6 compatability"""
  6. if hasattr(td, 'total_seconds'):
  7. return int(td.total_seconds())
  8. ms = td.microseconds
  9. secs = (td.seconds + td.days * 24 * 3600)
  10. return int((ms + secs * 10**6) / 10**6)
  11. class RedisCache(BaseCache):
  12. def __init__(self, conn):
  13. self.conn = conn
  14. def get(self, key):
  15. return self.conn.get(key)
  16. def set(self, key, value, expires=None):
  17. if not expires:
  18. self.conn.set(key, value)
  19. else:
  20. expires = expires - datetime.utcnow()
  21. self.conn.setex(key, total_seconds(expires), value)
  22. def delete(self, key):
  23. self.conn.delete(key)
  24. def clear(self):
  25. """Helper for clearing all the keys in a database. Use with
  26. caution!"""
  27. for key in self.conn.keys():
  28. self.conn.delete(key)
  29. def close(self):
  30. """Redis uses connection pooling, no need to close the connection."""
  31. pass