Enhance Caching System and Optimize Memory Usage with Customizable Storage Support#1016
Conversation
|
Thanks @AlboCode this is an important feat. |
|
@AlboCode great contribution thanks. Unfortunately there are mismatches between HTTP and WS strays in working memory, need to investigate a little more and write many tests. Hopefully this is a step more to make the Cat stateless :D If not I'll write them down in issues, I'm cooked |
|
@pieroit Would be great to have a chat, unfortunately my time shift doesn't help 🥲 |
available tonight |
|
Hello @pieroit . What do you think if I continue on this PR, implementing a I perfectly agree with @AlboCode that the right vision is to have the cache manager pluggable. Despite this consideration, I would suggest having Redis hard-coded as a cache manager, in order to make CheshireCat a scalable solution out-of-the-box, without requiring any additional plugin. I would argument further with some advantages and disadvantages that come to my mind:
Advantages:
I'm not trying to avoid a more complex development. My opinion is sincere. As a proof, If you agree, i will also provide a proposal for the plugin version of cache management. What do you think? |
Redis client as a dependency can be done, do you know if it works also with Valkey? Also having hooks and REST API endpoints for setting the cache like we do with llms and auth seems overkill (I regret having the authhandler in the endpoints) That's why for v2 I would go for a So @lucapiccinelli I think it is worth it to have a redis cache in core, as long as: Thyanks & Peace |
|
@pieroit Thank you for your answer. Yes, look like is it working also with Valkey. 1 - yes it is just something like this redis_host = get_env("CCAT_REDIS_HOST")
redis_port = get_env("CCAT_REDIS_PORT")
redis_db = get_env("CCAT_REDIS_DB")2 - I'm sorry, I really would like to be a maintainer of such a good project, but I can't commit to it. Anyway, digging in the code, I found that it is already really simple to implement your cache manager in a plugin. It is as simple as this: from cat.env import get_env
from cat.mad_hatter.decorators.hook import hook
@hook
def after_cat_bootstrap(cat):
redis_host = get_env("CCAT_REDIS_HOST")
redis_port = get_env("CCAT_REDIS_PORT")
redis_db = get_env("CCAT_REDIS_DB")
from cat.plugins.cc_redis_cache.redis_cache import RedisCache
cat.cache = RedisCache(redis_host, redis_port, redis_db)
print("setting redis cache bla")I can still open a PR to add it to the core, but maintaining it is something that I can't do. Despite that, I found a bug introduced in this commit in the file I know that's not the right place, but as we are here, I add a working draft of the RedisCache implementation. from cat.cache.base_cache import BaseCache
from cat.cache.cache_item import CacheItem
import pickle
import redis
class RedisCache(BaseCache):
def __init__(self, host="localhost", port=6379, db=0):
self._redis = redis.Redis(host=host, port=port, db=db)
def insert(self, cache_item: CacheItem):
encoded_item = pickle.dumps(cache_item)
self._redis.set(
cache_item.key,
encoded_item,
)
def get_item(self, key) -> CacheItem:
cache_item = self._get_and_decode(key)
if not cache_item:
return None
if cache_item.is_expired():
self.delete(key)
return None
return cache_item
def get_value(self, key):
cache_item = self._get_and_decode(key)
if not cache_item:
return None
return cache_item.value
def delete(self, key):
print(f"clearing my conversation for {key}")
self._redis.delete(key)
def _get_and_decode(self, key) -> CacheItem | None:
encoded_item = self._redis.get(key)
if encoded_item:
cache_item = pickle.loads(encoded_item)
return cache_item
else:
return None |
|
I opened a PR to fix the cache bug #1076 |
|
Let's reiterate on the redis cache during v2 development. Thanks |

Description
Add a caching system and optimize memory usage by removing stray_cats from the app state and storing the working memory in the cache.
Related to issue #846
Next, I would like to add a hook to customize the cache initialization, allowing to integrate the preferred storage, such as Redis, through a plugin.
I'm still unsure how to make the TTL of the working_memory customizable. Open to suggestions.
Type of change
Checklist: