承接 mcustiel/php-simple-cache 相关项目开发

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

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

mcustiel/php-simple-cache

Composer 安装命令:

composer require mcustiel/php-simple-cache

包简介

A simple library to abstract different cache servers.

README 文档

README

What is it

php-simple-cache is a PHP Cache library with the minimum functionalities needed that abstracts different cache mechanisms. It is thought to be performant, flexible and easy to use without the need of a containing framework.

Currently the cache systems abstracted by php-simple-cache are:

  • Serialized files ("file" driver): Uses files saved in a directory containing the serialized data.
  • Memcache ("memcache" driver): Uses php's memcache extension through \Memcache class to access a memcached server.
  • Redis ("phpredis" driver): Uses php's phpredis extension through \Redis class
  • APCu ("apcu" driver): Uses apcu pecl extension for fast local cache.

This library is thought to be used with or without a dependency injection system, that's why the constructor of each driver allows the injection of it's dependencies. If you don't use a dependency injection system, maybe you just want to use a provided factory class that instantiates each driver by it's name, but you need to call init method to configure the driver after it's instantiated.

Installation

Download code

This library supports PSR-4 so you can just download the code and map your autoloader to use it.

Composer

php-simple-config also supports composer, just add the packagist dependency:

{
    "require": {
    	// ...
        "mcustiel/php-simple-cache": ">=1.3.1"
    }
}

How to use it

Driver instantiation

There's a variety of ways to instantiate the drivers, as an example memcache driver is used:

Simple instantiation

$cacheManager = new \Mcustiel\SimpleCache\Drivers\memcache\Cache();
$config = new \stdClass();
$config->host = 'yourmemcached.host.com';
$config->port = 11211;
$config->timeout = 1;
$cacheManager->init($config);

Or with your own Memcache object (this technique could also be used with a dependency injection system.

$connection = new \Memcache();
$connection->connect();
$cacheManager = new \Mcustiel\SimpleCache\Drivers\memcache\Cache($connection);
// No init() call required here.

Using provided factory class

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);

Caching and retrieving data

Using php-simple-cache for caching data and retrieve it after is really simple, the idea is (as in most cache systems) to check if data is cached, if not generate the data and cache it.

use \Mcustiel\SimpleCache\Types\Key;

$key = new Key('cached-data-key');
$data = $cacheManager->get($key);
if ($data === null) {
	$data = someHeavyProcessThatGeneratesDataThatCanBeCached();
	$cacheManager->set($key, $data);
}

Removing cached data

When there is cached data that isn't needed to be cached anymore, just use the delete method:

use \Mcustiel\SimpleCache\Types\Key;

$key = new Key('cached-data-key');
$cacheManager->delete($key);

Driver-specific configurations

Each driver has a unique configuration specification, the best way to avoid this is to inject the dependencies into each driver but, if you need to use it the init() method, these are the config parameters for each driver:

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';

File

$fileService = new Mcustiel\SimpleCache\Drivers\file\Utils\FileService('/path/to/cache/files')

Considerations

Please have in mind that each driver is different in it's implementation. There's no much difference between phpredis and memcache drivers, but file driver does not auto expire cache. For the file case I added support for timeout but that makes it a slow cache, so for the future I have as a TODO, to create a "garbage collector" script that processes expired files and remove the logic to delete expired files in get method. Use file driver just in very rare cases in which you don't have access to memcache or redis or for development environment.

mcustiel/php-simple-cache 适用场景与选型建议

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: GPL-3.0
  • 更新时间: 2014-12-15