Skip to content

Commit 4c1c474

Browse files
committed
test
1 parent 5b280f9 commit 4c1c474

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

datamodel/abstract.py

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,25 @@ class ModelMeta(type):
156156
__aliases__: Dict
157157
__primary_keys__: List
158158
# Class-level cache
159-
_base_class_cache = {}
159+
_base_class_cache = OrderedDict()
160+
_MAX_CACHE_SIZE = 512 # size limit
161+
162+
@classmethod
163+
def _cache_get(cls, key):
164+
"""Get item from cache with LRU behavior."""
165+
if key not in cls._base_class_cache:
166+
return None
167+
value = cls._base_class_cache.pop(key)
168+
cls._base_class_cache[key] = value
169+
return value
170+
171+
@classmethod
172+
def _cache_set(cls, key, value):
173+
"""Set item in cache with LRU eviction."""
174+
# If cache is full, remove oldest item (first in OrderedDict)
175+
if len(cls._base_class_cache) >= cls._MAX_CACHE_SIZE:
176+
cls._base_class_cache.popitem(last=False)
177+
cls._base_class_cache[key] = value
160178

161179
@staticmethod
162180
def _initialize_fields(attrs, annotations, strict):
@@ -295,21 +313,25 @@ def __new__(cls, name, bases, attrs, **kwargs): # noqa
295313
annotations = attrs.get('__annotations__', {})
296314
_strict_ = False
297315
cols = OrderedDict()
316+
317+
# Base class constructor
298318
base_key = (name, tuple(bases), tuple(sorted(annotations.items())))
299319
with contextlib.suppress(TypeError, AttributeError, KeyError):
300320
_strict_ = attrs['Meta'].strict
301321

302-
if base_key in cls._base_class_cache:
322+
# Use LRU get method
323+
cached = cls._cache_get(base_key)
324+
if cached:
325+
# if base_key in cls._base_class_cache:
303326
# Check the Cache First:
304-
cached = cls._base_class_cache[base_key]
327+
# cached = cls._base_class_cache[base_key]
305328
cols = cached['cols'].copy()
306329
_types = cached['types'].copy()
307330
_typing_args = cached['_typing_args'].copy()
308331
aliases = cached['aliases'].copy()
309332
primary_keys = cached['primary_keys'].copy()
310333
else:
311334
# Compute field from Bases:
312-
cols = OrderedDict()
313335
_types = {}
314336
_typing_args = {}
315337
aliases = {}
@@ -340,13 +362,21 @@ def __new__(cls, name, bases, attrs, **kwargs): # noqa
340362
primary_keys.extend(nw_primary_keys)
341363

342364
# Store computed results in cache
343-
cls._base_class_cache[base_key] = {
365+
# cls._base_class_cache[base_key] = {
366+
# 'cols': cols.copy(),
367+
# 'types': _types.copy(),
368+
# '_typing_args': _typing_args.copy(),
369+
# 'aliases': aliases.copy(),
370+
# 'primary_keys': primary_keys.copy(),
371+
# }
372+
cache_entry = {
344373
'cols': cols.copy(),
345374
'types': _types.copy(),
346375
'_typing_args': _typing_args.copy(),
347376
'aliases': aliases.copy(),
348377
'primary_keys': primary_keys.copy(),
349378
}
379+
cls._cache_set(base_key, cache_entry)
350380

351381
_columns = cols.keys()
352382
cls.__slots__ = tuple(_columns)

0 commit comments

Comments
 (0)