silmaralberti / php-cache
1.1.2
2023-02-20 19:42 UTC
Requires
- php: ^7.2|^8.0
- ext-redis: *
Requires (Dev)
- phpunit/phpunit: ^9.6
Suggests
- ext-igbinary: low memory and cache space utilization
README
可扩展和灵活的PHP缓存库。
安装到项目中
composer require silmaralberti/php-cache
文档
使用示例
// set redis adapter connection $redisConnectionParams = [ 'host' => 'host.docker.internal' ]; $serializer = new IgBinaryLib(); $redisAdapter = new RedisAdapter($redisConnectionParams); $hash = new HashKeyLib(); $testSettings = new PhpCacheSettingsModel( $redisAdapter, $serializer, $hash ); $phpCache = new PhpCache($testSettings); $queryContent = [ 'keyA' => 'keyContentA', 'keyB' => 'keyContentB' ]; $valueContent = [ 'valueA' => 'contentA', 'valueB' => 'contentB' ]; $valueCount = $phpCache->increase($queryContent, 1); // $valueCount 1 if first call $successOnSave = $phpCache->set($queryContent, $valueContent); // $successOnSave true if success else false $cachedValueContent = $phpCache->get($queryContent); // false if not found in cache // $cachedValueContent is equal $valueContent
序列化类
IgBinaryLib
默认序列化库
适配器类
Redis适配器
键生成库
哈希键库
PhpCache方法
increase()
增加并返回键值
get()
加载缓存数据
set()
存储缓存数据
cacheFunction()
从缓存获取函数的响应或调用函数并将结果存储在缓存中
示例
$serializer = new IgBinaryLib(); $redisAdapter = new RedisAdapter($redisConnectionParams); $hash = new HashKeyLib(); $testSettings = new PhpCacheSettingsModel( $redisAdapter, $serializer, $hash ); $phpCache = new PhpCache($testSettings); $testValue = 'storedOnCacheValue'; $result = $phpCache->cacheFunction( function ($testValue) { return $testValue; }, [$testValue], 'functionExample' ); echo $result; // print 'storedOnCacheValue';