mirror of
https://github.com/meta-llama/llama-stack.git
synced 2025-08-05 18:22:41 +00:00
Optimized number of redis calls
This commit is contained in:
parent
44b3f90d13
commit
9cb73d564b
1 changed files with 14 additions and 11 deletions
|
@ -48,24 +48,27 @@ class RedisKVStoreImpl(KVStore):
|
|||
async def range(self, start_key: str, end_key: str) -> List[str]:
|
||||
start_key = self._namespaced_key(start_key)
|
||||
end_key = self._namespaced_key(end_key)
|
||||
|
||||
result = []
|
||||
cursor = 0
|
||||
pattern = start_key + "*" # Match all keys starting with start_key prefix
|
||||
|
||||
matching_keys = []
|
||||
while True:
|
||||
cursor, keys = await self.redis.scan(cursor, match=pattern)
|
||||
cursor, keys = await self.redis.scan(cursor, match=pattern, count=1000)
|
||||
|
||||
for key in keys:
|
||||
key_str = key.decode("utf-8") if isinstance(key, bytes) else key
|
||||
if start_key <= key_str <= end_key:
|
||||
value = await self.redis.get(key)
|
||||
if value is not None:
|
||||
value_str = (
|
||||
value.decode("utf-8") if isinstance(value, bytes) else value
|
||||
)
|
||||
result.append(value_str)
|
||||
matching_keys.append(key)
|
||||
|
||||
if cursor == 0:
|
||||
break
|
||||
|
||||
return result
|
||||
# Then fetch all values in a single MGET call
|
||||
if matching_keys:
|
||||
values = await self.redis.mget(matching_keys)
|
||||
return [
|
||||
value.decode("utf-8") if isinstance(value, bytes) else value
|
||||
for value in values
|
||||
if value is not None
|
||||
]
|
||||
|
||||
return []
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue