OLD | NEW |
---|---|
1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 from future import Future | 5 from future import All, Future |
6 from object_store import ObjectStore | 6 from object_store import ObjectStore |
7 | 7 |
8 | 8 |
9 class CacheChainObjectStore(ObjectStore): | 9 class CacheChainObjectStore(ObjectStore): |
10 '''Maintains an in-memory cache along with a chain of other object stores to | 10 '''Maintains an in-memory cache along with a chain of other object stores to |
11 try for the same keys. This is useful for implementing a multi-layered cache. | 11 try for the same keys. This is useful for implementing a multi-layered cache. |
12 The in-memory cache is inbuilt since it's synchronous, but the object store | 12 The in-memory cache is inbuilt since it's synchronous, but the object store |
13 interface is asynchronous. | 13 interface is asynchronous. |
14 The rules for the object store chain are: | 14 The rules for the object store chain are: |
15 - When setting (or deleting) items, all object stores in the hierarcy will | 15 - When setting (or deleting) items, all object stores in the hierarcy will |
16 have that item set. | 16 have that item set. |
17 - When getting items, the behaviour depends on |start_empty|. | 17 - When getting items, the behaviour depends on |start_empty|. |
18 - If false, each object store is tried in order. The first object | 18 - If false, each object store is tried in order. The first object |
19 store to find the item will trickle back up, setting it on all object | 19 store to find the item will trickle back up, setting it on all object |
20 stores higher in the hierarchy. | 20 stores higher in the hierarchy. |
21 - If true, only the first in-memory cache is checked, as though the store | 21 - If true, only the first in-memory cache is checked, as though the store |
22 had been initialized with no content as opposed to the union of its | 22 had been initialized with no content as opposed to the union of its |
23 delegate stores. | 23 delegate stores. |
24 ''' | 24 ''' |
25 def __init__(self, object_stores, start_empty=False): | 25 def __init__(self, object_stores, start_empty=False): |
26 self._object_stores = object_stores | 26 self._object_stores = object_stores |
27 self._start_empty = start_empty | 27 self._start_empty = start_empty |
28 self._cache = {} | 28 self._cache = {} |
29 | 29 |
30 def SetMulti(self, mapping): | 30 def SetMulti(self, mapping): |
31 self._cache.update(mapping) | 31 self._cache.update(mapping) |
32 for object_store in self._object_stores: | 32 return All(object_store.SetMulti(mapping) |
Ken Rockot(use gerrit already)
2014/10/23 20:16:18
Actually we don't want to force callers to Get the
not at google - send to devlin
2014/10/23 20:18:08
ah good point. You might want to add a comment for
| |
33 object_store.SetMulti(mapping) | 33 for object_store in self._object_stores) |
34 | 34 |
35 def GetMulti(self, keys): | 35 def GetMulti(self, keys): |
36 missing_keys = list(keys) | 36 missing_keys = list(keys) |
37 cached_items = {} | 37 cached_items = {} |
38 for key in keys: | 38 for key in keys: |
39 if key in self._cache: | 39 if key in self._cache: |
40 cached_items[key] = self._cache.get(key) | 40 cached_items[key] = self._cache.get(key) |
41 missing_keys.remove(key) | 41 missing_keys.remove(key) |
42 if len(missing_keys) == 0 or self._start_empty: | 42 if len(missing_keys) == 0 or self._start_empty: |
43 return Future(value=cached_items) | 43 return Future(value=cached_items) |
(...skipping 28 matching lines...) Expand all Loading... | |
72 if updates: | 72 if updates: |
73 object_store.SetMulti(updates) | 73 object_store.SetMulti(updates) |
74 return cached_items | 74 return cached_items |
75 return Future(callback=resolve) | 75 return Future(callback=resolve) |
76 | 76 |
77 def DelMulti(self, keys): | 77 def DelMulti(self, keys): |
78 for k in keys: | 78 for k in keys: |
79 self._cache.pop(k, None) | 79 self._cache.pop(k, None) |
80 for object_store in self._object_stores: | 80 for object_store in self._object_stores: |
81 object_store.DelMulti(keys) | 81 object_store.DelMulti(keys) |
OLD | NEW |