exos/hybridcache 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

exos/hybridcache

Composer 安装命令:

composer require exos/hybridcache

包简介

PHP Cache manager with multiple storages types

README 文档

README

Example of usage:

<?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);

Multiples storages

Currently, Hybrid Cache includes drivers for caching on Disk, Memcache and Redis, but you can extend it creating your own key/value storage connector, implementing the Hybrid\StorageMedia interface.

Storage instances can be assigned for reading and writing, and you can have multiple different storage systems.

Replication

Consider you have the following scenario using 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

You can set it up like this:

<?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 );

By default, HybridCache uses a hash-based mechanism to balance load on multiple storage media. In this case, each server will receive the same amount of petitions. In a replication scenario a random petition distribution is more effective and HybridCache can try using another server on the list if the first one doesn't return anything.

To change the balancing method you should change the balanceMethod property of the instance:

<?php

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

However, if you wish to apply the change globally for all new instances of the HybridCache class, you can define a constant:

<?php

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

Note that the random method is extremely inefficient when there are several master servers.

Horizontal scalability

Horizontal scalability can be achieved with any storage media and sometimes it's better than replication.

In this case you define a number of storage media and the balancing is done using a hash generated by the key-value pair. All backend use the same algorithm, so they will all fetch the cache on the corresponding storage medium. It is very important to define the storages in the same order in all the backend servers, since the algorithm is based on the order and amount of them.

By default HybridCache uses a hash balance method, but if you want to be sure, you can set it explicitly:

<?php

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

To define a scalation array:

<?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') );

Important: HybridCache doesn't support HA (High Availability) methods in this scenario yet. We expect to implement failover mechanisms on future versions.

Multiple array philosophy

In future versions we're planning to implement groups of arrays that can combine the efficency of scalability with the fault tolerance of replication.

Who use it??

DePaginas

Website with several sections, web directory, classifieds, news and more

http://depaginas.com.ar

Periodico Tribuna

Argentinan digital newspaper

http://periodicotribuna.com.ar/

uWall.tv

Best artist listed in a wall format. Just pick an artist and discover a new experience :)

http://uWall.tv

Taggify

Ad network with improvents and creatives products.

http://taggify.net

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

统计信息

  • 总下载量: 15
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 9
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 9
  • Watchers: 3
  • Forks: 7
  • 开发语言: PHP

其他信息

  • 授权协议: WTFPL
  • 更新时间: 2017-09-09

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固