File tree Expand file tree Collapse file tree 1 file changed +8
-1
lines changed Expand file tree Collapse file tree 1 file changed +8
-1
lines changed Original file line number Diff line number Diff line change 55import logging
66import re
77import threading
8+ from collections import OrderedDict
89from dataclasses import dataclass , field
910from typing import Any
1011
@@ -136,7 +137,10 @@ def __init__(
136137 self ._total_evaluation_time_ms = 0.0
137138
138139 # Policy lookup cache: (model_name, agent) -> AccessPolicy | None
139- self ._policy_cache : dict [tuple [str , str | None ], AccessPolicy | None ] = {}
140+ self ._policy_cache : OrderedDict [tuple [str , str | None ], AccessPolicy | None ] = (
141+ OrderedDict ()
142+ )
143+ self ._policy_cache_size = 128
140144 self ._cache_hits = 0
141145 self ._cache_misses = 0
142146 self ._cache_lock = threading .Lock ()
@@ -244,6 +248,7 @@ def _select_policy(
244248 cache_key = (model_name , agent )
245249 with self ._cache_lock :
246250 if cache_key in self ._policy_cache :
251+ self ._policy_cache .move_to_end (cache_key )
247252 self ._cache_hits += 1
248253 return self ._policy_cache [cache_key ]
249254 self ._cache_misses += 1
@@ -260,6 +265,8 @@ def _select_policy(
260265 # Cache the result
261266 with self ._cache_lock :
262267 self ._policy_cache [cache_key ] = selected_policy
268+ if len (self ._policy_cache ) > self ._policy_cache_size :
269+ self ._policy_cache .popitem (last = False )
263270 return selected_policy
264271
265272 def filter_tool_definitions (
You can’t perform that action at this time.
0 commit comments