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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
|
#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""Stub version of the memcache API, keeping all data in process memory."""
import logging
import time
from google.appengine.api import apiproxy_stub
from google.appengine.api import memcache
from google.appengine.api.memcache import memcache_service_pb
from google.appengine.runtime import apiproxy_errors
MemcacheSetResponse = memcache_service_pb.MemcacheSetResponse
MemcacheSetRequest = memcache_service_pb.MemcacheSetRequest
MemcacheIncrementRequest = memcache_service_pb.MemcacheIncrementRequest
MemcacheIncrementResponse = memcache_service_pb.MemcacheIncrementResponse
MemcacheDeleteResponse = memcache_service_pb.MemcacheDeleteResponse
class CacheEntry(object):
"""An entry in the cache."""
def __init__(self, value, expiration, flags, gettime):
"""Initializer.
Args:
value: String containing the data for this entry.
expiration: Number containing the expiration time or offset in seconds
for this entry.
flags: Opaque flags used by the memcache implementation.
gettime: Used for testing. Function that works like time.time().
"""
assert isinstance(value, basestring)
assert len(value) <= memcache.MAX_VALUE_SIZE
assert isinstance(expiration, (int, long))
self._gettime = gettime
self.value = value
self.flags = flags
self.created_time = self._gettime()
self.will_expire = expiration != 0
self.locked = False
self._SetExpiration(expiration)
def _SetExpiration(self, expiration):
"""Sets the expiration for this entry.
Args:
expiration: Number containing the expiration time or offset in seconds
for this entry. If expiration is above one month, then it's considered
an absolute time since the UNIX epoch.
"""
if expiration > (86400 * 30):
self.expiration_time = expiration
else:
self.expiration_time = self._gettime() + expiration
def CheckExpired(self):
"""Returns True if this entry has expired; False otherwise."""
return self.will_expire and self._gettime() >= self.expiration_time
def ExpireAndLock(self, timeout):
"""Marks this entry as deleted and locks it for the expiration time.
Used to implement memcache's delete timeout behavior.
Args:
timeout: Parameter originally passed to memcache.delete or
memcache.delete_multi to control deletion timeout.
"""
self.will_expire = True
self.locked = True
self._SetExpiration(timeout)
def CheckLocked(self):
"""Returns True if this entry was deleted but has not yet timed out."""
return self.locked and not self.CheckExpired()
class MemcacheServiceStub(apiproxy_stub.APIProxyStub):
"""Python only memcache service stub.
This stub keeps all data in the local process' memory, not in any
external servers.
"""
def __init__(self, gettime=time.time, service_name='memcache'):
"""Initializer.
Args:
gettime: time.time()-like function used for testing.
service_name: Service name expected for all calls.
"""
super(MemcacheServiceStub, self).__init__(service_name)
self._gettime = lambda: int(gettime())
self._ResetStats()
self._the_cache = {}
def _ResetStats(self):
"""Resets statistics information."""
self._hits = 0
self._misses = 0
self._byte_hits = 0
self._cache_creation_time = self._gettime()
def _GetKey(self, namespace, key):
"""Retrieves a CacheEntry from the cache if it hasn't expired.
Does not take deletion timeout into account.
Args:
namespace: The namespace that keys are stored under.
key: The key to retrieve from the cache.
Returns:
The corresponding CacheEntry instance, or None if it was not found or
has already expired.
"""
namespace_dict = self._the_cache.get(namespace, None)
if namespace_dict is None:
return None
entry = namespace_dict.get(key, None)
if entry is None:
return None
elif entry.CheckExpired():
del namespace_dict[key]
return None
else:
return entry
def _Dynamic_Get(self, request, response):
"""Implementation of MemcacheService::Get().
Args:
request: A MemcacheGetRequest.
response: A MemcacheGetResponse.
"""
namespace = request.name_space()
keys = set(request.key_list())
for key in keys:
entry = self._GetKey(namespace, key)
if entry is None or entry.CheckLocked():
self._misses += 1
continue
self._hits += 1
self._byte_hits += len(entry.value)
item = response.add_item()
item.set_key(key)
item.set_value(entry.value)
item.set_flags(entry.flags)
def _Dynamic_Set(self, request, response):
"""Implementation of MemcacheService::Set().
Args:
request: A MemcacheSetRequest.
response: A MemcacheSetResponse.
"""
namespace = request.name_space()
for item in request.item_list():
key = item.key()
set_policy = item.set_policy()
old_entry = self._GetKey(namespace, key)
set_status = MemcacheSetResponse.NOT_STORED
if ((set_policy == MemcacheSetRequest.SET) or
(set_policy == MemcacheSetRequest.ADD and old_entry is None) or
(set_policy == MemcacheSetRequest.REPLACE and old_entry is not None)):
if (old_entry is None or
set_policy == MemcacheSetRequest.SET
or not old_entry.CheckLocked()):
if namespace not in self._the_cache:
self._the_cache[namespace] = {}
self._the_cache[namespace][key] = CacheEntry(item.value(),
item.expiration_time(),
item.flags(),
gettime=self._gettime)
set_status = MemcacheSetResponse.STORED
response.add_set_status(set_status)
def _Dynamic_Delete(self, request, response):
"""Implementation of MemcacheService::Delete().
Args:
request: A MemcacheDeleteRequest.
response: A MemcacheDeleteResponse.
"""
namespace = request.name_space()
for item in request.item_list():
key = item.key()
entry = self._GetKey(namespace, key)
delete_status = MemcacheDeleteResponse.DELETED
if entry is None:
delete_status = MemcacheDeleteResponse.NOT_FOUND
elif item.delete_time() == 0:
del self._the_cache[namespace][key]
else:
entry.ExpireAndLock(item.delete_time())
response.add_delete_status(delete_status)
def _internal_increment(self, namespace, request):
"""Internal function for incrementing from a MemcacheIncrementRequest.
Args:
namespace: A string containing the namespace for the request, if any.
Pass an empty string if there is no namespace.
request: A MemcacheIncrementRequest instance.
Returns:
An integer or long if the offset was successful, None on error.
"""
key = request.key()
entry = self._GetKey(namespace, key)
if entry is None:
if not request.has_initial_value():
return None
if namespace not in self._the_cache:
self._the_cache[namespace] = {}
self._the_cache[namespace][key] = CacheEntry(str(request.initial_value()),
expiration=0,
flags=0,
gettime=self._gettime)
entry = self._GetKey(namespace, key)
assert entry is not None
try:
old_value = long(entry.value)
if old_value < 0:
raise ValueError
except ValueError:
logging.error('Increment/decrement failed: Could not interpret '
'value for key = "%s" as an unsigned integer.', key)
return None
delta = request.delta()
if request.direction() == MemcacheIncrementRequest.DECREMENT:
delta = -delta
new_value = old_value + delta
if not (0 <= new_value < 2**64):
new_value = 0
entry.value = str(new_value)
return new_value
def _Dynamic_Increment(self, request, response):
"""Implementation of MemcacheService::Increment().
Args:
request: A MemcacheIncrementRequest.
response: A MemcacheIncrementResponse.
"""
namespace = request.name_space()
new_value = self._internal_increment(namespace, request)
if new_value is None:
raise apiproxy_errors.ApplicationError(
memcache_service_pb.MemcacheServiceError.UNSPECIFIED_ERROR)
response.set_new_value(new_value)
def _Dynamic_BatchIncrement(self, request, response):
"""Implementation of MemcacheService::BatchIncrement().
Args:
request: A MemcacheBatchIncrementRequest.
response: A MemcacheBatchIncrementResponse.
"""
namespace = request.name_space()
for request_item in request.item_list():
new_value = self._internal_increment(namespace, request_item)
item = response.add_item()
if new_value is None:
item.set_increment_status(MemcacheIncrementResponse.NOT_CHANGED)
else:
item.set_increment_status(MemcacheIncrementResponse.OK)
item.set_new_value(new_value)
def _Dynamic_FlushAll(self, request, response):
"""Implementation of MemcacheService::FlushAll().
Args:
request: A MemcacheFlushRequest.
response: A MemcacheFlushResponse.
"""
self._the_cache.clear()
self._ResetStats()
def _Dynamic_Stats(self, request, response):
"""Implementation of MemcacheService::Stats().
Args:
request: A MemcacheStatsRequest.
response: A MemcacheStatsResponse.
"""
stats = response.mutable_stats()
stats.set_hits(self._hits)
stats.set_misses(self._misses)
stats.set_byte_hits(self._byte_hits)
items = 0
total_bytes = 0
for namespace in self._the_cache.itervalues():
items += len(namespace)
for entry in namespace.itervalues():
total_bytes += len(entry.value)
stats.set_items(items)
stats.set_bytes(total_bytes)
stats.set_oldest_item_age(self._gettime() - self._cache_creation_time)
|