HEX
Server: LiteSpeed
System: Linux houston.panomity.com 6.8.0-100-generic #100-Ubuntu SMP PREEMPT_DYNAMIC Tue Jan 13 16:40:06 UTC 2026 x86_64
User: nudepix (1011)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: //opt/agenticSeek/llm_server/sources/cache.py
import os
import json
from pathlib import Path

class Cache:
    def __init__(self, cache_dir='.cache', cache_file='messages.json'):
        self.cache_dir = Path(cache_dir)
        self.cache_file = self.cache_dir / cache_file
        self.cache_dir.mkdir(parents=True, exist_ok=True)
        if not self.cache_file.exists():
            with open(self.cache_file, 'w') as f:
                json.dump([], f)

        with open(self.cache_file, 'r') as f:
            self.cache = set(json.load(f))

    def add_message_pair(self, user_message: str, assistant_message: str):
        """Add a user/assistant pair to the cache if not present."""
        if not any(entry["user"] == user_message for entry in self.cache):
            self.cache.append({"user": user_message, "assistant": assistant_message})
            self._save()

    def is_cached(self, user_message: str) -> bool:
        """Check if a user msg is cached."""
        return any(entry["user"] == user_message for entry in self.cache)

    def get_cached_response(self, user_message: str) -> str | None:
        """Return the assistant response to a user message if cached."""
        for entry in self.cache:
            if entry["user"] == user_message:
                return entry["assistant"]
        return None

    def _save(self):
        with open(self.cache_file, 'w') as f:
            json.dump(self.cache, f, indent=2)