|
| 1 | +import typing as t |
1 | 2 | from abc import abstractmethod
|
2 | 3 |
|
3 |
| -from superduper.backends.base.backends import BaseBackend |
4 | 4 | from superduper.components.component import Component
|
5 | 5 |
|
6 | 6 |
|
7 |
| -class Cache(BaseBackend): |
| 7 | +class Cache: |
8 | 8 | """Cache object for caching components.
|
9 | 9 |
|
10 | 10 | # noqa
|
11 | 11 | """
|
12 | 12 |
|
13 | 13 | @abstractmethod
|
14 |
| - def __getitem__(self, *item) -> Component: |
| 14 | + def __getitem__(self, *item) -> t.Dict | t.List: |
15 | 15 | """Get a component from the cache."""
|
16 | 16 | pass
|
| 17 | + |
| 18 | + @abstractmethod |
| 19 | + def keys(self, *pattern) -> t.List[str]: |
| 20 | + """Get the keys from the cache. |
| 21 | +
|
| 22 | + :param pattern: The pattern to match. |
| 23 | +
|
| 24 | + >>> cache.keys('*', '*', '*') |
| 25 | + >>> cache.keys('Model', '*', '*') |
| 26 | + >>> cache.keys('Model', 'my_model', '*') |
| 27 | + >>> cache.keys('*', '*', '1234567890') |
| 28 | + """ |
| 29 | + |
| 30 | + def get_with_uuid(self, uuid: str): |
| 31 | + """Get a component from the cache with a specific uuid. |
| 32 | +
|
| 33 | + :param uuid: The uuid of the component to get. |
| 34 | + """ |
| 35 | + key = self.keys('*', '*', uuid) |
| 36 | + if not key: |
| 37 | + return None |
| 38 | + else: |
| 39 | + key = key[0] |
| 40 | + |
| 41 | + try: |
| 42 | + return self[key] |
| 43 | + except KeyError: |
| 44 | + return |
| 45 | + |
| 46 | + def get_with_component(self, component: str): |
| 47 | + """Get all components from the cache of a certain type. |
| 48 | +
|
| 49 | + :param component: The component to get. |
| 50 | + """ |
| 51 | + keys = self.keys(component, '*', '*') |
| 52 | + return [self[k] for k in keys] |
| 53 | + |
| 54 | + def get_with_component_identifier(self, component: str, identifier: str): |
| 55 | + """Get a component from the cache with a specific identifier. |
| 56 | +
|
| 57 | + :param component: The component to get. |
| 58 | + :param identifier: The identifier of the component to |
| 59 | + """ |
| 60 | + keys = self.keys(component, identifier, '*') |
| 61 | + out = [self[k] for k in keys] |
| 62 | + if not out: |
| 63 | + return None |
| 64 | + out = max(out, key=lambda x: x['version']) # type: ignore[arg-type, call-overload] |
| 65 | + return out |
| 66 | + |
| 67 | + def get_with_component_identifier_version( |
| 68 | + self, component: str, identifier: str, version: int |
| 69 | + ): |
| 70 | + """Get a component from the cache with a specific version. |
| 71 | +
|
| 72 | + :param component: The component to get. |
| 73 | + :param identifier: The identifier of the component to get. |
| 74 | + :param version: The version of the component to get. |
| 75 | + """ |
| 76 | + keys = self.keys(component, identifier, '*') |
| 77 | + out = [self[k] for k in keys] |
| 78 | + try: |
| 79 | + return next(r for r in out if r['version'] == version) # type: ignore[call-overload] |
| 80 | + except StopIteration: |
| 81 | + return |
| 82 | + |
| 83 | + def __contains__(self, key: str) -> bool: |
| 84 | + return key in self.keys() |
| 85 | + |
| 86 | + @abstractmethod |
| 87 | + def __setitem__(self, key: t.Tuple[str, str, str], value: t.Dict) -> None: |
| 88 | + pass |
| 89 | + |
| 90 | + def delete_uuid(self, uuid: str): |
| 91 | + """Delete a component from the cache. |
| 92 | +
|
| 93 | + :param uuid: The uuid of the component to delete. |
| 94 | + """ |
| 95 | + keys = self.keys('*', '*', uuid) |
| 96 | + for key in keys: |
| 97 | + del self[key] # type: ignore[arg-type] |
| 98 | + |
| 99 | + @abstractmethod |
| 100 | + def __delitem__(self, key: t.Tuple[str, str, str]): |
| 101 | + pass |
0 commit comments