https://github.com/phpnomad/cache
Caching abstraction layer with logger integration and storage-agnostic strategies
https://github.com/phpnomad/cache
cache caching framework php phpnomad platform-agnostic
Last synced: 23 days ago
JSON representation
Caching abstraction layer with logger integration and storage-agnostic strategies
- Host: GitHub
- URL: https://github.com/phpnomad/cache
- Owner: phpnomad
- License: mit
- Created: 2023-10-07T01:08:22.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2026-04-10T02:09:36.000Z (about 2 months ago)
- Last Synced: 2026-04-10T04:11:50.227Z (about 2 months ago)
- Topics: cache, caching, framework, php, phpnomad, platform-agnostic
- Language: PHP
- Homepage: https://phpnomad.com
- Size: 63.5 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# phpnomad/cache
[](https://packagist.org/packages/phpnomad/cache)
[](https://packagist.org/packages/phpnomad/cache)
[](https://packagist.org/packages/phpnomad/cache)
[](https://packagist.org/packages/phpnomad/cache)
`phpnomad/cache` defines the caching contract for PHPNomad applications. It gives you a backend-agnostic `CacheStrategy` interface, a `CachePolicy` contract for deciding what gets cached and for how long, a `CacheableService` that wires the two together with event broadcasting, and helpers for key generation and in-memory instance caching.
The package itself is an abstraction. The concrete backend comes from an integration package like `phpnomad/symfony-cache-integration`, which adapts Symfony's Cache component to this interface. Your application code depends only on the interface, so swapping the backend later never touches your business logic. The cache layer in this package powers [Siren](https://sirenaffiliates.com) and other production PHPNomad apps.
## Installation
```bash
composer require phpnomad/cache
```
You will also need a concrete cache backend such as `phpnomad/symfony-cache-integration` bound to `CacheStrategy` in your container.
## Quick Start
Inject `CacheStrategy` into any service that needs a read-through cache. The interface exposes `get`, `set`, `exists`, `delete`, and `clear`. A miss on `get` throws `CachedItemNotFoundException`, which you catch to fall back to your source of truth.
```php
cache = $cache;
$this->api = $api;
}
public function findById(int $id): Widget
{
$key = 'widget_' . $id;
try {
return $this->cache->get($key);
} catch (CachedItemNotFoundException $e) {
$widget = $this->api->fetchWidget($id);
$this->cache->set($key, $widget, self::CACHE_TTL);
return $widget;
}
}
public function invalidate(int $id): void
{
$this->cache->delete('widget_' . $id);
}
}
```
The container resolves `CacheStrategy` to whichever concrete implementation you've bound at bootstrap time, so `WidgetRepository` never references Symfony, Redis, or any specific cache backend. If you want the read-through pattern pre-built with event broadcasting on misses, you can compose `CacheableService` instead of writing the try/catch yourself.
## Key Concepts
- `CacheStrategy` defines the backend contract with `get`, `set`, `delete`, `exists`, and `clear`.
- `CachePolicy` decides whether a given operation should be cached, computes the TTL, and flags when a write operation should invalidate the cache.
- `CacheableService` composes a `CacheStrategy` and a `CachePolicy` into a read-through helper that broadcasts a `CacheMissed` event on misses, letting event listeners react to cache behavior.
- `HashedCacheKeyFactory` generates cache keys from a context array using a configurable prefix and a hashed suffix, which keeps keys stable and short even when the context is large.
- `WithInstanceCache` is a trait for per-instance in-memory caching, useful for collapsing repeated lookups inside a single request.
## Documentation
Full documentation lives at [phpnomad.com](https://phpnomad.com).
## License
Licensed under the [MIT License](LICENSE.txt).