Skip to content

Commit

Permalink
refactor: optimize cache::clear method pipeline usage
Browse files Browse the repository at this point in the history
  • Loading branch information
bsbodden committed Sep 4, 2024
1 parent 7d37dfa commit 92223b1
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions libs/redis/langchain_redis/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,15 +252,20 @@ def clear(self, **kwargs: Any) -> None:
- If no keys match the prefix, the method will complete without any errors.
"""
cursor = 0
pipe = self.redis.pipeline()
while True:
pipe = self.redis.pipeline()
cursor, keys = self.redis.scan(cursor, match=f"{self.prefix}:*", count=100) # type: ignore[misc]
if keys:
pipe.delete(*keys)
pipe.execute()

if cursor == 0:
break
try:
cursor, keys = self.redis.scan(
cursor, match=f"{self.prefix}:*", count=100
) # type: ignore[misc]
if keys:
pipe.delete(*keys)
pipe.execute()

if cursor == 0:
break
finally:
pipe.reset()


class RedisSemanticCache(BaseCache):
Expand Down

0 comments on commit 92223b1

Please sign in to comment.