def cache_file_at_url(self, url): """ Caches the remote file or retrieves its cached version if one exists. Returns None if an error occurs. """ filename, extension = os.path.splitext(url) cache_asset_hash = hash(str(self.channel_id) + filename) asset = Asset.selectBy(plugin_channel=self.channel_id, filename=filename, is_cached=True).getOne(None) if asset is None: if CacheManager._get_lock(cache_asset_hash, blocking=False): try: asset = Asset(plugin_channel=self.channel_id, filename=filename, user=None, extension=extension if extension is not None and len(extension) > 0 else None, is_cached=True) self.download_manager.enqueue_asset(asset) except (URLError, OSError): logger.warning( 'Exception encountered when attempting to cache file at url %s', url, exc_info=True) CacheManager._release_lock(cache_asset_hash) return None CacheManager._release_lock(cache_asset_hash) else: CacheManager._get_lock(cache_asset_hash, blocking=True) CacheManager._release_lock(cache_asset_hash) asset = Asset.selectBy(plugin_channel=self.channel_id, filename=filename, is_cached=True).getOne(None) return asset
def get_cached_file(self, filename): """ Returns an Asset if this cache manager has a cached version of the given file for this channel or None if none exists. """ return Asset.selectBy(plugin_channel=self.channel_id, filename=filename, is_cached=True).getOne(None)
def cleanup_cache(self): """ Cleans up the cached assets by deleting all assets not referenced during this day. """ today = datetime.date.today() unused_assets = Asset.selectBy(is_cached=True).filter(Asset.q.last_reference < sqlbuilder.func.date(str(today))) total_assets_size = 0 if unused_assets.count() > 0: total_assets_size = int(unused_assets.sum(Asset.q.file_size)) Asset.deleteMany(AND(Asset.q.last_reference < sqlbuilder.func.date(str(today)), Asset.q.is_cached == True)) logger.info('Ran cache cleanup and deleted %d assets for a total size of %s', unused_assets.count(), CleanupScheduler._human_readable_size(total_assets_size)) next_cleanup = datetime.datetime.combine(today + datetime.timedelta(days=1), datetime.time(hour=23, minute=55)) self.s.enterabs(time.mktime(next_cleanup.timetuple()), 1, self.cleanup_cache)