OLD | NEW |
---|---|
1 # Copyright 2016 The LUCI Authors. All rights reserved. | 1 # Copyright 2016 The LUCI Authors. All rights reserved. |
2 # Use of this source code is governed under the Apache License, Version 2.0 | 2 # Use of this source code is governed under the Apache License, Version 2.0 |
3 # that can be found in the LICENSE file. | 3 # that can be found in the LICENSE file. |
4 | 4 |
5 """This file implements Named Caches.""" | 5 """This file implements Named Caches.""" |
6 | 6 |
7 import contextlib | 7 import contextlib |
8 import logging | 8 import logging |
9 import optparse | 9 import optparse |
10 import os | 10 import os |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
56 """Opens NamedCaches for mutation operations, such as request or trim. | 56 """Opens NamedCaches for mutation operations, such as request or trim. |
57 | 57 |
58 Only on caller can open the cache manager at a time. If the same thread | 58 Only on caller can open the cache manager at a time. If the same thread |
59 calls this function after opening it earlier, the call will deadlock. | 59 calls this function after opening it earlier, the call will deadlock. |
60 | 60 |
61 time_fn is a function that returns timestamp (float) and used to take | 61 time_fn is a function that returns timestamp (float) and used to take |
62 timestamps when new caches are requested. | 62 timestamps when new caches are requested. |
63 | 63 |
64 Returns a context manager that must be closed as soon as possible. | 64 Returns a context manager that must be closed as soon as possible. |
65 """ | 65 """ |
66 state_path = os.path.join(self.root_dir, u'state.json') | |
67 with self._lock: | 66 with self._lock: |
67 state_path = os.path.join(self.root_dir, u'state.json') | |
68 assert self._lru is None, 'acquired lock, but self._lru is not None' | |
68 if os.path.isfile(state_path): | 69 if os.path.isfile(state_path): |
69 self._lru = lru.LRUDict.load(state_path) | 70 try: |
70 else: | 71 self._lru = lru.LRUDict.load(state_path) |
71 self._lru = lru.LRUDict() | 72 except ValueError as ex: |
73 logging.exception('failed to load named cache state file') | |
74 logging.warning('deleting named caches') | |
75 file_path.rmtree(self.root_dir) | |
M-A Ruel
2017/03/17 13:43:41
nit: I'd prefer the directory itself to not be del
nodir
2017/03/17 16:20:56
why? CacheManager works fine if the directory does
M-A Ruel
2017/03/20 23:22:57
I don't know, it's a feeling about simplifying edg
| |
76 self._lru = self._lru or lru.LRUDict() | |
72 if time_fn: | 77 if time_fn: |
73 self._lru.time_fn = time_fn | 78 self._lru.time_fn = time_fn |
74 try: | 79 try: |
75 yield | 80 yield |
76 finally: | 81 finally: |
77 file_path.ensure_tree(self.root_dir) | 82 file_path.ensure_tree(self.root_dir) |
78 self._lru.save(state_path) | 83 self._lru.save(state_path) |
79 self._lru = None | 84 self._lru = None |
80 | 85 |
81 def __len__(self): | 86 def __len__(self): |
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
276 if options.named_cache_root: | 281 if options.named_cache_root: |
277 return CacheManager(os.path.abspath(options.named_cache_root)) | 282 return CacheManager(os.path.abspath(options.named_cache_root)) |
278 return None | 283 return None |
279 | 284 |
280 | 285 |
281 def _validate_named_cache_path(path): | 286 def _validate_named_cache_path(path): |
282 if os.path.isabs(path): | 287 if os.path.isabs(path): |
283 raise Error('named cache path must not be absolute') | 288 raise Error('named cache path must not be absolute') |
284 if '..' in path.split(os.path.sep): | 289 if '..' in path.split(os.path.sep): |
285 raise Error('named cache path must not contain ".."') | 290 raise Error('named cache path must not contain ".."') |
OLD | NEW |