OLD | NEW |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 appengine_wrappers import CACHE_TIMEOUT | |
6 from future import Future | 5 from future import Future |
7 | 6 |
8 class _SingleGetFuture(object): | 7 class _SingleGetFuture(object): |
9 def __init__(self, multi_get, key): | 8 def __init__(self, multi_get, key): |
10 self._future = multi_get | 9 self._future = multi_get |
11 self._key = key | 10 self._key = key |
12 | 11 |
13 def Get(self): | 12 def Get(self): |
14 return self._future.Get().get(self._key) | 13 return self._future.Get().get(self._key) |
15 | 14 |
16 class ObjectStore(object): | 15 class ObjectStore(object): |
17 """A class for caching picklable objects. | 16 '''A class for caching picklable objects. |
18 """ | 17 ''' |
19 def Set(self, key, value, time=CACHE_TIMEOUT): | 18 def Get(self, key): |
20 """Sets key -> value in the object store, with the specified timeout. | 19 '''Gets a |Future| with the value of |key| in the object store, or None |
21 """ | 20 if |key| is not in the object store. |
22 self.SetMulti({ key: value }, time=time) | 21 ''' |
| 22 return Future(delegate=_SingleGetFuture(self.GetMulti([key]), key)) |
23 | 23 |
24 def SetMulti(self, mapping, time=CACHE_TIMEOUT): | 24 def GetMulti(self, keys): |
25 """Sets the mapping of keys to values in the object store with the specified | 25 '''Gets a |Future| with values mapped to |keys| from the object store, with |
26 timeout. | 26 any keys not in the object store omitted. |
27 """ | 27 ''' |
28 raise NotImplementedError() | 28 raise NotImplementedError() |
29 | 29 |
30 def Get(self, key, time=CACHE_TIMEOUT): | 30 def Set(self, key, value): |
31 """Gets a |Future| with the value of |key| in the object store, or None | 31 '''Sets key -> value in the object store. |
32 if |key| is not in the object store. | 32 ''' |
33 """ | 33 self.SetMulti({ key: value }) |
34 return Future(delegate=_SingleGetFuture(self.GetMulti([key], time=time), | |
35 key)) | |
36 | 34 |
37 def GetMulti(self, keys, time=CACHE_TIMEOUT): | 35 def SetMulti(self, items): |
38 """Gets a |Future| with values mapped to |keys| from the object store, with | 36 '''Atomically sets the mapping of keys to values in the object store. |
39 any keys not in the object store mapped to None. | 37 ''' |
40 """ | |
41 raise NotImplementedError() | 38 raise NotImplementedError() |
42 | 39 |
43 def Delete(self, key): | 40 def Del(self, key): |
44 """Deletes a key from the object store. | 41 '''Deletes a key from the object store. |
45 """ | 42 ''' |
| 43 self.DelMulti([key]) |
| 44 |
| 45 def DelMulti(self, keys): |
| 46 '''Deletes |keys| from the object store. |
| 47 ''' |
46 raise NotImplementedError() | 48 raise NotImplementedError() |
OLD | NEW |