Abstract implementation. Return cache if exists otherwise use function to get Assumes to add to session if Miss. Session may not exist. In which case we assume it's still better to run the cache_miss_function for example for a new project.
(self,
cache_key: str,
cache_miss_function,
session,
miss_function_args={})
| 15 | """ |
| 16 | |
| 17 | def get_with_cache(self, |
| 18 | cache_key: str, |
| 19 | cache_miss_function, |
| 20 | session, |
| 21 | miss_function_args={}): |
| 22 | """ |
| 23 | Abstract implementation. |
| 24 | |
| 25 | Return cache if exists otherwise use function to get |
| 26 | |
| 27 | Assumes to add to session if Miss. |
| 28 | Session may not exist. In which case we assume |
| 29 | it's still better to run the cache_miss_function |
| 30 | for example for a new project. |
| 31 | |
| 32 | We pass cach_key to regenerate_cache_by_key so that it automatically |
| 33 | will match. That way a user needs only use this function, and the |
| 34 | cache_miss_function that's passed doesn't have ot know about the caching |
| 35 | concepts. |
| 36 | |
| 37 | |
| 38 | Cache Info __info concept: |
| 39 | Why: For debugging good to know. |
| 40 | Also could use it if cache is getting too stale. (eg by default we refresh if too |
| 41 | old even if no direct trigger). |
| 42 | |
| 43 | Composite key idea |
| 44 | Context: The datatype is unknown and don't want to start recursively |
| 45 | sniffing the data type... |
| 46 | eg could be list of dicts or list of ints, or single int, etc. |
| 47 | |
| 48 | Separate dict because: |
| 49 | __info name because 'info' seems too high level may want to use that for normal use |
| 50 | dictionary |
| 51 | |
| 52 | a) We can serialize single __info even if multiple attributes (vs having to declare key each time) |
| 53 | b) Can add date generated (which is separate from bool) |
| 54 | |
| 55 | Example of how to get cache info |
| 56 | Add this key on the return object: |
| 57 | 'cache_info': self.cache_dict.get('__info') if self.cache_dict else None |
| 58 | See project.py for example |
| 59 | Careful, cache_dict could be None |
| 60 | |
| 61 | Example of the output it shows: |
| 62 | project: { |
| 63 | cache_info: { |
| 64 | directory_list: { |
| 65 | from_cache: false, |
| 66 | regenerate_datetime: "2020-09-21T23:59:05.819037"} |
| 67 | member_list: { |
| 68 | from_cache: true |
| 69 | } |
| 70 | preview_file_list: { |
| 71 | from_cache: true |
| 72 | } |
| 73 | |
| 74 | Slight gotcha: |