承接 pluggit/cache 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

pluggit/cache

Composer 安装命令:

composer require pluggit/cache

包简介

A library to provide cache interface access to different backend caches

README 文档

README

Build Status Scrutinizer Code Quality Code Coverage

Provides an abstracion over most common user caches

TLDR;

/** @var \Cmp\Cache\Cache $cache */
$cache = (new CacheBuilder)
  ->withLogging($psrLogger)
  ->withoutExceptions()
  ->withArrayCache()
  ->withRedisCacheFromParams($host, $port, $dbNumber)
  ->build();

// Set an item
$cache->set('foo', 'bar');

// Demand an item, throws an exception if not present
$bar = $cache->demand('foo');

// Get an item, if not present in any cache it will return the given default
$default = $cache->get('not found', 'default');

// Delete an item
$cache->delete('foo');

// Empty the cache
$cache->flush();

//Create a tagged cache
$taggedCache = $cache->tag('myTag');

//Set a tagged item
$taggedCache->set('key','value');

//Remove all tagged items
$taggedCache->flush();

The Cache interface

The cache interface allows access to 10 methods:

  • set
  • setAll
  • has
  • get
  • getAll
  • demand
  • delete
  • deleteAll
  • flush
  • tag
  • appendList
  • increase

Set

Use it to store values in the cache. You can make an item expire after a certain time by passing the $timeToLive parameter bigger than zero

Note: Time to live must be an integer representing seconds

SetAll

Use it to store multiple values at one

Has

Checks if an item is in the cache and has not expired

Get

Tries to get an item from the cache, and if it's not there, it return a given default value

GetAll

Tries to get multiple items from the cache, if an item is not found, the key will be returned with a null

Demand

Tries to retrieve an item from the cache, if it's not present, it throws a NotFoundException

Delete

Deletes an item from the cache

Delete All

Deletes multiple items at once

Flush

It empties the cache

Tag

Creates a tagged cache instance. It works as a normal cache, but all the entries are put in a special bucket which you can flush.

Cache backends provided

These are the current backend implementations:

  • Redis
  • Array (in memory)
  • ChainCache

Note: You can use the CacheFactory to easily build a cache object with one of the provided backends

ChainCache

The chain cache will accept one or more cache and tries all caches before failing

Cache backend decorator provided

  • LoggerCache

Logger Cache

It allows to log any exception thrown from the decorated cache. You can choose the silent mode to avoid throwing exceptions from the cache

Note: This decorator is used always when building a cache trough the builder, as it ensures that all exceptions thrown extend the base CacheException

Tags

It is possible to use tags for grouping related objects. All three provided backends have this ability. To use it just request a new tagged cache instance using the tag() method, and then you'll be able to use it in the same way you use any other cache backend:

// Store one photo in the cache
$cache->set('photo.1', $photoOne);

// Store a set of photos and tag them
$cache->tag('photos')->set('photo.1', $taggedPhotoOne);
$cache->tag('photos')->set('photo.2', $taggedPhotoTwo);

// The photos are into different buckets
$cache->has('photo.1');                // true 
$cache->has('photo.2')                 // false
$cache->tag('photos')->has('photo.1'); // true
$cache->tag('photos')->has('photo.2')  // true

// You can flush all items with the same tag at once
$cache->tag('photos')->flush();
$cache->has('photo.1');                // true 
$cache->tag('photos')->has('photo.1'); // false

Pimple service provider

The library includes a service provider for Pimple ^3.0 (included on Silex ^2.0) to easily register a cache

By default it will register an ArrayCache on the key cache

$pimple->register(new CacheServiceProvider());

/** @var LoggerCache $cache */
$cache = $pimple['cache'];

/** @var ArrayCache $cache */
$arrayCache = $pimple['cache']->getDecoratedCache();

Options

  • cache.backends: an array of backends caches to use, if more than one is provided, the ChainCache will be used to wrap them all. Each backend option must have the key backend, the available options are:

    • array: It will create an ArrayCache (same as default)
    • redis: If you already have a redis connection, it can be passed on the key connection (if a string is used, the provider will try to get the service from the container. If the connection is not given, the provider will try to build a connection reading from the options array:
      • host: 127.0.0.1 by default
      • port: 6379 by default
      • db: 0 by default
      • timeout: 0.0 by default
    • string: If a string is given, the provider will try to get the service from the container, the service must implement then Cmp\Cache\Cache interface
    • Cmp\Cache\Cache: If an object implementing the Cache interface is given, it will be used directly
  • cache.exceptions: a boolean, true by default. If is set to true, the cache will be throw exceptions, if false, no exceptions will be thrown.

  • cache.logging: It allows to log exceptions thrown by the cache system. it accepts an array, with the following options:

    • logger: And instance of an logger implementing the Psr\LoggerInterface
    • log_level: The log level to use for logging the exceptions, 'alert' by default. Must be one of the valid levels defined at Psr\LogLevel

Examples:

// Redis from parameters
$pimple->register(new PimpleCacheProvider(), ['cache.backends' => [
    ['backend' => 'redis', 'host' => '127.0.0.1', 'port' => 6379, 'db' => 1, 'timeout' => 2.5]
]]);

// ArrayCache + Redis from existing connection
$pimple->register(new PimpleCacheProvider(), ['cache.backends' => [
    ['backend' => 'array']
    ['backend' => 'redis', 'connection' => $redisConnection,]
]]);

// ArrayCache + Redis from a service registered in the container
$pimple->register(new PimpleCacheProvider(), ['cache.backends' => [
    ['backend' => 'array']
    ['backend' => 'redis', 'connection' => 'redis.connection']
]]);

// Chain caches with logging and muted exceptions
$pimple->register(new PimpleCacheProvider(), [
    'cache.backends'   => [
        ['backend' => 'array'],
        ['backend' => 'custom_cache_service_key'],
    ],
    'cache.exceptions' => false,
    'cache.logging'    => ['logger' => $psrLogger, 'log_level' => PsrLevel::CRITICAL],
]);

pluggit/cache 适用场景与选型建议

pluggit/cache 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 42.33k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2017 年 06 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 pluggit/cache 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 pluggit/cache 我们能提供哪些服务?
定制开发 / 二次开发

基于 pluggit/cache 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

  • Stars: 1
  • Watchers: 19
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-06-15