Skip to content

Commit 74e8ea9

Browse files
Merge pull request #1036 from Aiven-Open/keejon/fix-lru-cache
fix: use async lru_cache
2 parents 6e61dfe + 480fee7 commit 74e8ea9

6 files changed

Lines changed: 18 additions & 2 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ dependencies = [
1212
"accept-types < 1",
1313
"aiohttp < 4",
1414
"aiokafka == 0.10.0",
15+
"async_lru",
1516
"cachetools == 5.3.3",
1617
"confluent-kafka == 2.4.0",
1718
"isodate < 1",

requirements/requirements-dev.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ aiosignal==1.3.1
1616
# via aiohttp
1717
anyio==4.4.0
1818
# via watchfiles
19+
async-lru==2.0.4
20+
# via karapace (/karapace/pyproject.toml)
1921
async-timeout==4.0.3
2022
# via
2123
# aiohttp
@@ -208,6 +210,7 @@ tomli==2.0.1
208210
typing-extensions==4.12.2
209211
# via
210212
# anyio
213+
# async-lru
211214
# karapace (/karapace/pyproject.toml)
212215
ujson==5.10.0
213216
# via karapace (/karapace/pyproject.toml)

requirements/requirements-typing.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ aiosignal==1.3.1
1616
# via aiohttp
1717
anyio==4.5.0
1818
# via watchfiles
19+
async-lru==2.0.4
20+
# via karapace (/karapace/pyproject.toml)
1921
async-timeout==4.0.3
2022
# via
2123
# aiohttp
@@ -113,6 +115,7 @@ types-protobuf==3.20.4.6
113115
typing-extensions==4.12.2
114116
# via
115117
# anyio
118+
# async-lru
116119
# karapace (/karapace/pyproject.toml)
117120
# multidict
118121
# mypy

requirements/requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ aiosignal==1.3.1
1616
# via aiohttp
1717
anyio==4.4.0
1818
# via watchfiles
19+
async-lru==2.0.4
20+
# via karapace (/karapace/pyproject.toml)
1921
async-timeout==4.0.3
2022
# via
2123
# aiohttp
@@ -96,6 +98,7 @@ tenacity==9.0.0
9698
typing-extensions==4.12.2
9799
# via
98100
# anyio
101+
# async-lru
99102
# karapace (/karapace/pyproject.toml)
100103
ujson==5.10.0
101104
# via karapace (/karapace/pyproject.toml)

src/karapace/serialization.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
from __future__ import annotations
66

77
from aiohttp import BasicAuth
8+
from async_lru import alru_cache
89
from avro.io import BinaryDecoder, BinaryEncoder, DatumReader, DatumWriter
910
from cachetools import TTLCache
1011
from collections.abc import MutableMapping
11-
from functools import lru_cache
1212
from google.protobuf.message import DecodeError
1313
from jsonschema import ValidationError
1414
from karapace.client import Client
@@ -180,7 +180,7 @@ async def _get_schema_recursive(
180180
except InvalidSchema as e:
181181
raise SchemaRetrievalError(f"Failed to parse schema string from response: {json_result}") from e
182182

183-
@lru_cache(maxsize=100)
183+
@alru_cache(maxsize=100)
184184
async def get_schema(
185185
self,
186186
subject: Subject,

tests/integration/test_client_protobuf.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ async def test_remote_client_protobuf(registry_async_client):
1818
assert sc_id >= 0
1919
stored_schema, _ = await reg_cli.get_schema_for_id(sc_id)
2020
assert stored_schema == schema_protobuf, f"stored schema {stored_schema} is not {schema_protobuf}"
21+
22+
stored_id, stored_schema, _ = await reg_cli.get_schema(subject)
23+
assert stored_id == sc_id
24+
assert stored_schema == schema_protobuf
25+
26+
# get same schema a second time to hit cache
2127
stored_id, stored_schema, _ = await reg_cli.get_schema(subject)
2228
assert stored_id == sc_id
2329
assert stored_schema == schema_protobuf

0 commit comments

Comments
 (0)