定制 simplecomplex/cache 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

simplecomplex/cache

Composer 安装命令:

composer require simplecomplex/cache

包简介

File-based PSR-16 Simple Cache implementation; cache management; CLI interface.

README 文档

README

Scope

Caching of complex variables, and variables which are expensive to generate.
Like configuration, localization and service responses.

Not page caching, no stampede protection.

Cache abstraction

CacheBroker decouples code using cache from the actual PSR-16 cache implementation.

Defines three cache class aliases:

  • variable time-to-live (default ttl and set() arg ttl)
  • fixed time-to-live (default ttl, set() arg ttl ignored)
  • persistent (default ttl 'forever' and set() arg ttl ignored)

Plus three like the above which allow long keys; length 128 instead of the PSR-16 compliant 64.

How to use

Ask CacheBroker for an aliased type of cache instance - do not instantiate a particular cache class.

Extend CacheBroker, if you later want to switch from say file-based to database-based caching.

Dependency injection container ID: cache-broker

Recommendation: access (and thus instantiate) the cache broker via DI container ID 'cache-broker'.
See SimpleComplex Utils Dependency.

File-based caching

FileCache is a thorough and cautious PSR-16 Simple Cache implementation; file-based.
Coded defensively - key (and other argument) validation.

Addresses:

  • default time-to-live; ignore set() arg ttl option; no time-to-live
  • garbage collection
  • clearing all items or expired items only
  • exporting all items, to JSON
  • building/replacing a cache store during production (using a 'candidate' store)
  • CLI interface for clearing items, e.g. via cron
  • concurrency issues (storage-wise only)

Cache management, replacement and backup

Defines two extensions to the PSR-16 CacheInterface, implemented by FileCache.

ManageableCacheInterface

  • is the cache store new or empty?
  • setting default time-to-live; setting 'ignore' set() arg ttl
  • clearing and exporting
  • listing all cache stores

BackupCacheInterface

  • backup/restore
  • replacing a store, by building a 'candidate' and switching to that when it's complete

Example

// Bootstrap.
Dependency::genericSet('cache-broker', function () {
    return new \SimpleComplex\Cache\CacheBroker();
});
// ...
// Use.
/** @var \Psr\Container\ContainerInterface $container */
$container = Dependency::container();
/** @var \SimpleComplex\Cache\CacheBroker $cache_broker */
$cache_broker = $container->get('cache-broker');
/**
 * Create or re-initialize a cache store.
 *
 * @var \SimpleComplex\Cache\FileCache $cache_store
 */
$cache_store = $cache_broker->getStore(
    'some-cache-store',
    CacheBroker::CACHE_VARIABLE_TTL
);
unset($cache_broker);
/** @var mixed $whatever */
$whatever = $cache_store->get('some-key', 'the default value');

CLI commands

# List all cache commands and their help.
php cli.php cache -h
# One command's help.
php cli.php cache-xxx -h

# List existing cache stores.
php cli.php cache-list-stores

# Display/get value of a cache item.
php cli.php cache-get store key

# Delete a cache item.
php cli.php cache-delete store key

# Delete all expired items of one or all cache stores.
php cli.php cache-clear-expired

# Delete all items of one or all cache stores.
php cli.php cache-clear

# Backup a cache store.
php cli.php cache-backup store

# Restore a cache store from backup.
php cli.php cache-restore store

# Destroy one or all cache stores.
php cli.php cache-destroy

Installation

Create a 'private' files directory alongside the document root dir
and make it writable for the webserver user (www-data or apache).

Like:
/var/www/my-host/http
/var/www/my-host/private

On first cache store instantiation, FileCache will create directory
private/lib/simplecomplex/file-cache

If that directory structure isn't suitable, do either:

  • supply CacheBroker (or FileCache constructor directly) with a 'path' argument
  • extend FileCache and override it's class constant PATH_DEFAULT

Requirements

Suggestions

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

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

它主要适用于以下技术方向: 「php」 「simple cache」 「psr-16」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-07-12