Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/pip/_vendor/cachecontrol/cache.py: 52%
29 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:48 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:48 +0000
1# SPDX-FileCopyrightText: 2015 Eric Larson
2#
3# SPDX-License-Identifier: Apache-2.0
5"""
6The cache object API for implementing caches. The default is a thread
7safe in-memory dictionary.
8"""
9from threading import Lock
12class BaseCache(object):
14 def get(self, key):
15 raise NotImplementedError()
17 def set(self, key, value, expires=None):
18 raise NotImplementedError()
20 def delete(self, key):
21 raise NotImplementedError()
23 def close(self):
24 pass
27class DictCache(BaseCache):
29 def __init__(self, init_dict=None):
30 self.lock = Lock()
31 self.data = init_dict or {}
33 def get(self, key):
34 return self.data.get(key, None)
36 def set(self, key, value, expires=None):
37 with self.lock:
38 self.data.update({key: value})
40 def delete(self, key):
41 with self.lock:
42 if key in self.data:
43 self.data.pop(key)
46class SeparateBodyBaseCache(BaseCache):
47 """
48 In this variant, the body is not stored mixed in with the metadata, but is
49 passed in (as a bytes-like object) in a separate call to ``set_body()``.
51 That is, the expected interaction pattern is::
53 cache.set(key, serialized_metadata)
54 cache.set_body(key)
56 Similarly, the body should be loaded separately via ``get_body()``.
57 """
58 def set_body(self, key, body):
59 raise NotImplementedError()
61 def get_body(self, key):
62 """
63 Return the body as file-like object.
64 """
65 raise NotImplementedError()