|
17 | 17 | from typing import Sequence |
18 | 18 | from typing import Tuple |
19 | 19 | from typing import Type |
| 20 | +from typing import TYPE_CHECKING |
20 | 21 | from typing import Union |
21 | 22 |
|
22 | 23 | from decorator import decorate |
@@ -700,7 +701,12 @@ def is_configured(self): |
700 | 701 | """ |
701 | 702 | return "backend" in self.__dict__ |
702 | 703 |
|
703 | | - def get(self, key, expiration_time=None, ignore_expiration=False): |
| 704 | + def get( |
| 705 | + self, |
| 706 | + key: KeyType, |
| 707 | + expiration_time: Optional[float] = None, |
| 708 | + ignore_expiration: bool = False, |
| 709 | + ) -> CacheReturnType: |
704 | 710 | """Return a value from the cache, based on the given key. |
705 | 711 |
|
706 | 712 | If the value is not present, the method returns the token |
@@ -771,15 +777,49 @@ def get(self, key, expiration_time=None, ignore_expiration=False): |
771 | 777 |
|
772 | 778 |
|
773 | 779 | """ |
| 780 | + value = self._get_cache_value(key, expiration_time, ignore_expiration) |
| 781 | + return value.payload |
| 782 | + |
| 783 | + def get_value_metadata( |
| 784 | + self, |
| 785 | + key: KeyType, |
| 786 | + expiration_time: Optional[float] = None, |
| 787 | + ignore_expiration: bool = False, |
| 788 | + ) -> Optional[CachedValue]: |
| 789 | + """Return the :class:`.CachedValue` object directly from the cache. |
| 790 | +
|
| 791 | + This is the enclosing datastructure that includes the value as well as |
| 792 | + the metadata, including the timestamp when the value was cached. |
| 793 | + Convenience accessors on :class:`.CachedValue` also provide for common |
| 794 | + data such as :attr:`.CachedValue.cached_time` and |
| 795 | + :attr:`.CachedValue.age`. |
| 796 | +
|
774 | 797 |
|
| 798 | + .. versionadded:: 1.3. Added :meth:`.CacheRegion.get_value_metadata` |
| 799 | + """ |
| 800 | + cache_value = self._get_cache_value( |
| 801 | + key, expiration_time, ignore_expiration |
| 802 | + ) |
| 803 | + if cache_value is NO_VALUE: |
| 804 | + return None |
| 805 | + else: |
| 806 | + if TYPE_CHECKING: |
| 807 | + assert isinstance(cache_value, CachedValue) |
| 808 | + return cache_value |
| 809 | + |
| 810 | + def _get_cache_value( |
| 811 | + self, |
| 812 | + key: KeyType, |
| 813 | + expiration_time: Optional[float] = None, |
| 814 | + ignore_expiration: bool = False, |
| 815 | + ) -> CacheReturnType: |
775 | 816 | if self.key_mangler: |
776 | 817 | key = self.key_mangler(key) |
777 | 818 | value = self._get_from_backend(key) |
778 | 819 | value = self._unexpired_value_fn(expiration_time, ignore_expiration)( |
779 | 820 | value |
780 | 821 | ) |
781 | | - |
782 | | - return value.payload |
| 822 | + return value |
783 | 823 |
|
784 | 824 | def _unexpired_value_fn(self, expiration_time, ignore_expiration): |
785 | 825 | if ignore_expiration: |
|
0 commit comments