phpfastcache / phpfastcache
PHP 抽象缓存类 - 使用缓存系统减少数据库调用。Phpfastcache 支持大量驱动程序,如 Apc(u)、Cassandra、CouchBase、Couchdb、Dynamodb、Firestore、Mongodb、Files、(P)redis、Leveldb、Memcache(d)、Ravendb、Ssdb、Sqlite、Wincache、Xcache、Zend Data Cache。
Requires
- php: >=8.0
- ext-json: *
- ext-mbstring: *
- psr/cache: ^2.0||^3.0
- psr/simple-cache: ^2.0||^3.0
Requires (Dev)
- ext-gettext: *
- jetbrains/phpstorm-stubs: dev-master
- league/climate: ^3.8
- phpfastcache/phpfastcache-devtools: dev-master
- phpmd/phpmd: @stable
- phpstan/phpstan: @stable
- squizlabs/php_codesniffer: @stable
Suggests
- ext-apcu: *
- ext-cassandra: *
- ext-couchbase: ^3.0
- ext-curl: *
- ext-intl: *
- ext-leveldb: *
- ext-memcache: *
- ext-memcached: *
- ext-redis: *
- ext-sqlite: *
- ext-wincache: *
- phpfastcache/arangodb-extension: ^9.2
- phpfastcache/couchbasev4-extension: ^9.2
- phpfastcache/couchdb-extension: ^9.2
- phpfastcache/dynamodb-extension: ^9.2
- phpfastcache/firestore-extension: ^9.2
- phpfastcache/mongodb-extension: ^9.2
- phpfastcache/phpssdb: ~1.1.0
- phpfastcache/ravendb-extension: ^9.2
- phpfastcache/solr-extension: ^9.2
- predis/predis: ^2.0
Provides
- psr/cache-implementation: 2.0|3.0
- psr/simple-cache-implementation: 2.0|3.0
- dev-master
- v10.x-dev
- v9.x-dev
- v9.2.x-dev
- 9.2.3
- 9.2.2
- 9.2.1
- 9.2.0
- 9.2.0-RC3
- 9.2.0-rc2
- 9.2.0-rc
- 9.2.0-beta3
- 9.2.0-beta2
- 9.2.0-beta
- 9.1.3
- 9.1.2
- 9.1.1
- 9.1.0
- 9.0.2
- 9.0.1
- 9.0.0
- 9.0.0-rc
- 9.0.0-beta5
- 9.0.0-beta4
- 9.0.0-beta3
- 9.0.0-beta2
- 9.0.0-beta
- 9.0.0-alpha3
- 9.0.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- 4.3
- 4.2.4
- 4.2.3
- 4.2.2
- 4.2.1
- 4.1.1
- 4.1
- 4.0.18
- 4.0.17
- 4.0.16
- 4.0.15
- 4.0.14
- 4.0.13
- 4.0.12
- 4.0.11
- 4.0.10
- 4.0.9
- 4.0.8
- 4.0.7
- 4.0.6
- 4.0.5
- 4.0.4
- 4.0.3
- 4.0.3-beta1
- 4.0.2
- 4.0.2beta1
- 4.0.1
- 4.0
- 3.1.1
- 3.1.0
- 3.0.28
- 3.0.27
- 3.0.26
- 3.0.25
- 3.0.24
- 3.0.23
- 3.0.22
- 3.0.21
- 3.0.20
- 3.0.19
- 3.0.18
- 3.0.17
- 3.0.16
- 3.0.15
- 3.0.14
- 3.0.13
- 3.0.12
- 3.0.11
- 3.0.10
- 3.0.9
- 3.0.8
- 3.0.7
- 3.0.6
- 3.0.5
- 3.0.4
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0
- 2.4.3
- 2.4.2
- dev-fix/copyright-header-typo
- dev-git-issue-config-patch-1
This package is auto-updated.
Last update: 2024-09-01 23:02:21 UTC
README
⚠️ 注意:V9 主要是一个针对 Phpfastcache 的 PHP 8 类型感知更新,其中包含一些重大变化!
由于 V9 与之前版本不兼容,请仔细阅读迁移指南,以确保平滑迁移。最大的变化之一是配置系统,现在是一个对象,取代了我们当时使用的原始数组。另外,请注意,V9 至少需要 PHP 8 或更高版本才能正常工作。
简单而强大的 PHP 缓存类
更多信息请参考 Wiki 抽象的简洁性:一个类可以用于多个后端缓存。您无需再次多次重写代码。
当前支持的驱动程序 *
💡 欢迎通过提交新的 Pull Request 提出驱动程序,我们非常欢迎!
* 驱动程序描述可在 DOCS/DRIVERS.md 中找到
🆕 截至 v9.2,已发布一个新的 Couchbase 扩展:Couchbasev4。同时,还添加了一个新的驱动程序扩展:Ravendb
。该驱动程序将在功能上进行 积极开发,以允许更好的细粒度配置。这个新扩展是 Phpfastcache 以及其他一些扩展的 新时代的开始。
许多驱动程序已从核心移动到各自的子存储库作为独立扩展:Arangodb
、Couchdb
、Dynamodb
、Firestore
、Mongodb
、Solr
。
它们可以通过 composer 简单地添加,例如:composer install phpfastcache/couchbasev4-extension
。但是,出于兼容性原因,Couchbasev3
将继续保留在核心中,但将被弃用。
因为缓存并不意味着削弱你的代码
Phpfastcache 多年来一直致力于 3 个主要目标
- 性能:我们优化并仍在优化代码,以提供尽可能轻的库
- 安全:因为缓存策略有时会带来不希望出现的漏洞,我们尽力为您提供尽可能安全且强大的库
- 便携性:无论您在什么操作系统上工作,我们都尽力为您提供尽可能跨平台的代码
丰富的开发 API
Phpfastcache 为您提供了大量有用的 API
项目 API (ExtendedCacheItemInterface)
项目池 API (ExtendedCacheItemPoolInterface)
🆕 在 V8 中:现在支持多种策略 ($strategy
) 进行标记
TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE
允许您通过至少一个指定的匹配标签获取缓存项。 默认行为。TaggableCacheItemPoolInterface::TAG_STRATEGY_ALL
允许您通过所有指定的匹配标签获取缓存项(缓存项可以包含额外的标签)TaggableCacheItemPoolInterface::TAG_STRATEGY_ONLY
允许您仅通过指定的匹配标签获取缓存项(缓存项不能包含额外的标签)
它还支持多次调用、标记和设置缓存文件夹。请查看我们的示例文件夹以获取更多信息。
Phpfastcache 版本号 API
Phpfastcache 提供了一个类,可以为您提供有关您的 Phpfastcache 安装的详细信息
- 使用
Phpfastcache\Api::GetVersion();
获取 API 版本(项目+池接口) - 使用
Phpfastcache\Api::getChangelog();
获取 API 变更日志(项目+池接口) - 使用
Phpfastcache\Api::getPhpfastcacheVersion();
获取 Phpfastcache 版本 - 使用
Phpfastcache\Api::getPhpfastcacheChangelog();
获取 Phpfastcache 变更日志
想要让它更简单吗?
😅 好消息,从 V6 开始,提供了一个 Psr16 适配器,使用非常基本的获取器/设置器使缓存保持最简单。
get($key, $default = null);
set($key, $value, $ttl = null);
delete($key);
clear();
getMultiple($keys, $default = null);
setMultiple($values, $ttl = null);
deleteMultiple($keys);
has($key);
基本用法
<?php use Phpfastcache\Helper\Psr16Adapter; $defaultDriver = 'Files'; $Psr16Adapter = new Psr16Adapter($defaultDriver); if(!$Psr16Adapter->has('test-key')){ // Setter action $data = 'lorem ipsum'; $Psr16Adapter->set('test-key', 'lorem ipsum', 300);// 5 minutes }else{ // Getter action $data = $Psr16Adapter->get('test-key'); } /** * Do your stuff with $data */
内部,Psr16 适配器通过缓存管理器调用 Phpfastcache Api。
介绍事件
📣 从 V6 开始,Phpfastcache 提供了一个事件机制。您可以通过向活动事件传递闭包来订阅事件
<?php use Phpfastcache\EventManager; /** * Bind the event callback */ EventManager::getInstance()->onCacheGetItem(function(ExtendedCacheItemPoolInterface $itemPool, ExtendedCacheItemInterface $item){ $item->set('[HACKED BY EVENT] ' . $item->get()); });
事件回调可以解绑,但您必须先提供一个名称给回调
<?php use Phpfastcache\EventManager; /** * Bind the event callback */ EventManager::getInstance()->onCacheGetItem(function(ExtendedCacheItemPoolInterface $itemPool, ExtendedCacheItemInterface $item){ $item->set('[HACKED BY EVENT] ' . $item->get()); }, 'myCallbackName'); /** * Unbind the event callback */ EventManager::getInstance()->unbindEventCallback('onCacheGetItem', 'myCallbackName');
🆕 从 V8 开始,您可以简单地订阅 Phpfastcache 的所有事件。
有关实现和事件的更多信息,请参阅 Wiki
介绍新助手
📚 从 V6 开始,Phpfastcache 提供了一些助手,使您的代码更易于编写。
- (:warning: 在 v8 中已删除,为什么?)
用于帮助您同时作用于多个实例的 ActOnAll 助手。 - 用于帮助您使基本的条件语句更容易的 CacheConditional 助手。
- 用于适配 Psr16 的 Psr16 适配器
未来可能会有更多内容,欢迎贡献力量!
介绍聚合集群支持
请查看 WIKI 了解如何实现聚合缓存集群功能。
像开啤酒一样快
👍 第 1 步:使用 composer 将 phpFastCache 包含到您的项目中
composer require phpfastcache/phpfastcache
🚧 第 2 步:设置您的网站代码以实现 phpFastCache 调用(使用 Composer)
<?php use Phpfastcache\CacheManager; use Phpfastcache\Config\ConfigurationOption; // Setup File Path on your config files // Please note that as of the V6.1 the "path" config // can also be used for Unix sockets (Redis, Memcache, etc) CacheManager::setDefaultConfig(new ConfigurationOption([ 'path' => '/var/www/phpfastcache.com/dev/tmp', // or in windows "C:/tmp/" ])); // In your class, function, you can call the Cache $InstanceCache = CacheManager::getInstance('files'); /** * Try to get $products from Caching First * product_page is "identity keyword"; */ $key = "product_page"; $CachedString = $InstanceCache->getItem($key); $your_product_data = [ 'First product', 'Second product', 'Third product' /* ... */ ]; if (!$CachedString->isHit()) { $CachedString->set($your_product_data)->expiresAfter(5);//in seconds, also accepts Datetime $InstanceCache->save($CachedString); // Save the cache item just like you do with doctrine and entities echo 'FIRST LOAD // WROTE OBJECT TO CACHE // RELOAD THE PAGE AND SEE // '; echo $CachedString->get(); } else { echo 'READ FROM CACHE // '; echo $CachedString->get()[0];// Will print 'First product' } /** * use your products here or return them; */ echo implode('<br />', $CachedString->get());// Will echo your product list
⚡ 第 3 步:享受!您的网站现在比闪电还快!
对于好奇的开发者,还有很多其他示例可供参考 这里。
💥 Phpfastcache 支持
发现了一个问题或有想法吗?请 在这里 告诉我们!