onnerby / nestedcache
一种可以通过关系和/或层次结构进行失效的嵌套缓存
1.0.1
2020-02-05 09:20 UTC
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16
- phpunit/phpunit: ^8
This package is auto-updated.
Last update: 2024-09-05 20:07:30 UTC
README
一种可以通过关系和/或层次结构进行失效的嵌套缓存
让我们从一个简单的例子开始
\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']);
嵌套如何?
让我们尝试通过在内部缓存书籍来嵌套我们的书籍画廊
\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]);
引擎
目前只有两种选项。
- APCu: (默认)
\Doe\NestedCache::init(['engine' => 'APCu']);
- TempMemory: 仅用于当前请求的内存缓存(主要用于调试目的)
\Doe\NestedCache::init(['engine' => 'TempMemory']);