onnerby / nestedcache
A nested cache that can be invalidated through relations and/or hierarchy
Installs: 4
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 1
pkg:composer/onnerby/nestedcache
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16
- phpunit/phpunit: ^8
This package is auto-updated.
Last update: 2025-10-06 20:19:49 UTC
README
A nested cache that can be invalidated through relations and/or hierarchy
Lets start with an simple example:
\Doe\NestedCache::init(); if ($cache = \Doe\NestedCache::get(['book-gallery'])) { // Do stuff with book gallery cache } else { \Doe\NestedCache::start(['book-gallery']); $newCache = 'Lots of generated data'; ... \Doe\NestedCache::end($newCache); } // Remove the cache \Doe\NestedCache::invalidate(['book-gallery']);
How about nesting?
Let's try nesting our book gallery by caching books inside
\Doe\NestedCache::init(); if ($cache = \Doe\NestedCache::get(['book-gallery'])) { // Do stuff with book gallery cache } else { \Doe\NestedCache::start(['book-gallery']); $newCache = ""; foreach ($books as $book) { if (!($bookOutput = \Doe\NestedCache::get(['book-listing', $book->id]))) { $bookOutput = "book: " . $book->name . "\n"; \Doe\NestedCache::set(['book-listing', $book->id], $bookOutput); } $newCache .= $bookOutput; } \Doe\NestedCache::end($newCache); } // Remove the cache for a single book will also remove cache for "book-gallery" (but not for other book listings) \Doe\NestedCache::invalidate(['book-listing', 123]);
Engines
There are so far only 2 options.
- APCu: (default)
\Doe\NestedCache::init(['engine' => 'APCu']);
- TempMemory: A in memory cache only for current request (mostly for debug purpose)
\Doe\NestedCache::init(['engine' => 'TempMemory']);