Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/pip/_vendor/cachecontrol/cache.py: 55%

33 statements  

« prev     ^ index     » next       coverage.py v7.4.3, created at 2024-02-26 06:33 +0000

1# SPDX-FileCopyrightText: 2015 Eric Larson 

2# 

3# SPDX-License-Identifier: Apache-2.0 

4 

5""" 

6The cache object API for implementing caches. The default is a thread 

7safe in-memory dictionary. 

8""" 

9from __future__ import annotations 

10 

11from threading import Lock 

12from typing import IO, TYPE_CHECKING, MutableMapping 

13 

14if TYPE_CHECKING: 

15 from datetime import datetime 

16 

17 

18class BaseCache: 

19 def get(self, key: str) -> bytes | None: 

20 raise NotImplementedError() 

21 

22 def set( 

23 self, key: str, value: bytes, expires: int | datetime | None = None 

24 ) -> None: 

25 raise NotImplementedError() 

26 

27 def delete(self, key: str) -> None: 

28 raise NotImplementedError() 

29 

30 def close(self) -> None: 

31 pass 

32 

33 

34class DictCache(BaseCache): 

35 def __init__(self, init_dict: MutableMapping[str, bytes] | None = None) -> None: 

36 self.lock = Lock() 

37 self.data = init_dict or {} 

38 

39 def get(self, key: str) -> bytes | None: 

40 return self.data.get(key, None) 

41 

42 def set( 

43 self, key: str, value: bytes, expires: int | datetime | None = None 

44 ) -> None: 

45 with self.lock: 

46 self.data.update({key: value}) 

47 

48 def delete(self, key: str) -> None: 

49 with self.lock: 

50 if key in self.data: 

51 self.data.pop(key) 

52 

53 

54class SeparateBodyBaseCache(BaseCache): 

55 """ 

56 In this variant, the body is not stored mixed in with the metadata, but is 

57 passed in (as a bytes-like object) in a separate call to ``set_body()``. 

58 

59 That is, the expected interaction pattern is:: 

60 

61 cache.set(key, serialized_metadata) 

62 cache.set_body(key) 

63 

64 Similarly, the body should be loaded separately via ``get_body()``. 

65 """ 

66 

67 def set_body(self, key: str, body: bytes) -> None: 

68 raise NotImplementedError() 

69 

70 def get_body(self, key: str) -> IO[bytes] | None: 

71 """ 

72 Return the body as file-like object. 

73 """ 

74 raise NotImplementedError()