Access to TOC cache. To turn of the cache functionality don't supply any directories.
| 43 | |
| 44 | |
| 45 | class TocCache(): |
| 46 | """ |
| 47 | Access to TOC cache. To turn of the cache functionality |
| 48 | don't supply any directories. |
| 49 | """ |
| 50 | |
| 51 | def __init__(self, ro_cache=None, rw_cache=None): |
| 52 | self._cache_files = [] |
| 53 | if (ro_cache): |
| 54 | self._cache_files += glob(ro_cache + '/*.json') |
| 55 | if (rw_cache): |
| 56 | self._cache_files += glob(rw_cache + '/*.json') |
| 57 | if not os.path.exists(rw_cache): |
| 58 | os.makedirs(rw_cache) |
| 59 | |
| 60 | self._rw_cache = rw_cache |
| 61 | |
| 62 | def fetch(self, crc): |
| 63 | """ Try to get a hit in the cache, return None otherwise """ |
| 64 | cache_data = None |
| 65 | pattern = '%08X.json' % crc |
| 66 | hit = None |
| 67 | |
| 68 | for name in self._cache_files: |
| 69 | if (name.endswith(pattern)): |
| 70 | hit = name |
| 71 | |
| 72 | if (hit): |
| 73 | try: |
| 74 | cache = open(hit) |
| 75 | cache_data = json.load(cache, |
| 76 | object_hook=self._decoder) |
| 77 | cache.close() |
| 78 | except Exception as exp: |
| 79 | logger.warning('Error while parsing cache file [%s]:%s', |
| 80 | hit, str(exp)) |
| 81 | |
| 82 | return cache_data |
| 83 | |
| 84 | def insert(self, crc, toc): |
| 85 | """ Save a new cache to file """ |
| 86 | if self._rw_cache: |
| 87 | try: |
| 88 | filename = '%s/%08X.json' % (self._rw_cache, crc) |
| 89 | cache = open(filename, 'w') |
| 90 | cache.write(json.dumps(toc, indent=2, |
| 91 | default=self._encoder)) |
| 92 | cache.close() |
| 93 | logger.info('Saved cache to [%s]', filename) |
| 94 | self._cache_files += [filename] |
| 95 | except Exception as exp: |
| 96 | logger.warning('Could not save cache to file [%s]: %s', |
| 97 | filename, str(exp)) |
| 98 | else: |
| 99 | logger.warning('Could not save cache, no writable directory') |
| 100 | |
| 101 | def _encoder(self, obj): |
| 102 | """ Encode a toc element leaf-node """ |