定制 damimpr/cache-multi-layer 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

damimpr/cache-multi-layer

Composer 安装命令:

composer require damimpr/cache-multi-layer

包简介

PHP library used for fast management of a single cache system, or management of multiple systems with priority levels.v

README 文档

README

PHP library used for fast management of a single cache system, or management of multiple systems with priority levels.

The management of a single system works like a mask and allows transparent use of the cache by abstracting which one you want to use.

Multi-level management is conceived as a vertical hierarchy, where the highest priority is at the top, and lower priorities are gradually lower down. Write operations are performed on all levels. Read operations are performed by reading from the cache system with the highest priority, and if the data is not found, the next system is read. Whenever a read from a system is successful, the cache systems with higher priority that did not return any value are updated.

The currently implemented caches are:

  • Apcu
  • Redis
  • Memcache

All cache systems support all primitive types (int, float, string) and instances of classes that implement the Cacheable interface. Arrays are also supported, provided that the elements they contain are as described above.

Install

composer require damimpr/cache-multi-layer

Usage examples

Single cache

example with redis cache

use CacheMultiLayer\Enum\CacheEnum;
use CacheMultiLayer\Service\Cache;

/*
 * creates the redis instance located on the host “redis-server” 
 * with a default TTL of 60 seconds 
 */ 
$cache = Cache::factory(CacheEnum::REDIS, 60, ['server_address' => 'redis-server']);


// set new value
$x = 8;
$key = 'test_key';
$res = $cache->set($key, $x);

// get
$y = $cache->get($key); // the value of $y is 8



//test with object

$foo = new Foo(); //Foo implements Cacheable
$foo->x = 5;
$keyFoo = 'key_foo';
$resFoo = $cache->set($keyFoo, $foo);
$fooCache = $cache->get($keyFoo);

$testEqual = $keyFoo === $foo; //$testEquals could be false
$testEqualX = $keyFoo->x === $foo->x; //testEqualX is true

Multi cache level

Example with apcu cache used with maximum priority and redis cache used with lower priority

use CacheMultiLayer\Enum\CacheEnum;
use CacheMultiLayer\Service\Cache;
use CacheMultiLayer\Service\CacheConfiguration;
use CacheMultiLayer\Service\CacheManager;

/*
 * creates the apcu instance as the highest priority cache and then
 * creates the redis instance located on the host ‘redis-server’ with a default TTL of 60 seconds with lower priority
 */
$cacheManager = CacheManager::factory();
$cacheManager->appendCache(Cache::factory(CacheEnum::APCU, 10));
$cacheManager->appendCache(Cache::factory(CacheEnum::REDIS, 65, ['server_address' => 'redis-server']));


// it's the same using cache configuration
 
$cacheConfiguration = new CacheConfiguration();
$cacheConfiguration->appendCacheLevel(CacheEnum::APCU, 10);
$cacheConfiguration->appendCacheLevel(CacheEnum::REDIS, 65, ['server_address' => 'redis-server']);
$cacheManager = CacheManager::factory($cacheConfiguration);


// set new value
$x = 8;
$key = 'test_key';
$res = $cacheManager->set($key, $x);

//wait 15 seconds
sleep(15); 


$y = $cacheManager->get($key); 
// the value of $y is 8 read from redis, and apcu, which had expired, has been refreshed

Contribuiting

If you would like to contribute to this library, there is a docker you can use in development.

The docker image has been developed to execute commands, which are executable through the commands file, and are listed below:

  • bash commands test-sw # Running the phpunit test suite
  • bash commands update-vendor # Update composer packages
  • bash commands php-cs-fixer # Format the code according to the PSR-12 standard using php-cs-fixer
  • bash commands rector # Run powerful php tool useful for refactoring
  • bash commands phpstan # Run powerful php tool useful finding bugs 
  • bash commands sh # Run the sh shell in the container

Credits

Powerful tools used:

统计信息

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

GitHub 信息

  • Stars: 3
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2025-10-07

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固