hammermaps-dev / phpfastcache
PHP 抽象缓存类 - 使用缓存系统减少数据库调用。PhpFastCache 处理了许多驱动程序,例如 Apc(u)、Cassandra、CouchBase、Couchdb、Mongodb、文件、(P)redis、Leveldb、Memcache(d)、Ssdb、Sqlite、Wincache、Xcache、Zend Data Cache。
Requires
- php: >=7.0
- ext-json: *
- ext-mbstring: *
- psr/cache: ~1.0.0
- psr/simple-cache: ~1.0.0
Suggests
- ext-apc: *
- ext-couchbase: *
- ext-intl: *
- ext-leveldb: *
- ext-memcache: *
- ext-memcached: *
- ext-redis: *
- ext-sqlite: *
- ext-wincache: *
- ext-xcache: *
- mongodb/mongodb: ^1.1
- phpfastcache/couchdb: ~1.0.0
- phpfastcache/phpssdb: ~1.0.0
- phpfastcache/riak-client: ~1.4.4
- predis/predis: ~1.1.0
Conflicts
- v7.x-dev
- 7.0.10
- 7.0.9
- 7.0.8
- 7.0.7
- 7.0.6
- 7.0.5.3
- 7.0.5.2
- 7.0.5.1
- 7.0.5
- 7.0.4
- 7.0.3
- 7.0.2
- 7.0.1
- 7.0.0
- 7.0.0-RC4
- 7.0.0-RC3
- 7.0.0-RC2
- 7.0.0-RC
- 7.0.0-beta3
- 7.0.0-beta2
- 7.0.0-beta
- 7.0.0-alpha3
- 7.0.0-alpha2
- 7.0.0-alpha
- v6.x-dev
- 6.1.3
- 6.1.2
- 6.1.1
- 6.1.0
- 6.0.8
- 6.0.7
- 6.0.6
- 6.0.5
- 6.0.4
- 6.0.3
- 6.0.2
- 6.0.1
- 6.0.0
- 6.0.0-rc4
- 6.0.0-rc3
- 6.0.0-rc2
- 6.0.0-rc1
- 6.0.0-beta2
- 6.0.0-beta1
- 6.0.0-alpha2
- 6.0.0-alpha
- v5.x-dev
- 5.0.21
- 5.0.20
- 5.0.19
- 5.0.18
- 5.0.17
- 5.0.16
- 5.0.15
- 5.0.14
- 5.0.13
- 5.0.12
- 5.0.11
- 5.0.10
- 5.0.9
- 5.0.8
- 5.0.7
- 5.0.6
- 5.0.5
- 5.0.4
- 5.0.3
- 5.0.2
- 5.0.1
- 5.0.0
- 5.0.0-rc3
- 5.0.0-rc2
- 5.0.0-rc1
- 5.0.0-beta2
- 5.0.0-beta1
- 5.0.0-alpha2
- 5.0.0-alpha1
- v4.x-dev
- 4.3.18
- 4.3.17
- 4.3.16
- 4.3.15
- 4.3.14
- 4.3.13
- 4.3.12
- 4.3.11
- 4.3.10
- 4.3.9
- 4.3.8
- 4.3.7
- 4.3.6
- 4.3.5
- 4.3.4
- 4.3.3
- 4.3.2
- 4.3.1
- 4.3
- 4.2.4
- 4.2.3
- 4.2.2
- 4.2.1
- 4.1.1
- 4.1.0
- 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.2-beta1
- 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-master
This package is auto-updated.
Last update: 2024-09-22 07:32:59 UTC
README
⚠️ 请注意,V7 是 PhpFastCache 的一个主要(不兼容向后)更新!
由于 V7 完全不兼容以前的版本,请仔细阅读迁移指南,以确保尽可能平稳的迁移。最大的变化之一是配置系统,它现在是一个对象,取代了我们当时使用的原始数组。请注意,V7 至少需要 PHP7 或更高版本才能正常工作。
简单而强大的 PHP 缓存类
更多详细信息请参阅 Wiki。抽象的简单性:一个类用于多个后端缓存。您无需多次重写代码。
PHP7 强制类型
从 V7 版本开始,PhpFastCache 强制执行 PHP7 强制类型,以确保完全兼容 PHP7 并且类型感知。这确保了当处理变量类型时,库是完全可靠的。
当前支持的驱动程序 *
💡 欢迎通过创建一个新的 Pull Request 提出驱动程序,它们是受欢迎的!
* 驱动程序描述可在 DOCS/DRIVERS.md 中找到
❓ 请注意,由于 PHP7,一些 PHP 扩展尚未更新(并且可能不会更新),我们保留代码在此处,但无法保证它们仍然正常工作,尤其是由于 Opcache 引入的核心更改。
不要忘记 Symfony/Drupal 开发者!
从 V5 版本开始,phpFastCache 附带一个 Symfony Bundle。它现在是一个成熟的项目 Flex - ready,因此请随时尝试并报告任何错误(如果有)。
此外,一个 Drupal 8 模块 正在开发中,将其添加到您的收藏项目,以获得首次公开发布的通知。
非“传统”缓存
phpFastCache 与传统的缓存方法不同,这些方法会持续读写文件、sqlite 或保持大量连接到 memcache、redis、mongodb...
此外,当您使用高性能驱动程序时,您的缺失命中将大大减少。
与您在互联网上找到的通常缓存库略有不同,phpFastCache 库尽可能减少了 I/O 和 CPU 的负载。
<?php use Phpfastcache\CacheManager; CacheManager::getInstance('files', $config); // An alternative exists: CacheManager::Files($config);
减少数据库/Webservice 调用
您的网站有 10,000 名在线访客,并且您的动态页面必须在每次页面加载时向数据库或 Webservice 发送 10,000 次相同的查询。
使用 phpFastCache,您的页面只向您的 DB/WS 发送 1 次查询,并使用缓存为其他 9,999 名访客提供服务。
您当然可以决定 TTL 以满足您的需求。
丰富的开发 API
PhpFastCache 为您提供大量有用的 API
项目 API (ExtendedCacheItemInterface)
* 需要启用配置指令 "itemDetailedDate",否则将抛出 \LogicException
项目池 API (ExtendedCacheItemPoolInterface)
它还支持多个调用、标记、设置缓存文件夹。有关更多信息,请参阅我们的示例文件夹。
PhpFastCache 版本控制 API
PhpFastCache提供了一个类,可以为您提供关于您的PhpFastCache安装的基本信息
- 使用
Phpfastcache\Api::GetVersion();
获取API版本(Item+Pool接口) - 使用
Phpfastcache\Api::getChangelog();
获取API变更日志(Item+Pool接口) - 使用
Phpfastcache\Api::getPhpFastCacheVersion();
获取PhpFastCache版本 - 使用
Phpfastcache\Api::getPhpFastCacheChangelog();
获取PhpFastCache变更日志
想要保持简单?
😅 好消息,从V6版本开始,提供了一个Psr16适配器,使用非常基本的getter/setter来使缓存保持最简单
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');
有关实现和事件的更多信息,请参阅Wiki
介绍新的辅助工具
📚 从V6版本开始,PhpFastCache提供了一些辅助工具,使您的代码更容易编写。
- ActOnAll Helper,帮助您同时操作多个实例。
- CacheConditional Helper,帮助您更容易地编写基本的条件语句。
- Psr16适配器,如上所述,保持简单。
未来可能还会有更多,欢迎贡献力量!
像开啤酒一样快速实现
👍 第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
💾 旧版支持(不使用Composer)
- 有关更多信息,请参阅examples/withoutComposer.php文件。
⚡ 第3步:享受!您的网站现在比闪电还快!
对于好奇的开发者,还有很多其他示例可供参考这里。
💥 phpFastCache支持
发现了一个问题或有一个想法?请在这里告诉我们!