comporu/cache
Composer 安装命令:
composer require comporu/cache
包简介
Provides an cache interface for several adapters Apc, Apcu, File, Mongo, Memcache, Memcached, Mysql, Mongo, Redis is supported. New adapters is comming!
README 文档
README
A simple cache library. Implements different adapters that you can use and change easily by a manager or similar.
Installation
With Composer
It is best installed it through packagist
by including desarrolla2/cache in your project composer.json require:
"require": { // ... "desarrolla2/cache": "~2.0" }
Without Composer
You can also download it from [Github] (https://github.com/desarrolla2/Cache), but no autoloader is provided so you'll need to register it with your own PSR-0 compatible autoloader.
Usage
<?php use Desarrolla2\Cache\Cache; use Desarrolla2\Cache\Adapter\NotCache; $cache = new Cache(new NotCache()); $cache->set('key', 'myKeyValue', 3600); // later ... echo $cache->get('key');
Adapters
Apcu
Use it if you will you have APC cache available in your system.
<?php use Desarrolla2\Cache\Cache; use Desarrolla2\Cache\Adapter\Apcu; $adapter = new Apcu(); $adapter->setOption('ttl', 3600); $cache = new Cache($adapter);
File
Use it if you will you have dont have other cache system available in your system or if you like to do your code more portable.
<?php use Desarrolla2\Cache\Cache; use Desarrolla2\Cache\Adapter\File; $cacheDir = '/tmp'; $adapter = new File($cacheDir); $adapter->setOption('ttl', 3600); $cache = new Cache($adapter);
Memcache
Use it if you will you have mencache available in your system.
<?php use Desarrolla2\Cache\Cache; use Desarrolla2\Cache\Adapter\Memcache; $adapter = new Memcache(); $cache = new Cache($adapter);
You can config your connection before
<?php use Desarrolla2\Cache\Cache; use Desarrolla2\Cache\Adapter\Memcache; use \Memcache as Backend $backend = new Backend(); // configure it here $cache = new Cache(new Memcache($backend));
Memcached
Is the same like mencache adapter.
Memory
This is the fastest cache type, since the elements are stored in memory. Cache Memory such is very volatile and is removed when the process terminates. Also it is not shared between different processes.
Memory cache have a option "limit", that limit the max items in cache.
<?php use Desarrolla2\Cache\Cache; use Desarrolla2\Cache\Adapter\Memory; $adapter = new Memory(); $adapter->setOption('ttl', 3600); $adapter->setOption('limit', 200); $cache = new Cache($adapter);
Mongo
Use it to store the cache in a Mongo database. Requires the (legacy) mongo extension or the mongodb/mongodb library.
You may pass either a database or collection object to the constructor. If a
database object is passed, the items collection within that DB is used.
<?php use Desarrolla2\Cache\Cache; use Desarrolla2\Cache\Adapter\Mongo; $client = new MongoClient($dsn); $database = $client->selectDatabase($dbname); $adapter = new Mongo($database); $adapter->setOption('ttl', 3600); $cache = new Cache($adapter);
<?php use Desarrolla2\Cache\Cache; use Desarrolla2\Cache\Adapter\Mongo; $client = new MongoClient($dsn); $database = $client->selectDatabase($dbName); $collection = $database->selectCollection($collectionName); $adapter = new Mongo($collection); $adapter->setOption('ttl', 3600); $cache = new Cache($adapter);
Note that expired cache items aren't automatically deleted. To keep your database clean, you should create a ttl index.
db.items.createIndex( { "ttl": 1 }, { expireAfterSeconds: 30 } )
Mysqli
Use it if you will you have mysqlnd available in your system.
<?php use Desarrolla2\Cache\Cache; use Desarrolla2\Cache\Adapter\Mysqli; $adapter = new Mysqli(); $adapter->setOption('ttl', 3600); $cache = new Cache($adapter);
<?php use Desarrolla2\Cache\Cache; use Desarrolla2\Cache\Adapter\Mysqli; use \mysqli as Backend $backend = new Backend(); // configure it here $cache = new Cache(new Mysqli($backend));
NotCache
Use it if you will not implement any cache adapter is an adapter that will serve to fool the test environments.
Predis
Use it if you will you have redis available in your system.
You need to add predis as dependency in your composer file.
"require": { //... "predis/predis": "~1.0.0" }
other version will have compatibility issues.
<?php use Desarrolla2\Cache\Cache; use Desarrolla2\Cache\Adapter\Predis; $adapter = new Predis(); $cache = new Cache($adapter);
If you need to configure your predis client, you will instantiate it and pass it to constructor.
<?php use Desarrolla2\Cache\Cache; use Desarrolla2\Cache\Adapter\Predis; use Predis\Client as Backend $adapter = new Predis(new Backend($options)); $cache = new Cache($adapter);
Coming soon
This library implements other adapters as soon as possible, feel free to send new adapters if you think it appropriate.
This can be a list of pending tasks.
- Cleaning cache
- MemcachedAdapter
- Other Adapters
Contact
You can contact with me on @desarrolla2.
comporu/cache 适用场景与选型建议
comporu/cache 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 141 次下载、GitHub Stars 达 0, 最近一次更新时间为 2016 年 02 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「redis」 「file」 「cache」 「mysql」 「apc」 「mongo」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 comporu/cache 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 comporu/cache 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 comporu/cache 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
Microservice RPC through message queues.
The file manager intended for using Laravel with CKEditor / TinyMCE / Colorbox
The CodeIgniter Redis package
贝嘟分布式缓存扩展
Laravel 5 - Repositories to the database layer
统计信息
- 总下载量: 141
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-02-03