temant/cache-manager

一个灵活的多适配器缓存系统(Redis、Memcached、文件)。

dev-main 2024-09-07 21:47 UTC

This package is not auto-updated.

Last update: 2024-10-01 12:51:16 UTC


README

Build Status Coverage Status License PHPStan

支持多种适配器的灵活缓存系统,包括Redis、Memcached和基于文件的缓存。这个库兼容PSR-6,使得它易于与现有的PHP应用程序集成。

特性

  • Redis缓存适配器:使用Redis进行高性能缓存。
  • Memcached缓存适配器:使用Memcached进行轻量级和分布式缓存。
  • 单文件PHP缓存适配器:一个简单存储在单个PHP文件中的基于文件的缓存。
  • 灵活的缓存管理器:无缝切换缓存适配器。
  • PSR-6兼容:与任何基于PSR-6的应用程序兼容。

安装

  1. 通过Composer安装

    composer require temant/cache-system
  2. 如果您使用的是相应的适配器,请确保您已安装Redis和Memcached

    • 对于Redis:sudo apt install redis-server
    • 对于Memcached:sudo apt install memcached

使用方法

基本设置

use Temant\Cache\CacheManager;
use Temant\Cache\Adapter\RedisCacheAdapter;

// Example using Redis adapter
$redisAdapter = new RedisCacheAdapter('127.0.0.1', 6379);
$cacheManager = new CacheManager($redisAdapter);

// Save an item in the cache
$cacheItem = $cacheManager->getItem('my_key');
$cacheItem->set('some_value');
$cacheManager->save($cacheItem);

// Retrieve the item from the cache
$cachedItem = $cacheManager->getItem('my_key');
if ($cachedItem->isHit()) {
    echo $cachedItem->get(); // Outputs 'some_value'
}

// Switch to another adapter dynamically
$memcachedAdapter = new MemcachedCacheAdapter('127.0.0.1', 11211);
$cacheManager->setAdapter($memcachedAdapter);

运行测试

您可以使用PHPUnit运行测试套件

composer test

使用PHPStan进行静态分析

composer phpstan

贡献

请随意提交问题或拉取请求。对于重大更改,请打开问题来讨论您想更改的内容。

许可

本项目采用MIT许可证 - 详细信息请参阅LICENSE文件。