@@ -16,20 +16,26 @@ class CacheSection(Enum):
1616
1717
1818class CacheEntry (Generic [_M , _D ]):
19- """Lazy-deserializing cache entry.
19+ """Lazy cache entry that defers both deserialization and data blob loading .
2020
21- Meta and data blobs are deserialized on first property access, not when read from DB.
21+ Only the meta blob is read from the DB initially. The data blob is fetched
22+ lazily on first `.data` access, avoiding the transfer of large blobs when
23+ only meta validation is needed (e.g. on cache misses).
2224 """
2325
2426 def __init__ (
2527 self ,
28+ conn : sqlite3 .Connection ,
29+ section : "CacheSection" ,
30+ entry_name : str ,
2631 meta_blob : Optional [bytes ],
27- data_blob : bytes ,
2832 meta_type : Union [Type [_M ], Tuple [Type [_M ], ...]],
2933 data_type : Union [Type [_D ], Tuple [Type [_D ], ...]],
3034 ) -> None :
35+ self ._conn = conn
36+ self ._section = section
37+ self ._entry_name = entry_name
3138 self ._meta_blob = meta_blob
32- self ._data_blob = data_blob
3339 self ._meta_type = meta_type
3440 self ._data_type = data_type
3541 self ._meta_cache : Optional [_M ] = None
@@ -51,7 +57,13 @@ def meta(self) -> Optional[_M]:
5157 @property
5258 def data (self ) -> _D :
5359 if not self ._data_loaded :
54- result = pickle .loads (self ._data_blob )
60+ row = self ._conn .execute (
61+ f"SELECT data FROM { self ._section .value } WHERE entry_name = ?" ,
62+ (self ._entry_name ,),
63+ ).fetchone ()
64+ if row is None :
65+ raise RuntimeError (f"Cache entry '{ self ._entry_name } ' disappeared from DB" )
66+ result = pickle .loads (row [0 ])
5567 if not isinstance (result , self ._data_type ):
5668 raise TypeError (f"Expected { self ._data_type } but got { type (result )} " )
5769 self ._data_cache = cast (_D , result )
@@ -116,14 +128,14 @@ def read_entry(
116128 data_type : Union [Type [_D ], Tuple [Type [_D ], ...]],
117129 ) -> Optional [CacheEntry [_M , _D ]]:
118130 row = self ._conn .execute (
119- f"SELECT meta, data FROM { section .value } WHERE entry_name = ?" ,
131+ f"SELECT meta FROM { section .value } WHERE entry_name = ?" ,
120132 (entry_name ,),
121133 ).fetchone ()
122134
123135 if row is None :
124136 return None
125137
126- return CacheEntry (row [ 0 ], row [1 ], meta_type , data_type )
138+ return CacheEntry (self . _conn , section , entry_name , row [0 ], meta_type , data_type )
127139
128140 def save_entry (
129141 self ,
0 commit comments