npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@layrx/ai-cache

v0.1.3

Published

AI request/response cache for LayrX based on context packages and queries

Readme

@layrx/ai-cache

AI Cache Manager for LayrX. Caches AI request and response data based on user queries and context packages, independently of any LLM provider.

Architecture

User Query + Context Package
            ↓
      CacheHasher (deterministic key)
            ↓
       CacheStore (SQLite)
            ↓
     Hit? → Return response
     Miss? → Ready for future LLM call
            ↓
       Store response (set)

| File | Responsibility | |------|----------------| | AICacheManager.ts | Orchestrator and public API | | CacheStore.ts | SQLite persistence | | CacheHasher.ts | Key generation and context hashing | | CachePolicy.ts | TTL and eviction policy | | CacheCleaner.ts | Expired entry cleanup | | CacheStatistics.ts | Aggregate metrics | | CacheTypes.ts | Shared types |

Key generation

Cache keys are SHA-256 digests of:

  • Normalised user query
  • Context package hash (canonical JSON)
  • Model name
  • Model version
  • Prompt version

Equivalent context packages produce identical keys regardless of property order.

TTL strategy

  • Default TTL: 24 hours
  • Configurable per manager (policy.defaultTtlMs) or per entry (ttlMs on set)
  • Expired entries are treated as cache misses
  • Optional eviction on lookup (evictOnLookup: true, default)
  • clearExpired() removes all expired rows

Public API

import {
  get,
  set,
  deleteCache,
  clearExpired,
  clearAll,
  getStatistics,
} from '@layrx/ai-cache';

const context = {
  repositoryId: 'repo-id',
  summary: 'Relevant code context',
  entities: [{ entityId: '1', entityType: 'Function', name: 'Widget' }],
};

// Lookup
const cached = get({
  query: 'Explain Widget',
  context,
  model: 'gpt-4',
  modelVersion: '2024-01',
  promptVersion: 'v1',
});

if (cached) {
  console.log(cached.response);
} else {
  // Future: call LLM, then store response
  set({
    query: 'Explain Widget',
    context,
    model: 'gpt-4',
    modelVersion: '2024-01',
    promptVersion: 'v1',
    response: 'Widget is a React component...',
    tokenUsage: { promptTokens: 100, completionTokens: 50, totalTokens: 150 },
  });
}

Class API: new AICacheManager({ databasePath, policy })

Schema

ai_cache

cache_key, query, context_hash, model, model_version, prompt_version, response, token_usage, created_at, expires_at, last_accessed, hit_count

cache_statistics

Singleton row tracking cache_hits and cache_misses

Statistics

  • totalEntries
  • cacheHits / cacheMisses
  • hitRate
  • expiredEntries
  • databaseSize

Logging

Cache lookup → Cache hit / Cache miss → Entry created → Entry expired → Cache cleaned → Errors

Future distributed cache support

  • Stable cache_key maps directly to Redis/Memcached keys
  • context_hash enables cache invalidation when context changes
  • CacheStore can be swapped for a DistributedCacheStore adapter
  • TTL maps to Redis EXPIRE; hit counts via HINCRBY
  • SQLite remains local L1; Redis as shared L2 across agents

Tests

npm run test --workspace=@layrx/ai-cache

Flow

User Query → Context Package → Generate Cache Key → Lookup SQLite
  → Hit: Return Response
  → Miss: Ready for LLM → Store Response

Do not use this package for LLM calls, HTTP APIs, or security scanning.