Skip to content

Commit

Permalink
replace lru_cache with specialized cache handling at the entry list l…
Browse files Browse the repository at this point in the history
…evel

Signed-off-by: Andrew Whitehead <[email protected]>
  • Loading branch information
andrewwhitehead committed Jan 28, 2025
1 parent 01dcf5b commit d502f4a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
10 changes: 9 additions & 1 deletion wrappers/python/aries_askar/bindings/handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
c_void_p,
)

from .lib import ByteBuffer, Lib, StrBuffer, finalize_struct
from .lib import ByteBuffer, Lib, StrBuffer, entry_cache, finalize_struct


LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -124,6 +124,7 @@ class EntryListHandle(ArcHandle):

_dtor_ = "askar_entry_list_free"

@entry_cache
def get_category(self, index: int) -> str:
"""Get the entry category."""
cat = StrBuffer()
Expand All @@ -136,6 +137,7 @@ def get_category(self, index: int) -> str:
)
return str(cat)

@entry_cache
def get_name(self, index: int) -> str:
"""Get the entry name."""
name = StrBuffer()
Expand All @@ -148,6 +150,7 @@ def get_name(self, index: int) -> str:
)
return str(name)

@entry_cache
def get_value(self, index: int) -> memoryview:
"""Get the entry value."""
val = ByteBuffer()
Expand All @@ -160,6 +163,7 @@ def get_value(self, index: int) -> memoryview:
)
return val.view

@entry_cache
def get_tags(self, index: int) -> dict:
"""Get the entry tags."""
tags = StrBuffer()
Expand All @@ -185,6 +189,7 @@ class KeyEntryListHandle(ArcHandle):

_dtor_ = "askar_key_entry_list_free"

@entry_cache
def get_algorithm(self, index: int) -> str:
"""Get the key algorithm."""
name = StrBuffer()
Expand All @@ -197,6 +202,7 @@ def get_algorithm(self, index: int) -> str:
)
return str(name)

@entry_cache
def get_name(self, index: int) -> str:
"""Get the key name."""
name = StrBuffer()
Expand All @@ -209,6 +215,7 @@ def get_name(self, index: int) -> str:
)
return str(name)

@entry_cache
def get_metadata(self, index: int) -> str:
"""Get for the key metadata."""
metadata = StrBuffer()
Expand All @@ -221,6 +228,7 @@ def get_metadata(self, index: int) -> str:
)
return str(metadata)

@entry_cache
def get_tags(self, index: int) -> dict:
"""Get the key tags."""
tags = StrBuffer()
Expand Down
22 changes: 22 additions & 0 deletions wrappers/python/aries_askar/bindings/lib.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Library instance and allocated buffer handling."""

import asyncio
import functools
import itertools
import logging
import os
Expand Down Expand Up @@ -48,6 +49,27 @@
}


def entry_cache(fn):
"""Cache results for properties of individual entries."""

@functools.wraps(fn)
def wrapper(self, index: int):
if not hasattr(self, "_ecache"):
setattr(self, "_ecache", {})
cache = self._ecache
ckey = (fn, index)
if ckey in cache:
return cache[ckey]
res = fn(self, index)
cache[ckey] = res
if isinstance(res, dict):
# make sure the cached copy is not mutated
res = dict(res)
return res

return wrapper


def _convert_log_level(level: Union[str, int, None]):
if level is None or level == "-1":
return -1
Expand Down
13 changes: 1 addition & 12 deletions wrappers/python/aries_askar/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
except ImportError:
import json

from functools import lru_cache
from typing import Optional, Sequence, Union

from . import bindings
Expand All @@ -32,25 +31,21 @@ def __init__(self, lst: EntryListHandle, pos: int):
self._pos = pos

@property
@lru_cache(maxsize=None)
def category(self) -> str:
"""Accessor for the entry category."""
return self._list.get_category(self._pos)

@property
@lru_cache(maxsize=None)
def name(self) -> str:
"""Accessor for the entry name."""
return self._list.get_name(self._pos)

@property
@lru_cache(maxsize=None)
def value(self) -> bytes:
"""Accessor for the entry value."""
return bytes(self.raw_value)

@property
@lru_cache(maxsize=None)
def raw_value(self) -> memoryview:
"""Accessor for the entry raw value."""
return self._list.get_value(self._pos)
Expand All @@ -61,7 +56,6 @@ def value_json(self) -> dict:
return json.loads(self.value)

@property
@lru_cache(maxsize=None)
def tags(self) -> dict:
"""Accessor for the entry tags."""
return self._list.get_tags(self._pos)
Expand Down Expand Up @@ -152,31 +146,26 @@ def __init__(self, lst: KeyEntryListHandle, pos: int):
self._pos = pos

@property
@lru_cache(maxsize=None)
def algorithm(self) -> str:
"""Accessor for the key entry algorithm."""
return self._list.get_algorithm(self._pos)

@property
@lru_cache(maxsize=None)
def name(self) -> str:
"""Accessor for the key entry name."""
return self._list.get_name(self._pos)

@property
@lru_cache(maxsize=None)
def metadata(self) -> str:
"""Accessor for the key entry metadata."""
return self._list.get_metadata(self._pos)

@property
@lru_cache(maxsize=None)
def key(self) -> Key:
"""Accessor for the entry metadata."""
"""Accessor for the key instance."""
return Key(self._list.load_key(self._pos))

@property
@lru_cache(maxsize=None)
def tags(self) -> dict:
"""Accessor for the entry tags."""
return self._list.get_tags(self._pos)
Expand Down

0 comments on commit d502f4a

Please sign in to comment.