PHPackages                             ykachala/semantic-cache - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Caching](/categories/caching)
4. /
5. ykachala/semantic-cache

ActiveLibrary[Caching](/categories/caching)

ykachala/semantic-cache
=======================

Embedding-similarity response cache for LLM calls. Serve a cached answer when a new prompt is semantically close to a previous one — cutting cost and latency.

00PHP

Since Jun 2Pushed 1mo agoCompare

[ Source](https://github.com/ykachala/semantic-cache)[ Packagist](https://packagist.org/packages/ykachala/semantic-cache)[ RSS](/packages/ykachala-semantic-cache/feed)WikiDiscussions main Synced 1w ago

READMEChangelogDependenciesVersions (1)Used By (0)

Ykachala Semantic Cache
=======================

[](#ykachala-semantic-cache)

> An **embedding-similarity** cache for LLM responses. When a new prompt is *semantically close* to one you've answered before, serve the cached answer instead of paying for — and waiting on — another model call.

[![PHP Version](https://camo.githubusercontent.com/5a2eb3f6217f798b7ca58d59764e8fd3d779752c0cef2b8634034d9e7e4ba92d/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253345253344382e332d373737626234)](https://www.php.net/)[![License](https://camo.githubusercontent.com/f8df3091bbe1149f398a5369b2c39e896766f9f6efba3477c63e9b4aa940ef14/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d677265656e)](LICENSE)

---

Why this exists (the 2026 gap)
------------------------------

[](#why-this-exists-the-2026-gap)

A normal cache keys on an exact string. LLM prompts are almost never byte-identical — *"How do I reset my password?"* and *"I forgot my password, what now?"* are the same question to a user but two different cache keys. So traditional caching gives near-zero hit rate on real LLM traffic, and teams pay full price for what is effectively the same answer thousands of times.

Python has **GPTCache** for exactly this. PHP, as of 2026, has **nothing native** — despite the PHP AI ecosystem (Prism, Neuron, Laravel AI SDK) now being mature enough that *cost* is the live problem. Ykachala Semantic Cache fills that hole.

How it works
------------

[](#how-it-works)

1. Embed the incoming prompt.
2. Search the vector store for the nearest previously-cached prompt.
3. If cosine similarity ≥ your threshold (e.g. `0.95`), **return the stored response** — no LLM call.
4. Otherwise call your model, then store `(embedding, prompt, response)` for next time.

A cheap **exact-match tier** runs first (hash lookup) so identical prompts never even pay for an embedding.

Install
-------

[](#install)

```
composer require ykachala/semantic-cache
```

Quick start
-----------

[](#quick-start)

```
use Ykachala\SemanticCache\SemanticCache;
use Ykachala\SemanticCache\Store\PgVectorStore;

$cache = new SemanticCache(
    embedder:  $yourEmbedder,           // any PHP closure/object that returns a vector
    store:     new PgVectorStore($pdo),
    threshold: 0.95,                    // tune for your risk tolerance
    ttl:       3600,
);

$answer = $cache->remember($prompt, function () use ($prompt, $llm) {
    // Only runs on a miss — this is the call you're trying to avoid
    return $llm->chat($prompt);
});
```

### Inspecting hits

[](#inspecting-hits)

```
$result = $cache->lookup($prompt);

if ($result->hit) {
    logger()->info('semantic cache hit', [
        'similarity' => $result->similarity,   // 0.0 – 1.0
        'matched'    => $result->matchedPrompt,
        'saved'      => $result->estimatedSaving?->format(),
    ]);
}
```

Tiers &amp; safety
------------------

[](#tiers--safety)

TierCostWhen**Exact**hash lookup, ~0byte-identical prompt**Semantic**1 embedding + 1 vector searchsimilar prompt above threshold**Miss**full LLM callnothing close enough- **Namespaces** isolate caches per user/tenant/prompt-template so you never serve one user's answer to another.
- **Threshold tuning** trades hit-rate for correctness — `0.97+` for factual lookups, lower for chit-chat. Ship with metrics so you can tune from real traffic.
- **Stampede protection** — concurrent misses for the same prompt collapse to one call.

Pluggable stores
----------------

[](#pluggable-stores)

```
InMemoryStore   # tests / single process
Psr16Store      # brute-force over any PSR-16 cache, good for small sets
RedisStore      # Redis 8 vector sets
PgVectorStore   # Postgres + pgvector, production default
QdrantStore     # external vector DB at scale

```

Architecture
------------

[](#architecture)

```
src/
├── SemanticCache.php     # remember() / lookup() / put()
├── Lookup.php            # result: hit, similarity, matchedPrompt, saving
├── Embedder/             # EmbedderInterface + adapters
├── Store/                # VectorStore interface + drivers
└── Similarity.php        # cosine / dot-product helpers

```

Roadmap
-------

[](#roadmap)

- Core `SemanticCache` (remember/lookup/put) + `Lookup` result
- Cosine similarity + exact-match tier
- `EmbedderInterface` + adapters
- In-memory + PSR-16 stores (brute force)
- pgvector + Redis + Qdrant stores
- Namespaces, TTL, stampede protection, hit-rate metrics

See [`CLAUDE.md`](CLAUDE.md) for the full phase plan and conventions.

License
-------

[](#license)

MIT

###  Health Score

19

—

LowBetter than 9% of packages

Maintenance59

Moderate activity, may be stable

Popularity0

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity11

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/5c5892a06b5cc3a1f6adaa3fa3d0cb5e9f77c6fc613425df614e2c4778b2e0a7?d=identicon)[joel767443](/maintainers/joel767443)

---

Top Contributors

[![ykachala](https://avatars.githubusercontent.com/u/985991?v=4)](https://github.com/ykachala "ykachala (7 commits)")

---

Tags

aicachecost-optimizationembeddingslatencyllmpgvectorqdrantredissemanticvector

### Embed Badge

![Health badge](/badges/ykachala-semantic-cache/health.svg)

```
[![Health](https://phpackages.com/badges/ykachala-semantic-cache/health.svg)](https://phpackages.com/packages/ykachala-semantic-cache)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
