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.

39 lines
805 B

4 years ago
  1. """
  2. The cache object API for implementing caches. The default is a thread
  3. safe in-memory dictionary.
  4. """
  5. from threading import Lock
  6. class BaseCache(object):
  7. def get(self, key):
  8. raise NotImplementedError()
  9. def set(self, key, value):
  10. raise NotImplementedError()
  11. def delete(self, key):
  12. raise NotImplementedError()
  13. def close(self):
  14. pass
  15. class DictCache(BaseCache):
  16. def __init__(self, init_dict=None):
  17. self.lock = Lock()
  18. self.data = init_dict or {}
  19. def get(self, key):
  20. return self.data.get(key, None)
  21. def set(self, key, value):
  22. with self.lock:
  23. self.data.update({key: value})
  24. def delete(self, key):
  25. with self.lock:
  26. if key in self.data:
  27. self.data.pop(key)