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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
|
from bmemcached.client.mixin import ClientMixin
class ReplicatingClient(ClientMixin):
"""
This is intended to be a client class which implement standard cache interface that common libs do...
It replicates values over servers and get a response from the first one it can.
"""
def _set_retry_delay(self, value):
for server in self._servers:
server.set_retry_delay(value)
def enable_retry_delay(self, enable):
"""
Enable or disable delaying between reconnection attempts.
The first reconnection attempt will always happen immediately, so intermittent network
errors don't cause caching to turn off. The retry delay takes effect after the first
reconnection fails.
The reconnection delay is enabled by default for TCP connections, and disabled by
default for Unix socket connections.
"""
# The public API only allows enabling or disabling the delay, so it'll be easier to
# add exponential falloff in the future. _set_retry_delay is exposed for tests.
self._set_retry_delay(5 if enable else 0)
def get(self, key, default=None, get_cas=False):
"""
Get a key from server.
:param key: Key's name
:type key: six.string_types
:param default: In case memcached does not find a key, return a default value
:param get_cas: If true, return (value, cas), where cas is the new CAS value.
:type get_cas: boolean
:return: Returns a key data from server.
:rtype: object
"""
for server in self.servers:
value, cas = server.get(key)
if value is not None:
if get_cas:
return value, cas
else:
return value
if default is not None:
if get_cas:
return default, None
return default
if get_cas:
return None, None
def gets(self, key):
"""
Get a key from server, returning the value and its CAS key.
This method is for API compatibility with other implementations.
:param key: Key's name
:type key: six.string_types
:return: Returns (key data, value), or (None, None) if the value is not in cache.
:rtype: object
"""
for server in self.servers:
value, cas = server.get(key)
if value is not None:
return value, cas
return None, None
def get_multi(self, keys, get_cas=False):
"""
Get multiple keys from server.
:param keys: A list of keys to from server.
:type keys: list
:param get_cas: If get_cas is true, each value is (data, cas), with each result's CAS value.
:type get_cas: boolean
:return: A dict with all requested keys.
:rtype: dict
"""
d = {}
if keys:
for server in self.servers:
results = server.get_multi(keys)
if not get_cas:
# Remove CAS data
for key, (value, cas) in results.items():
results[key] = value
d.update(results)
keys = [_ for _ in keys if _ not in d]
if not keys:
break
return d
def set(self, key, value, time=0, compress_level=-1):
"""
Set a value for a key on server.
:param key: Key's name
:type key: str
:param value: A value to be stored on server.
:type value: object
:param time: Time in seconds that your key will expire.
:type time: int
:param compress_level: How much to compress.
0 = no compression, 1 = fastest, 9 = slowest but best,
-1 = default compression level.
:type compress_level: int
:return: True in case of success and False in case of failure
:rtype: bool
"""
returns = []
for server in self.servers:
returns.append(server.set(key, value, time, compress_level=compress_level))
return any(returns)
def cas(self, key, value, cas, time=0, compress_level=-1):
"""
Set a value for a key on server if its CAS value matches cas.
:param key: Key's name
:type key: six.string_types
:param value: A value to be stored on server.
:type value: object
:param cas: The CAS value previously obtained from a call to get*.
:type cas: int
:param time: Time in seconds that your key will expire.
:type time: int
:param compress_level: How much to compress.
0 = no compression, 1 = fastest, 9 = slowest but best,
-1 = default compression level.
:type compress_level: int
:return: True in case of success and False in case of failure
:rtype: bool
"""
returns = []
for server in self.servers:
returns.append(server.cas(key, value, cas, time, compress_level=compress_level))
return any(returns)
def set_multi(self, mappings, time=0, compress_level=-1):
"""
Set multiple keys with it's values on server.
:param mappings: A dict with keys/values
:type mappings: dict
:param time: Time in seconds that your key will expire.
:type time: int
:param compress_level: How much to compress.
0 = no compression, 1 = fastest, 9 = slowest but best,
-1 = default compression level.
:type compress_level: int
:return: List of keys that failed to be set on any server.
:rtype: list
"""
returns = set()
if mappings:
for server in self.servers:
returns |= set(server.set_multi(mappings, time, compress_level=compress_level))
return list(returns)
def add(self, key, value, time=0, compress_level=-1):
"""
Add a key/value to server ony if it does not exist.
:param key: Key's name
:type key: six.string_types
:param value: A value to be stored on server.
:type value: object
:param time: Time in seconds that your key will expire.
:type time: int
:param compress_level: How much to compress.
0 = no compression, 1 = fastest, 9 = slowest but best,
-1 = default compression level.
:type compress_level: int
:return: True if key is added False if key already exists
:rtype: bool
"""
returns = []
for server in self.servers:
returns.append(server.add(key, value, time, compress_level=compress_level))
return any(returns)
def replace(self, key, value, time=0, compress_level=-1):
"""
Replace a key/value to server ony if it does exist.
:param key: Key's name
:type key: six.string_types
:param value: A value to be stored on server.
:type value: object
:param time: Time in seconds that your key will expire.
:type time: int
:param compress_level: How much to compress.
0 = no compression, 1 = fastest, 9 = slowest but best,
-1 = default compression level.
:type compress_level: int
:return: True if key is replace False if key does not exists
:rtype: bool
"""
returns = []
for server in self.servers:
returns.append(server.replace(key, value, time, compress_level=compress_level))
return any(returns)
def delete(self, key, cas=0):
"""
Delete a key/value from server. If key does not exist, it returns True.
:param key: Key's name to be deleted
:param cas: CAS of the key
:return: True in case o success and False in case of failure.
"""
returns = []
for server in self.servers:
returns.append(server.delete(key, cas))
return any(returns)
def delete_multi(self, keys):
returns = []
for server in self.servers:
returns.append(server.delete_multi(keys))
return all(returns)
def incr(self, key, value):
"""
Increment a key, if it exists, returns it's actual value, if it don't, return 0.
:param key: Key's name
:type key: six.string_types
:param value: Number to be incremented
:type value: int
:return: Actual value of the key on server
:rtype: int
"""
returns = []
for server in self.servers:
returns.append(server.incr(key, value))
return returns[0]
def decr(self, key, value):
"""
Decrement a key, if it exists, returns it's actual value, if it don't, return 0.
Minimum value of decrement return is 0.
:param key: Key's name
:type key: six.string_types
:param value: Number to be decremented
:type value: int
:return: Actual value of the key on server
:rtype: int
"""
returns = []
for server in self.servers:
returns.append(server.decr(key, value))
return returns[0]
|