ihor/cachalot

以合适的方式缓存大量数据(APC、XCache、Memcached、Redis、Couchbase)

2.4 2018-01-02 14:17 UTC

This package is auto-updated.

Last update: 2024-09-21 18:54:52 UTC


README

Cachalot(缓存大量数据)是一个易于使用的缓存库。它只做一件事 - 返回缓存的回调结果。

安装

在您的 composer.json 文件中定义以下要求

"require": {
    "ihor/cachalot": "2.3"
}

用法

// With Cachalot cache you can easily cache results of different types of functions
$cache = new \Cachalot\ArrayCache();

// built-in functions
$length = $cache->getCached('strlen', ['hello world']); 

// user defined functions
$unique = $cache->getCached('uniqueValues', [[1, 2, 3, 1, 2, 3]]);

// static methods
$result = $cache->getCached(['Calculator', 'subtract'], [1, 2]);

// instance methods
$square = $cache->getCached([new Calculator(), 'square'], [5]);

// anonymous functions
$reason = $cache->getCached($getErrorReason, [], \Cachalot\Cache::ONE_DAY, 'error-reason');

// callable objects
$trimed = $cache->getCached(new Trimmer(), [' hello world ']);

参考

缓存API

getCached($callback, array $args = array(), $expireIn = 0, $suffix = null, $useSuffixAsKey = false)

返回缓存的 $callback 结果

$callback 是我们想要缓存结果的函数(可调用)
$args 是传递给 $callback 的参数
$expireIn 以秒为单位设置缓存 TTL
$suffix 当回调是一个匿名函数时需要,$useSuffixAsKey 当为 true 时,缓存后缀将用作缓存键

$length = $cache->getCached('strlen', ['hello world']);

为了能够在需要时将 Cachalot 作为常规缓存库使用,它包含经典缓存方法

contains($key)

如果缓存包含具有给定键的条目,则返回 true

if ($cache->contains('lastVisit')) {
    echo 'This is not the first visit';
}
get($key)

通过键返回缓存的值,如果对于给定键没有缓存条目,则返回 false

if ($lastVisitDate = $cache->get('lastVisit')) {
    echo sprintf('Last visit was at %s', $lastVisitDate);
}
set($key, $value, $expireIn = 0)

通过键缓存值。当 $expireIn = 0 时,值将永久缓存

$cache->set('lastVisit', time());
delete($key)

通过键删除缓存条目

$cache->delete('lastVisit');
clear()

删除所有缓存条目

$cache->clear();

后端

Cachalot\ApcCache

将数据存储在 APC

$cache = new Cachalot\ApcCache();
Cachalot\XcacheCache

将数据存储在 Xcache

$cache = new Cachalot\XcacheCache();
Cachalot\MemcacheCache

使用 Memcache PHP 扩展Memcached 中存储数据

$memcache = new \Memcache();
$memcache->connect('unix:///usr/local/var/run/memcached.sock', 0);

$cache = new \Cachalot\MemcacheCache($memcache);
Cachalot\MemcachedCache

使用 Memcached PHP 扩展Memcached 中存储数据

$memcached = new \Memcached();
$memcached->addServer('/usr/local/var/run/memcached.sock', 0);

$cache = new \Cachalot\MemcachedCache($memcached);
Cachalot\RedisCache

将数据存储在 Redis

$redis = new \Redis();
$redis->connect('127.0.0.1');
$redis->select(1);

$cache = new \Cachalot\RedisCache($redis);
Cachalot\CouchbaseCache

使用 Couchbase PHP SDK 1.xCouchbase 中存储数据

$couchbase = new \Couchbase('127.0.0.1', '', '', 'default');

$cache = new \Cachalot\CouchbaseCache($couchbase);
Cachalot\Couchbase2Cache

使用 Couchbase PHP SDK 2.xCouchbase 中存储数据

$cluster = new \CouchbaseCluster('couchbase://localhost');
$bucket = $cluster->openBucket('default');

$cache = new \Cachalot\Couchbase2Cache($bucket);
Cachalot\ArrayCache

在 PHP 数组中存储数据

$cache = new \Cachalot\ArrayCache();
Cachalot\BlackholeCache

从不存储任何数据

$cache = new \Cachalot\BlackholeCache();