Skip to content

Client cache never evicts expired entries #26

Open

Description

@sujal-maheshwari2004

Summary

With cache_ttl set, every distinct URI passed to get() and every distinct prefix passed to list() adds an entry to self._cache. Expired entries are ignored on lookup but never deleted; the only removal is the pop in the subscription handler for an accepted reload. The cache grows without bound.

Impact

Long-running processes that touch many URIs or prefixes (multi-tenant services building URIs per customer, tools that browse with many list() prefixes) hold every response they have ever fetched, including full templates, for the life of the process.

Steps to reproduce

  1. pip install priompt-sdk
  2. Run (uses a fake stub so no server is needed):
    from priompt_sdk import PromptClient
    import priompt_sdk.client as mod
    
    class Stub:
        def GetPrompt(self, req, metadata=None):
            return req.uri
    
    now = [0.0]
    mod.time.monotonic = lambda: now[0]
    c = PromptClient(host="localhost:1", cache_ttl=1)
    c._stub = Stub()
    for i in range(10_000):
        c.get(f"priompt://acme/p{i}")
        now[0] += 2      # every previous entry is now expired
    print(len(c._cache))

Actual: 10000, although at most one entry is still valid.

Expected behavior

Expired entries are removed, and the cache size stays bounded (optionally by a configurable maximum).

Where to look

Suggested approach

  • Factor the lookup into a small helper that deletes an expired entry when it finds one.
  • Add an optional cache_max_entries (e.g. default 1024) and evict the oldest entry on insert, using an OrderedDict with move_to_end on hit (LRU).
  • Note: the subscription handler runs on another thread; a plain dict is fine for single operations, but if the helper becomes read-modify-write, guard it with a threading.Lock.

Getting started

  • Set up: pip install -e . pytest, then run pytest -q from the repo root.
  • The cache lives in PromptClient in priompt_sdk/client.py. tests/test_client.py already has a FakeStub and a clock fixture that controls time.monotonic, so TTL tests are deterministic; add eviction tests next to the existing cache tests.

Acceptance criteria

  • Expired entries are removed from _cache for both get() and list() keys.
  • The cache has a bounded size, with eviction tested.
  • Existing cache tests still pass.

Activity

  1. added
    bug
    good first issue
    on Nov 14, 2026
  2. techkayacodes commented on Dec 2, 2026

    @techkayacodes
    Contributor

    Hi! I'd like to claim this issue — I've opened PR #35 switching PromptClient._cache to a lock-guarded OrderedDict with expired-entry eviction on lookup/insert, an optional cache_max_entries=1024 LRU cap, and deterministic TTL/LRU unit tests. Happy to adjust anything after assignment!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    buggood first issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions