kuick/cache 问题修复 & 功能扩展

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

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

kuick/cache

最新稳定版本:v1.6.0

Composer 安装命令:

composer require kuick/cache

包简介

Kuick Cache is a slim PSR-16 Simple Cache Interface implementation, supporting backends including Redis, ApcU and FileSystem

README 文档

README

Latest Version PHP Total Downloads GitHub Actions CI codecov Software License

PSR-16 Simple Cache implementation

Supporting popular backends such as:

  • File system
  • Redis
  • Database (Doctrine Dbal)
  • APCu
  • InMemory (aka ArrayCache)
  • Layered

Usage

  1. Direct object creation:
<?php

use Kuick\Cache\FilesystemCache;

$fileCache = new FilesystemCache('/tmp/cache');
$fileCache->set('foo', 'bar');
echo $fileCache->get('foo'); // bar
  1. Cache factory: Factory provides automatic cache object creation by a valid DSN
<?php

use Kuick\Cache\CacheFactory;

$cacheFactory = new CacheFactory();

$dbCache    = $cacheFactory('pdo-mysql://127.0.0.1:3306/mydb'); // DbalCache instance
$apcuCache  = $cacheFactory('apcu://');                         // ApcuCache instance
$fileCache  = $cacheFactory('file:///tmp/cache');               // FilesystemCache instance
$redisCache = $cacheFactory('redis://redis-server.com:6379/2'); // RedisCache instance
  1. Customizing the serializer:
    Saved data can be serialized with one of available serializers: default, json, gzip and gzip-json.
    With larger datasets it can be beneficial to use Gzip serializer, on the other hand Json based serializers are safer to use, as stored objects are casted to simple, JSON objects.
<?php

use Kuick\Cache\CacheFactory;
use Kuick\Cache\FilesystemCache;
use Kuick\Cache\Serializers\GzipJsonSerializer;

$fileCache  = (new CacheFactory())('file:///tmp/cache?serializer=gzip-json');

// equivalent to:

$fileCache  = new FilesystemCache('/tmp/cache', new GzipJsonSerializer());
  1. Method overview
    Kuick Cache implements PSR-16 interface with no exceptions
<?php

use Kuick\Cache\InMemoryCache;

$cache = new InMemoryCache();
$cache->set('foo', 'bar', 300);     // set "foo" to "bar", with 5 minutes ttl
$cache->get('foo');                 // "bar"
$cache->get('inexistent, 'default') // "default" (using the default value as the key does not exist)
$cache->has('foo');                 // true
$cache->delete('foo');              // remove "foo"

// set "foo" to "bar", and "bar" to "baz"
$cache->setMultiple([
    'foo' => 'bar',
    'bar' => 'baz',
]);

// ['foo' => 'bar', 'bar' => 'baz']
$cache->getMultiple([
    'foo',
    'bar',
]);

// removes "foo" and "bar"
$cache->deleteMultiple([
    'foo',
    'bar',
]);

// removes all the keys
$cache->clear();
  1. Layered cache conception
    If you are using cost intensive backend like Dbal, it can be beneficial to store cache data on multiple layers. Get methods will try the fastest backend possible.
    PLEASE CONSIDER!
    If not all backends are distributed, data inconsistency may occur, in example: We have 2 PHP containers serving content. We have 2 layers - one APCu, another Dbal. If one container changes Dbal data the other one may serve stale cache.
<?php

use Kuick\Cache\CacheFactory;
use Kuick\Cache\InMemoryCache;
use Kuick\Cache\LayeredCache;

$layeredCache = new LayeredCache([
    new InMemoryCache(),                                // the fastest backend
    new FilesystemCache('/tmp/cache'),                  // medium backend
    (new CacheFactory())('pdo-mysql://remote:3306/db')  // slowest backend
]);

$layeredCache->get('foo');

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-12-27