-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'show_cache_info' into demo
# Conflicts: # src/modelbench/benchmark_runner.py
- Loading branch information
Showing
7 changed files
with
300 additions
and
140 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import collections.abc | ||
from abc import ABC, abstractmethod | ||
|
||
import diskcache | ||
|
||
|
||
class MBCache(ABC, collections.abc.Mapping): | ||
@abstractmethod | ||
def __setitem__(self, __key, __value): | ||
pass | ||
|
||
def __enter__(self): | ||
return self | ||
|
||
def __exit__(self, __type, __value, __traceback): | ||
pass | ||
|
||
|
||
class NullCache(MBCache): | ||
"""Doesn't save anything""" | ||
|
||
def __setitem__(self, __key, __value): | ||
pass | ||
|
||
def __getitem__(self, key, /): | ||
raise KeyError() | ||
|
||
def __len__(self): | ||
return 0 | ||
|
||
def __iter__(self): | ||
pass | ||
|
||
|
||
class InMemoryCache(MBCache): | ||
"""Holds stuff in memory only""" | ||
|
||
def __init__(self): | ||
super().__init__() | ||
self.contents = dict() | ||
|
||
def __setitem__(self, __key, __value): | ||
self.contents.__setitem__(__key, __value) | ||
|
||
def __getitem__(self, key, /): | ||
return self.contents.__getitem__(key) | ||
|
||
def __len__(self): | ||
return self.contents.__len__() | ||
|
||
def __iter__(self): | ||
return self.contents.__iter__() | ||
|
||
|
||
class DiskCache(MBCache): | ||
""" | ||
Holds stuff in memory only. The docs recommend using | ||
it as a context manager in a threaded context: | ||
"Each thread that accesses a cache should also call close | ||
on the cache. Cache objects can be used in a with statement | ||
to safeguard calling close." | ||
""" | ||
|
||
def __init__(self, cache_path): | ||
super().__init__() | ||
self.cache_path = cache_path | ||
self.raw_cache = diskcache.Cache(cache_path) | ||
self.contents = self.raw_cache | ||
|
||
def __enter__(self): | ||
self.contents = self.raw_cache.__enter__() | ||
return self.contents | ||
|
||
def __exit__(self, __type, __value, __traceback): | ||
self.raw_cache.__exit__(__type, __value, __traceback) | ||
self.contents = self.raw_cache | ||
|
||
def __setitem__(self, __key, __value): | ||
self.contents.__setitem__(__key, __value) | ||
|
||
def __getitem__(self, key, /): | ||
return self.contents.__getitem__(key) | ||
|
||
def __len__(self): | ||
return self.contents.__len__() | ||
|
||
def __iter__(self): | ||
return self.contents.__iter__() | ||
|
||
def __str__(self): | ||
return self.__class__.__name__ + f"({self.cache_path})" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.