Skip to content

Commit

Permalink
Merge pull request #1036 from Aiven-Open/keejon/fix-lru-cache
Browse files Browse the repository at this point in the history
fix: use async lru_cache
  • Loading branch information
jjaakola-aiven authored Feb 4, 2025
2 parents 6e61dfe + 480fee7 commit 74e8ea9
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 2 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ dependencies = [
"accept-types < 1",
"aiohttp < 4",
"aiokafka == 0.10.0",
"async_lru",
"cachetools == 5.3.3",
"confluent-kafka == 2.4.0",
"isodate < 1",
Expand Down
3 changes: 3 additions & 0 deletions requirements/requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ aiosignal==1.3.1
# via aiohttp
anyio==4.4.0
# via watchfiles
async-lru==2.0.4
# via karapace (/karapace/pyproject.toml)
async-timeout==4.0.3
# via
# aiohttp
Expand Down Expand Up @@ -208,6 +210,7 @@ tomli==2.0.1
typing-extensions==4.12.2
# via
# anyio
# async-lru
# karapace (/karapace/pyproject.toml)
ujson==5.10.0
# via karapace (/karapace/pyproject.toml)
Expand Down
3 changes: 3 additions & 0 deletions requirements/requirements-typing.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ aiosignal==1.3.1
# via aiohttp
anyio==4.5.0
# via watchfiles
async-lru==2.0.4
# via karapace (/karapace/pyproject.toml)
async-timeout==4.0.3
# via
# aiohttp
Expand Down Expand Up @@ -113,6 +115,7 @@ types-protobuf==3.20.4.6
typing-extensions==4.12.2
# via
# anyio
# async-lru
# karapace (/karapace/pyproject.toml)
# multidict
# mypy
Expand Down
3 changes: 3 additions & 0 deletions requirements/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ aiosignal==1.3.1
# via aiohttp
anyio==4.4.0
# via watchfiles
async-lru==2.0.4
# via karapace (/karapace/pyproject.toml)
async-timeout==4.0.3
# via
# aiohttp
Expand Down Expand Up @@ -96,6 +98,7 @@ tenacity==9.0.0
typing-extensions==4.12.2
# via
# anyio
# async-lru
# karapace (/karapace/pyproject.toml)
ujson==5.10.0
# via karapace (/karapace/pyproject.toml)
Expand Down
4 changes: 2 additions & 2 deletions src/karapace/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
from __future__ import annotations

from aiohttp import BasicAuth
from async_lru import alru_cache
from avro.io import BinaryDecoder, BinaryEncoder, DatumReader, DatumWriter
from cachetools import TTLCache
from collections.abc import MutableMapping
from functools import lru_cache
from google.protobuf.message import DecodeError
from jsonschema import ValidationError
from karapace.client import Client
Expand Down Expand Up @@ -180,7 +180,7 @@ async def _get_schema_recursive(
except InvalidSchema as e:
raise SchemaRetrievalError(f"Failed to parse schema string from response: {json_result}") from e

@lru_cache(maxsize=100)
@alru_cache(maxsize=100)
async def get_schema(
self,
subject: Subject,
Expand Down
6 changes: 6 additions & 0 deletions tests/integration/test_client_protobuf.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ async def test_remote_client_protobuf(registry_async_client):
assert sc_id >= 0
stored_schema, _ = await reg_cli.get_schema_for_id(sc_id)
assert stored_schema == schema_protobuf, f"stored schema {stored_schema} is not {schema_protobuf}"

stored_id, stored_schema, _ = await reg_cli.get_schema(subject)
assert stored_id == sc_id
assert stored_schema == schema_protobuf

# get same schema a second time to hit cache
stored_id, stored_schema, _ = await reg_cli.get_schema(subject)
assert stored_id == sc_id
assert stored_schema == schema_protobuf
Expand Down

0 comments on commit 74e8ea9

Please sign in to comment.