Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(135)

Side by Side Diff: chrome/common/extensions/docs/server2/in_memory_object_store.py

Issue 10829348: Extensions Docs Server: Large performance increase (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 import time
6
7 from future import Future
8 from object_store import ObjectStore, CACHE_TIMEOUT
9 from memcache_object_store import MemcacheObjectStore
10
11 class _CacheEntry(object):
12 def __init__(self, value, expire_time):
13 self.value = value
14 self._never_expires = (expire_time == 0)
15 self._expiry = time.time() + expire_time
16
17 def HasExpired(self):
18 if self._never_expires:
19 return False
20 return time.time() > self._expiry
21
22 class _AsyncGetFuture(object):
23 """A future for memcache gets.
24
25 Properties:
26 - |cache| the in-memory cache used by InMemoryObjectStore
27 - |time| the cache timeout
28 - |namespace| the namespace of the cache items
29 - |future| the |Future| from the backing |ObjectStore|
30 - |initial_mapping| a mapping of cache items already in memory
31 """
32 def __init__(self, cache, time, namespace, future, initial_mapping):
33 self._cache = cache
34 self._time = time
35 self._namespace = namespace
36 self._future = future
37 self._mapping = initial_mapping
38
39 def Get(self):
40 if self._future is not None:
41 result = self._future.Get()
42 self._cache[self._namespace].update(
43 dict((k, _CacheEntry(v, self._time)) for k, v in result.iteritems()))
44 self._mapping.update(result)
45 return self._mapping
46
47 class InMemoryObjectStore(ObjectStore):
48 def __init__(self, branch):
49 self._branch = branch
50 self._cache = {}
51 self._object_store = MemcacheObjectStore()
52
53 def _MakeNamespace(self, namespace):
54 return 'ObjectStore.%s.%s' % (self._branch, namespace)
55
56 def SetMulti(self, mapping, namespace, time=CACHE_TIMEOUT):
57 namespace = self._MakeNamespace(namespace)
58 for k, v in mapping.iteritems():
59 if namespace not in self._cache:
60 self._cache[namespace] = {}
61 self._cache[namespace][k] = _CacheEntry(v, time)
62 # TODO(cduvall): Use a batch set? App Engine kept throwing:
63 # ValueError: Values may not be more than 1000000 bytes in length
64 # for the batch set.
65 self._object_store.Set(k, v, namespace, time=time)
66
67 def GetMulti(self, keys, namespace, time=CACHE_TIMEOUT):
68 namespace = self._MakeNamespace(namespace)
69 keys = keys[:]
70 mapping = {}
71 if namespace not in self._cache:
72 self._cache[namespace] = {}
73 for key in keys:
74 cache_entry = self._cache[namespace].get(key, None)
75 if cache_entry is None or cache_entry.HasExpired():
76 mapping[key] = None
77 else:
78 mapping[key] = cache_entry.value
79 keys.remove(key)
80 future = self._object_store.GetMulti(keys, namespace, time=time)
81 return Future(delegate=_AsyncGetFuture(self._cache,
82 time,
83 namespace,
84 future,
85 mapping))
86
87 def Delete(self, key, namespace):
88 namespace = self._MakeNamespace(namespace)
89 if namespace in self._cache:
90 self._cache[namespace].pop(key)
91 self._object_store.Delete(key, namespace)
OLDNEW
« no previous file with comments | « chrome/common/extensions/docs/server2/handler.py ('k') | chrome/common/extensions/docs/server2/integration_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698