Cache data with Laminas Cache: Redis, file, or APCu adapters with the serializer plugin.
Caching with Laminas Cache
use LaminasCacheStorageFactory;
$cache = StorageFactory::factory([
"adapter" => [
"name" => "redis",
"options" => ["server" => ["host"=>"127.0.0.1","port"=>6379], "ttl" => 3600],
],
"plugins" => [
"exception_handler" => ["throw_exceptions" => false],
"serializer",
],
]);
// Store
$cache->setItem("all_posts", $posts);
// Retrieve
$posts = $cache->getItem("all_posts", $success);
if (!$success) {
$posts = $postTable->fetchAll()->toArray();
$cache->setItem("all_posts", $posts);
}
// Delete
$cache->removeItem("all_posts");
$cache->removeItems(["key1", "key2"]);
$cache->flush(); // clear all
// File cache (no Redis needed)
$cache = StorageFactory::factory(["adapter" => ["name"=>"filesystem","options"=>["cache_dir"=>"data/cache","ttl"=>3600]]]);