mcustiel / php-simple-cache
一个抽象不同缓存服务器的简单库。
Requires
- php: >=5.5
Requires (Dev)
- pdepend/pdepend: >=2.0.6
- phing/phing: >=2.12.0
- phpdocumentor/phpdocumentor: >=2.8.5
- phploc/phploc: >=2.1.1
- phpmd/phpmd: >=2.3.2
- phpunit/phpunit: >=4.8.9,<5.0
- sebastian/phpcpd: >=2.0.2
- squizlabs/php_codesniffer: >=2.3.4
Suggests
- ext-apcu: *
- ext-memcache: *
- ext-redis: *
This package is auto-updated.
Last update: 2024-09-19 09:25:34 UTC
README
这是什么
php-simple-cache是一个具有最小功能的PHP缓存库,它抽象了不同的缓存机制。它被认为是性能高、灵活且易于使用,无需包含框架。
目前由php-simple-cache抽象的缓存系统包括
- 序列化文件("file"驱动器):使用目录中保存的序列化数据。
- Memcache("memcache"驱动器):通过\Memcache类使用php的memcache扩展来访问memcached服务器。
- Redis("phpredis"驱动器):通过\Redis类使用php的phpredis扩展
- APCu("apcu"驱动器):使用apcu pecl扩展进行快速本地缓存。
这个库旨在与或没有依赖注入系统一起使用,这就是为什么每个驱动器的构造函数都允许注入其依赖项。如果您不使用依赖注入系统,也许您只想使用提供的工厂类,该类通过名称实例化每个驱动器,但您需要在实例化后调用init方法来配置驱动器。
安装
下载代码
此库支持PSR-4,因此您只需下载代码并将自动加载器映射到使用它。
Composer
php-simple-config也支持composer,只需添加packagist依赖项
{ "require": { // ... "mcustiel/php-simple-cache": ">=1.3.1" } }
如何使用它
驱动程序实例化
有各种实例化驱动程序的方法,以下是一个示例,使用的是memcache驱动程序
简单实例化
$cacheManager = new \Mcustiel\SimpleCache\Drivers\memcache\Cache(); $config = new \stdClass(); $config->host = 'yourmemcached.host.com'; $config->port = 11211; $config->timeout = 1; $cacheManager->init($config);
或使用您自己的Memcache对象(这种技术也可以与依赖注入系统一起使用。
$connection = new \Memcache(); $connection->connect(); $cacheManager = new \Mcustiel\SimpleCache\Drivers\memcache\Cache($connection); // No init() call required here.
使用提供的工厂类
use \Mcustiel\SimpleCache\SimpleCache; $factory = new SimpleCache(); $cacheManager = $factory->getCacheManager('memcache'); $config = new \stdClass(); $config->host = 'yourmemcached.host.com'; $config->port = 11211; $config->timeout = 1; $cacheManager->init($config);
缓存和检索数据
使用php-simple-cache缓存数据并在之后检索它非常简单,其思想(如大多数缓存系统一样)是检查数据是否已缓存,如果没有,则生成数据并将其缓存。
use \Mcustiel\SimpleCache\Types\Key; $key = new Key('cached-data-key'); $data = $cacheManager->get($key); if ($data === null) { $data = someHeavyProcessThatGeneratesDataThatCanBeCached(); $cacheManager->set($key, $data); }
删除缓存数据
当有不再需要缓存的数据时,只需使用delete方法即可。
use \Mcustiel\SimpleCache\Types\Key; $key = new Key('cached-data-key'); $cacheManager->delete($key);
驱动程序特定配置
每个驱动器都有独特的配置规范,最好的避免方法是向每个驱动器注入依赖项,但如果您需要使用init()方法,以下是每个驱动器的配置参数
Memcache
$config = new \stdClass() # Memcache server address, default is localhost $config->host = 'yourmemcached.host.com'; # Memcache server port, default is php.ini:memcache.default_port $config->port = 11211; # Connection timeout in seconds, default is 1 $config->timeoutInSeconds = 1; # Whether or not to use a persistent connection, default is false $config->persistent = false;
Phpredis
$config = new \stdClass() # Redis server address, default is localhost $config->host = 'yourredis.host.com'; # Redis server port, default is null $config->port = 6379; # Connection timeout in seconds, default is null $config->timeoutInSeconds = 1; # Redis server auth password, default is null $config->password = 'yourRedisPasswd'; # Database number, default is 0 $config->database = 2; # Persistent connection id. If not specified, then persistent connection is not used. $config->persistedId = 'yourPersistentConnectionId';
文件
$fileService = new Mcustiel\SimpleCache\Drivers\file\Utils\FileService('/path/to/cache/files')
考虑事项
请注意,每个驱动器的实现方式都不同。phpredis和memcache驱动器之间没有太大的区别,但文件驱动器不自动过期缓存。对于文件情况,我添加了对超时的支持,但这使其成为一个慢速缓存,因此对于未来,我将其作为一个待办事项,创建一个“垃圾回收器”脚本来处理过期的文件,并在get方法中删除删除过期文件的逻辑。仅在非常罕见的情况下使用文件驱动器,例如在没有访问memcache或redis或开发环境的情况下。