Changeset View
Changeset View
Standalone View
Standalone View
swh/fuse/cache.py
Show First 20 Lines • Show All 347 Lines • ▼ Show 20 Lines | class LRU(OrderedDict): | ||||
# Rough size estimate in bytes for a list of entries | # Rough size estimate in bytes for a list of entries | ||||
return len(value) * 1000 | return len(value) * 1000 | ||||
def __getitem__(self, key: Any) -> Any: | def __getitem__(self, key: Any) -> Any: | ||||
value = super().__getitem__(key) | value = super().__getitem__(key) | ||||
self.move_to_end(key) | self.move_to_end(key) | ||||
return value | return value | ||||
def __delitem__(self, key: Any) -> None: | |||||
self.used_ram -= self.sizeof(self[key]) | |||||
super().__delitem__(key) | |||||
def __setitem__(self, key: Any, value: Any) -> None: | def __setitem__(self, key: Any, value: Any) -> None: | ||||
if key in self: | if key in self: | ||||
self.move_to_end(key) | self.move_to_end(key) | ||||
else: | else: | ||||
self.used_ram += self.sizeof(value) | self.used_ram += self.sizeof(value) | ||||
super().__setitem__(key, value) | super().__setitem__(key, value) | ||||
while self.used_ram > self.max_ram and self: | while self.used_ram > self.max_ram and self: | ||||
oldest = next(iter(self)) | oldest = next(iter(self)) | ||||
self.used_ram -= self.sizeof(oldest) | |||||
del self[oldest] | del self[oldest] | ||||
def __init__(self, conf: Dict[str, Any]): | def __init__(self, conf: Dict[str, Any]): | ||||
m = re.match(r"(\d+)\s*(.+)\s*", conf["maxram"]) | m = re.match(r"(\d+)\s*(.+)\s*", conf["maxram"]) | ||||
if not m: | if not m: | ||||
logging.error("Cannot parse direntry maxram config: %s", conf["maxram"]) | logging.error("Cannot parse direntry maxram config: %s", conf["maxram"]) | ||||
sys.exit(1) | sys.exit(1) | ||||
Show All 10 Lines | class DirEntryCache: | ||||
def get(self, direntry: FuseDirEntry) -> Optional[List[FuseEntry]]: | def get(self, direntry: FuseDirEntry) -> Optional[List[FuseEntry]]: | ||||
return self.lru_cache.get(direntry.inode, None) | return self.lru_cache.get(direntry.inode, None) | ||||
def set(self, direntry: FuseDirEntry, entries: List[FuseEntry]) -> None: | def set(self, direntry: FuseDirEntry, entries: List[FuseEntry]) -> None: | ||||
if isinstance(direntry, (CacheDir, CacheDir.ArtifactShardBySwhid, OriginDir)): | if isinstance(direntry, (CacheDir, CacheDir.ArtifactShardBySwhid, OriginDir)): | ||||
# The `cache/` and `origin/` directories are populated on the fly | # The `cache/` and `origin/` directories are populated on the fly | ||||
pass | pass | ||||
elif ( | |||||
isinstance(direntry, RevisionHistoryShardByDate) | |||||
and not direntry.is_status_done | |||||
): | |||||
# The `by-date/' directory is populated in parallel so only cache it | |||||
# once it has finished fetching all data from the API | |||||
pass | |||||
else: | else: | ||||
self.lru_cache[direntry.inode] = entries | self.lru_cache[direntry.inode] = entries | ||||
def invalidate(self, direntry: FuseDirEntry) -> None: | |||||
try: | |||||
del self.lru_cache[direntry.inode] | |||||
except KeyError: | |||||
pass |