Loads our cache. _recovery is reserved for internal usage and should not be changed
(self, _recovery=False)
| 928 | return change |
| 929 | |
| 930 | def __load_cache(self, _recovery=False): |
| 931 | """Loads our cache. |
| 932 | |
| 933 | _recovery is reserved for internal usage and should not be changed |
| 934 | """ |
| 935 | |
| 936 | # Prepare our dirty flag |
| 937 | self.__dirty = False |
| 938 | |
| 939 | if self.__mode == PersistentStoreMode.MEMORY: |
| 940 | # Nothing further to do |
| 941 | self._cache = {} |
| 942 | return True |
| 943 | |
| 944 | # Prepare our cache file |
| 945 | cache_file = self.cache_file |
| 946 | try: |
| 947 | with gzip.open(cache_file, "rb") as f: |
| 948 | # Read our ontent from disk |
| 949 | self._cache = {} |
| 950 | for k, v in json.loads(f.read().decode(self.encoding)).items(): |
| 951 | co = CacheObject.instantiate(v) |
| 952 | if co: |
| 953 | # Verify our object before assigning it |
| 954 | self._cache[k] = co |
| 955 | |
| 956 | elif not self.__dirty: |
| 957 | # Track changes from our loadset |
| 958 | self.__dirty = True |
| 959 | |
| 960 | except ( |
| 961 | UnicodeDecodeError, |
| 962 | json.decoder.JSONDecodeError, |
| 963 | zlib.error, |
| 964 | TypeError, |
| 965 | AttributeError, |
| 966 | EOFError, |
| 967 | ): |
| 968 | # Let users known there was a problem |
| 969 | logger.warning( |
| 970 | "Corrupted access persistent cache content: %s", cache_file |
| 971 | ) |
| 972 | |
| 973 | if not _recovery: |
| 974 | try: |
| 975 | os.unlink(cache_file) |
| 976 | logger.trace( |
| 977 | "Removed previous persistent cache content: %s", |
| 978 | cache_file, |
| 979 | ) |
| 980 | |
| 981 | except FileNotFoundError: |
| 982 | # no worries; we were removing it anyway |
| 983 | pass |
| 984 | |
| 985 | except OSError as e: |
| 986 | # Permission error of some kind or disk problem... |
| 987 | # There is nothing we can do at this point |
no test coverage detected