-
Notifications
You must be signed in to change notification settings - Fork 1
/
meta_commands.py
114 lines (107 loc) · 3.49 KB
/
meta_commands.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
from typing import Any, Dict, List, Optional
from meta_memcache.errors import MemcacheError
from meta_memcache.interfaces.router import (
DEFAULT_FAILURE_HANDLING,
FailureHandling,
HasRouter,
)
from meta_memcache.protocol import (
Conflict,
Key,
MetaCommand,
Miss,
NotStored,
ReadResponse,
RequestFlags,
Success,
Value,
ValueContainer,
WriteResponse,
)
class MetaCommandsMixin:
def meta_multiget(
self: HasRouter,
keys: List[Key],
flags: Optional[RequestFlags] = None,
failure_handling: FailureHandling = DEFAULT_FAILURE_HANDLING,
) -> Dict[Key, ReadResponse]:
results: Dict[Key, ReadResponse] = {}
for key, result in self.router.exec_multi(
command=MetaCommand.META_GET,
keys=keys,
flags=flags,
failure_handling=failure_handling,
).items():
if not isinstance(result, (Miss, Value, Success)):
raise MemcacheError(
f"Unexpected response for Meta Get command: {result}"
)
results[key] = result
return results
def meta_get(
self: HasRouter,
key: Key,
flags: Optional[RequestFlags] = None,
failure_handling: FailureHandling = DEFAULT_FAILURE_HANDLING,
) -> ReadResponse:
result = self.router.exec(
command=MetaCommand.META_GET,
key=key,
flags=flags,
failure_handling=failure_handling,
)
if not isinstance(result, (Miss, Value, Success)):
raise MemcacheError(f"Unexpected response for Meta Get command: {result}")
return result
def meta_set(
self: HasRouter,
key: Key,
value: Any,
ttl: int,
flags: Optional[RequestFlags] = None,
failure_handling: FailureHandling = DEFAULT_FAILURE_HANDLING,
) -> WriteResponse:
result = self.router.exec(
command=MetaCommand.META_SET,
key=key,
value=ValueContainer(value),
flags=flags,
failure_handling=failure_handling,
)
if not isinstance(result, (Success, NotStored, Conflict, Miss)):
raise MemcacheError(f"Unexpected response for Meta Set command: {result}")
return result
def meta_delete(
self: HasRouter,
key: Key,
flags: Optional[RequestFlags] = None,
failure_handling: FailureHandling = DEFAULT_FAILURE_HANDLING,
) -> WriteResponse:
result = self.router.exec(
command=MetaCommand.META_DELETE,
key=key,
flags=flags,
failure_handling=failure_handling,
)
if not isinstance(result, (Success, NotStored, Conflict, Miss)):
raise MemcacheError(
f"Unexpected response for Meta Delete command: {result}"
)
return result
def meta_arithmetic(
self: HasRouter,
key: Key,
flags: Optional[RequestFlags] = None,
failure_handling: FailureHandling = DEFAULT_FAILURE_HANDLING,
) -> WriteResponse:
result = self.router.exec(
command=MetaCommand.META_ARITHMETIC,
key=key,
flags=flags,
failure_handling=failure_handling,
)
if not isinstance(result, (Success, NotStored, Conflict, Miss, Value)):
raise MemcacheError(
f"Unexpected response for Meta Delete command: {result}"
)
return result