zheltikov/php-memoize-redis

1.0.3 2022-04-18 15:19 UTC

This package is auto-updated.

Last update: 2024-09-18 20:44:02 UTC


README

一个由 memoization 缓存提供者,后端支持 Redis

安装

此库通过 Composer 可用

$ composer require zheltikov/php-memoize-redis

使用

您可以创建一个新的 RedisCache 实例,配置其 Redis 客户端,并将其提供给缓存包装器。

<?php

require_once(__DIR__ . '/../vendor/autoload.php');

use Zheltikov\Memoize\RedisCache;

use function Zheltikov\Memoize\wrap;

// Create a new instance
$cache = new RedisCache();

$cache->setHashName('my_hash_name');    // the hash used to cache the results

// Configure the Redis object
$cache->getRedis()
    // For example, change the serializer to be used.
    // By default, `Redis::SERIALIZER_PHP` will be used by the cache.
    ->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_JSON);

// or you can supply your own Redis object
$my_redis_object = new Redis();
// do some configuration here...
// set it to the cache
$cache->setRedis($my_redis_object);

// finally, use it when memoizing:
function my_expensive_function() { /* ... */ }
$wrapped = wrap('my_expensive_function', $cache);
// enjoy!

正如您可能已经注意到的,此缓存提供者作为一个进程间缓存,可以提供惊人的速度提升!