voku/simple-cache
Composer 安装命令:
composer require voku/simple-cache
包简介
Simple Cache library
关键字:
README 文档
README
⚡ Simple Cache Class
This is a simple Cache Abstraction Layer for PHP >= 8.0 that provides a simple interaction with your cache-server. You can define the Adapter / Serializer in the "constructor" or the class will auto-detect you server-cache in this order:
- Memcached / Memcache
- Redis
- Xcache
- APC / APCu
- OpCache (via PHP-files)
- Static-PHP-Cache
Get "Simple Cache"
You can download it from here, or require it using composer.
{
"require": {
"voku/simple-cache": "5.*"
}
}
Install via "composer require"
composer require voku/simple-cache
Quick Start
use voku\cache\Cache; require_once 'composer/autoload.php'; $cache = new Cache(); $ttl = 3600; // 60s * 60 = 1h $cache->setItem('foo', 'bar', $ttl); $bar = $cache->getItem('foo');
Usage
use voku\cache\Cache; $cache = new Cache(); if ($cache->getCacheIsReady() === true && $cache->existsItem('foo')) { return $cache->getItem('foo'); } else { $bar = someSpecialFunctionsWithAReturnValue(); $cache->setItem('foo', $bar); return $bar; }
If you have an heavy task e.g. a really-big-loop, then you can also use static-cache. But keep in mind, that this will be stored into PHP (it needs more memory).
use voku\cache\Cache; $cache = new Cache(); if ($cache->getCacheIsReady() === true && $cache->existsItem('foo')) { for ($i = 0; $i <= 100000; $i++) { echo $this->cache->getItem('foo', 3); // use also static-php-cache, when we hit the cache 3-times } return $cache->getItem('foo'); } else { $bar = someSpecialFunctionsWithAReturnValue(); $cache->setItem('foo', $bar); return $bar; }
PS: By default, the static cache is also used by >= 10 cache hits. But you can configure this behavior via $cache->setStaticCacheHitCounter(INT).
No-Cache for the admin or a specific ip-address
If you use the parameter "$checkForUser" (=== true) in the constructor, then the cache isn't used for the admin-session.
-> You can also overwrite the check for the user, if you add a global function named "checkForDev()".
Overwrite the auto-connection option
You can overwrite the cache auto-detect via "CacheAdapterAutoManager" and the "$cacheAdapterManagerForAutoConnect" option in the "Cache"-constructor. Additional you can also activate the "$cacheAdapterManagerForAutoConnectOverwrite" option in the "Cache"-constructor, so that you can implement your own cache auto-detect logic.
$cacheManager = new \voku\cache\CacheAdapterAutoManager(); // 1. check for "APCu" support first $cacheManager->addAdapter( \voku\cache\AdapterApcu::class ); // 2. check for "APC" support $cacheManager->addAdapter( \voku\cache\AdapterApcu::class ); // 3. try "OpCache"-Cache $cacheManager->addAdapter( \voku\cache\AdapterOpCache::class, static function () { $cacheDir = \realpath(\sys_get_temp_dir()) . '/simple_php_cache_opcache'; return $cacheDir; } ); // 4. try "File"-Cache $cacheManager->addAdapter( \voku\cache\AdapterFileSimple::class, static function () { $cacheDir = \realpath(\sys_get_temp_dir()) . '/simple_php_cache_file'; return $cacheDir; } ); // 5. use Memory Cache as final fallback $cacheManager->addAdapter( \voku\cache\AdapterArray::class ); $cache = new \voku\cache\CachePsr16( null, // use auto-detection null, // use auto-detection false, // do not check for usage true, // enable the cache false, // do not check for admin session false, // do not check for dev false, // do not check for admin session false, // do not check for server vs. client ip '', // do not use "_GET"-parameter for disabling $cacheManager, // new auto-detection logic true // overwrite the auto-detection logic );
Support
For support and donations please visit Github | Issues | PayPal | Patreon.
For status updates and release announcements please visit Releases | Twitter | Patreon.
For professional support please contact me.
Thanks
- Thanks to GitHub (Microsoft) for hosting the code and a good infrastructure including Issues-Managment, etc.
- Thanks to IntelliJ as they make the best IDEs for PHP and they gave me an open source license for PhpStorm!
- Thanks to GitHub Actions for powering the project's continuous integration pipeline.
- Thanks to StyleCI for the simple but powerfull code style check.
- Thanks to PHPStan && Psalm for relly great Static analysis tools and for discover bugs in the code!
License
voku/simple-cache 适用场景与选型建议
voku/simple-cache 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.65M 次下载、GitHub Stars 达 32, 最近一次更新时间为 2015 年 01 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「cache」 「caching」 「simple cache」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 voku/simple-cache 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 voku/simple-cache 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 voku/simple-cache 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
Query caching for Laravel 5
Laravel 5 - Repositories to the database layer
Simple PHP caching system that uses a tmp folder in a Linux environment.
Provides support for PSR-6 compatible cache for Yii1 application
Rinvex Repository is a simple, intuitive, and smart implementation of Active Repository with extremely flexible & granular caching system for Laravel, used to abstract the data layer, making applications more flexible to maintain.
统计信息
- 总下载量: 2.65M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 33
- 点击次数: 20
- 依赖项目数: 6
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2015-01-26