Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Add type hints to TTL cache.
Browse files Browse the repository at this point in the history
  • Loading branch information
clokep committed Feb 25, 2021
1 parent 37db2be commit da9c051
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 30 deletions.
10 changes: 6 additions & 4 deletions synapse/http/federation/well_known_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@
logger = logging.getLogger(__name__)


_well_known_cache = TTLCache("well-known")
_had_valid_well_known_cache = TTLCache("had-valid-well-known")
_well_known_cache = TTLCache("well-known") # type: TTLCache[bytes, Optional[bytes]]
_had_valid_well_known_cache = TTLCache(
"had-valid-well-known"
) # type: TTLCache[bytes, bool]


@attr.s(slots=True, frozen=True)
Expand All @@ -88,8 +90,8 @@ def __init__(
reactor: IReactorTime,
agent: IAgent,
user_agent: bytes,
well_known_cache: Optional[TTLCache] = None,
had_well_known_cache: Optional[TTLCache] = None,
well_known_cache: Optional[TTLCache[bytes, Optional[bytes]]] = None,
had_well_known_cache: Optional[TTLCache[bytes, bool]] = None,
):
self._reactor = reactor
self._clock = Clock(reactor)
Expand Down
59 changes: 33 additions & 26 deletions synapse/util/caches/ttlcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import logging
import time
from typing import Callable, Dict, Generic, Tuple, TypeVar, Union

import attr
from sortedcontainers import SortedList
Expand All @@ -25,41 +26,46 @@

SENTINEL = object()

T = TypeVar("T")
KT = TypeVar("KT")
VT = TypeVar("VT")

class TTLCache:

class TTLCache(Generic[KT, VT]):
"""A key/value cache implementation where each entry has its own TTL"""

def __init__(self, cache_name, timer=time.time):
def __init__(self, cache_name: str, timer: Callable[[], float] = time.time):
# map from key to _CacheEntry
self._data = {}
self._data = {} # type: Dict[KT, _CacheEntry[KT, VT]]

# the _CacheEntries, sorted by expiry time
self._expiry_list = SortedList() # type: SortedList[_CacheEntry]
self._expiry_list = SortedList() # type: SortedList[_CacheEntry[KT, VT]]

self._timer = timer

self._metrics = register_cache("ttl", cache_name, self, resizable=False)

def set(self, key, value, ttl):
def set(self, key: KT, value: VT, ttl: float) -> None:
"""Add/update an entry in the cache
Args:
key: key for this entry
value: value for this entry
ttl (float): TTL for this entry, in seconds
ttl: TTL for this entry, in seconds
"""
expiry = self._timer() + ttl

self.expire()
e = self._data.pop(key, SENTINEL)
if e != SENTINEL:
if e is not SENTINEL:
assert isinstance(e, _CacheEntry)
self._expiry_list.remove(e)

entry = _CacheEntry(expiry_time=expiry, ttl=ttl, key=key, value=value)
self._data[key] = entry
self._expiry_list.add(entry)

def get(self, key, default=SENTINEL):
def get(self, key: KT, default: T = SENTINEL) -> Union[VT, T]: # type: ignore
"""Get a value from the cache
Args:
Expand All @@ -72,23 +78,23 @@ def get(self, key, default=SENTINEL):
"""
self.expire()
e = self._data.get(key, SENTINEL)
if e == SENTINEL:
if e is SENTINEL:
self._metrics.inc_misses()
if default == SENTINEL:
if default is SENTINEL:
raise KeyError(key)
return default
assert isinstance(e, _CacheEntry)
self._metrics.inc_hits()
return e.value

def get_with_expiry(self, key):
def get_with_expiry(self, key: KT) -> Tuple[VT, float, float]:
"""Get a value, and its expiry time, from the cache
Args:
key: key to look up
Returns:
Tuple[Any, float, float]: the value from the cache, the expiry time
and the TTL
A tuple of the value from the cache, the expiry time and the TTL
Raises:
KeyError if the entry is not found
Expand All @@ -102,7 +108,7 @@ def get_with_expiry(self, key):
self._metrics.inc_hits()
return e.value, e.expiry_time, e.ttl

def pop(self, key, default=SENTINEL):
def pop(self, key: KT, default: T = SENTINEL) -> Union[VT, T]: # type: ignore
"""Remove a value from the cache
If key is in the cache, remove it and return its value, else return default.
Expand All @@ -118,29 +124,30 @@ def pop(self, key, default=SENTINEL):
"""
self.expire()
e = self._data.pop(key, SENTINEL)
if e == SENTINEL:
if e is SENTINEL:
self._metrics.inc_misses()
if default == SENTINEL:
if default is SENTINEL:
raise KeyError(key)
return default
assert isinstance(e, _CacheEntry)
self._expiry_list.remove(e)
self._metrics.inc_hits()
return e.value

def __getitem__(self, key):
def __getitem__(self, key: KT) -> VT:
return self.get(key)

def __delitem__(self, key):
def __delitem__(self, key: KT) -> None:
self.pop(key)

def __contains__(self, key):
def __contains__(self, key: KT) -> bool:
return key in self._data

def __len__(self):
def __len__(self) -> int:
self.expire()
return len(self._data)

def expire(self):
def expire(self) -> None:
"""Run the expiry on the cache. Any entries whose expiry times are due will
be removed
"""
Expand All @@ -154,11 +161,11 @@ def expire(self):


@attr.s(frozen=True, slots=True)
class _CacheEntry:
class _CacheEntry(Generic[KT, VT]):
"""TTLCache entry"""

# expiry_time is the first attribute, so that entries are sorted by expiry.
expiry_time = attr.ib()
ttl = attr.ib()
key = attr.ib()
value = attr.ib()
expiry_time = attr.ib(type=float)
ttl = attr.ib(type=float)
key = attr.ib() # type: KT
value = attr.ib() # type: VT

0 comments on commit da9c051

Please sign in to comment.