exos/hybridcache

支持多种存储类型的PHP缓存管理器

0.9.7 2017-09-09 01:28 UTC

This package is not auto-updated.

Last update: 2024-09-24 17:06:22 UTC


README

使用示例

<?php

// require init.php for autoload classes
require('vendor/autoload.php');

use Hybrid\Cache;

// Use other name for no conflicts with Memcache Class
use Hybrid\Storages\Memcache as MemcacheStorage;

// add a MediaStorage to the Cache class:
Cache::addStorageMedia( new MemcacheStorage('localhost') );

// create a cache instance, with an identifier (can be a lot of values):
$cache = Cache::create(__FILE__, 'list top users');

// check if cache exists and is aviable


$data = $cache->getCacheOr(true, function ($cache) {
    // make your heavy processing.... (saving the result in a variable)
    return $result;
});

// Or...

if ($data = $cache->getCache(true)) {
   // dump cached data
   echo $data;
   // stop the script (or the method, ect)
   exit(0);
} else {
   // set the cache status as "saving" (to avoid duplicating entries)
   $cache->setStatusSaving();
}

// make your heavy processing.... (saving the result in a variable)

// dump the result
echo $result;

// cache the result
$cache->save($result);

多种存储

目前,Hybrid Cache包括对磁盘、Memcache和Redis的缓存驱动程序,但您可以通过创建自己的键/值存储连接器并实现Hybrid\StorageMedia接口来扩展它。

存储实例可以被分配用于读写,并且您可以拥有多个不同的存储系统。

复制

考虑以下使用Redis的场景

 10.1.30.1   Redis master
       10.1.30.2  Redis replican
       10.1.30.3  Redis replican
       10.1.30.4  Redis replican
       10.1.30.5  Redis replican

您可以这样设置

<?php

use Hybrid\Storages\Redis as RedisStorage;

// Define Redis server for write only (master)
Cache::addStorageMedia( new RedisStorage('10.1.30.1'), Cache::FOR_WRITE );

// Define the rest of servers for read only
Cache::addStorageMedia( new RedisStorage('10.1.30.2'), Cache::FOR_READ );
Cache::addStorageMedia( new RedisStorage('10.1.30.3'), Cache::FOR_READ );
Cache::addStorageMedia( new RedisStorage('10.1.30.4'), Cache::FOR_READ );
Cache::addStorageMedia( new RedisStorage('10.1.30.5'), Cache::FOR_READ );

默认情况下,HybridCache使用基于哈希的机制在多个存储媒体之间平衡负载。在这种情况下,每个服务器将接收相同数量的请求。在复制场景中,随机请求分配更为有效,并且如果第一个服务器没有返回任何内容,HybridCache可以尝试使用列表中的另一个服务器。

要更改平衡方法,您应该更改实例的balanceMethod属性

<?php

$cache = Cache::create('key');
$cache->balanceMethod = Cache::B_RANDOM;

然而,如果您希望为HybridCache类的所有新实例全局应用此更改,您可以定义一个常量

<?php

define('CACHE_BALANCE_METHOD',Cache::B_RANDOM);

注意,当有多个主服务器时,随机方法效率极低。

水平扩展性

可以使用任何存储媒体实现水平扩展性,有时这比复制更好。

在这种情况下,您定义一组存储媒体,平衡是通过键值对的哈希生成的。所有后端都使用相同的算法,因此它们将在相应的存储媒体上检索缓存。定义所有后端服务器中的存储顺序非常重要,因为算法基于顺序和数量。

默认情况下,HybridCache使用哈希平衡方法,但为了确保,您可以显式设置它

<?php

define('CACHE_BALANCE_METHOD',Cache::B_HASH);

要定义扩展数组

<?php

use Hybrid\Storages\Memcache as MemcacheStorage;

Cache::addStorageMedia( new MemcacheStorage('10.1.30.1') );
Cache::addStorageMedia( new MemcacheStorage('10.1.30.2') );
Cache::addStorageMedia( new MemcacheStorage('10.1.30.3') );
Cache::addStorageMedia( new MemcacheStorage('10.1.30.4') );
Cache::addStorageMedia( new MemcacheStorage('10.1.30.5') );

重要提示:HybridCache在此场景中尚不支持HA(高可用性)方法。我们预计将在未来的版本中实现故障转移机制。

多个数组哲学

在未来的版本中,我们计划实现可以结合扩展性和复制容错性的数组组。

谁在使用它??

DePaginas

具有多个部分、网站目录、分类广告、新闻等的多功能网站

http://depaginas.com.ar

Periodico Tribuna

阿根廷数字报纸

http://periodicotribuna.com.ar/

uWall.tv

以墙壁格式列出最佳艺术家。只需选择一个艺术家,就可以发现新的体验 :)

http://uWall.tv

Taggify

具有改进和创意产品的广告网络。

http://taggify.net

                                               README fixed by Andres Gattinoni
                                               http://www.tail-f.com.ar/