1
0

Added CardCache to speed up listings

This commit is contained in:
Jon Michael Aanes 2024-09-27 11:06:06 +02:00
parent b5622d4993
commit 6a78399661

View File

@ -12,6 +12,30 @@ URL_API_ROOT = 'https://favro.com/api/v1'
URL_GET_ALL_CARDS = URL_API_ROOT + '/cards'
URL_UPDATE_CARD = URL_API_ROOT + '/cards/{card_id}'
class CardCache:
def __init__(self):
self.cards = []
def add_card(self, card: Card) -> None:
self.remove(card.card_id)
self.cards.append(card)
def get_card_by_card_id(self, card_id: CardId) -> Card | None:
for card in self.cards:
if card.card_id == card_id:
return card
def get_card_by_seq_id(self, seq_id: SeqId) -> Card | None:
for card in self.cards:
if card.seq_id == seq_id:
return card
def remove(self, card_id: CardId) -> Card | None:
card = self.get_card_by_card_id(card_id)
if card in self.cards:
self.cards.remove(card)
return card
class FavroClient:
def __init__(
@ -38,8 +62,7 @@ class FavroClient:
)
self.read_only = read_only
self.card_id_to_seq_id: dict[CardId, SeqId] = {}
self.seq_id_to_card_id: dict[SeqId, CardId] = {}
self.cache = CardCache()
def check_logged_in(self) -> None:
next(self.get_todo_list_cards())
@ -61,8 +84,7 @@ class FavroClient:
# TODO: Pageination
for entity_json in json['entities']:
card = Card.from_json(entity_json)
self.card_id_to_seq_id[card.card_id] = card.seq_id
self.seq_id_to_card_id[card.seq_id] = card.card_id
self.cache.add_card(card)
yield card
del entity_json
@ -79,17 +101,14 @@ class FavroClient:
return self.session.prepare_request(request)
def get_card(self, seq_id: SeqId) -> Card:
if card := self.cache.get_card_by_seq_id(seq_id):
return card
return next(self.get_cards(seq_id=seq_id))
def get_card_id(self, seq_id: SeqId) -> CardId:
if card_id := self.seq_id_to_card_id[seq_id]:
return card_id
first_card = next(self.get_cards(seq_id=seq_id))
return first_card.card_id
def _invalidate_cache(self, card_id: CardId) -> None:
card = self.cache.remove(card_id)
self.session.cache.delete(
requests=[self._get_cards_request(seq_id=self.card_id_to_seq_id[card_id])],
requests=[self._get_cards_request(seq_id=card.seq_id)],
)
def update_card_description(self, card_id: CardId, description: str) -> Card: