| OLD | NEW |
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 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 """This module provides a decorator to cache the results of a function. | 5 """This module provides a decorator to cache the results of a function. |
| 6 | 6 |
| 7 Examples: | 7 Examples: |
| 8 1. Decorate a function: | 8 1. Decorate a function: |
| 9 @cache_decorator.Cached() | 9 @cache_decorator.Cached() |
| 10 def Test(a): | 10 def Test(a): |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 | 29 |
| 30 d1 = Downloader('http://url', 4) | 30 d1 = Downloader('http://url', 4) |
| 31 d1.Download('path') | 31 d1.Download('path') |
| 32 | 32 |
| 33 d2 = Downloader('http://url', 5) | 33 d2 = Downloader('http://url', 5) |
| 34 d2.Download('path') # Returned the cached downloaded data. | 34 d2.Download('path') # Returned the cached downloaded data. |
| 35 """ | 35 """ |
| 36 | 36 |
| 37 import functools | 37 import functools |
| 38 import hashlib | 38 import hashlib |
| 39 import logging |
| 39 import inspect | 40 import inspect |
| 40 import logging | |
| 41 import pickle | 41 import pickle |
| 42 | 42 |
| 43 | 43 |
| 44 def _DefaultKeyGenerator(func, args, kwargs): | 44 def _DefaultKeyGenerator(func, args, kwargs): |
| 45 """Generates a key from the function and arguments passed to it. | 45 """Generates a key from the function and arguments passed to it. |
| 46 | 46 |
| 47 Args: | 47 Args: |
| 48 func (function): An arbitrary function. | 48 func (function): An arbitrary function. |
| 49 args (list): Positional arguments passed to ``func``. | 49 args (list): Positional arguments passed to ``func``. |
| 50 kwargs (dict): Keyword arguments passed to ``func``. | 50 kwargs (dict): Keyword arguments passed to ``func``. |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 127 except Exception: # pragma: no cover. | 127 except Exception: # pragma: no cover. |
| 128 logging.exception( | 128 logging.exception( |
| 129 'Failed to cache data for function %s.%s, args=%s, kwargs=%s', | 129 'Failed to cache data for function %s.%s, args=%s, kwargs=%s', |
| 130 func.__module__, func.__name__, repr(args), repr(kwargs)) | 130 func.__module__, func.__name__, repr(args), repr(kwargs)) |
| 131 | 131 |
| 132 return result | 132 return result |
| 133 | 133 |
| 134 return Wrapped | 134 return Wrapped |
| 135 | 135 |
| 136 return Decorator | 136 return Decorator |
| OLD | NEW |