drupal / cache_remember
1.0.0
2024-02-28 14:12 UTC
Requires
- php: >=8.1.0
- drupal/core: ^10.2
Requires (Dev)
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^9.6
- vimeo/psalm: ^5.22
This package is not auto-updated.
Last update: 2024-09-16 08:21:16 UTC
README
Drupal Cache Remember
关于项目
本项目是一个测试场,旨在通过使用类似于Laravel中的“记住”风格缓存接口,为Drupal提供一种更简单的方式来填充缓存。
此工作的原始开发始于Drupal问题队列,但开发停滞。我认为这主要是因为在实际缓存场景中很难审查提议的更改。该项目的计划是提供一种在现实世界场景中使用界面的简单方法,并积累一些反馈,最终以补丁的形式返回给Drupal。
此存储库中记录了两种方法。
- 第一种,CacheRememberInterface,是Drupal核心问题中的原始方法。但它有一个缺点,因为它需要为每次调用计算预期的过期时间,因为你不知道缓存是否命中。由于Drupal处理过期值的方式,这需要检索当前时间,即使不需要,也会增加可能可测量的开销和复杂性,从而影响缓存命中率。
- 第二种,SimpleCacheRememberInterface,是一种更新的方法,在许多方面简化了接口。它将cid、expire和tags参数传递给回调函数,允许它们仅在缓存未命中时计算和设置。当与命名参数结合使用时,这使得以更易于理解的方式完成常见缓存任务变得容易得多。
(返回顶部)
使用
您可以通过使用composer简单要求此项目来开始使用此项目。
composer require drupal/cache_remember
之后,您可以在任何想要使用此接口的类上简单地使用类上的一个辅助特质。
CacheRememberInterface
use \Neclimdul\DrupalCacheRemember\CacheRememberHelperTrait;
class OriginalApproach {
use CacheRememberHelperTrait;
public function __construct() {
// Use a real backend for this. Probably something injected from the service container.
$this->setCacheRememberBackend(new \Drupal\Core\Cache\MemoryBackend());
}
public function myFunction() {
print $this->remember('test:cid', function() {
return 'My test value';
}, \Drupal::time()->getCurrentTime() + 600, ['custom:tag']);
print $this->rememberPermanent('test:cid2', function() {
return 'My test value';
});
}
}
SimpleCacheRememberInterface
use \Neclimdul\DrupalCacheRemember\SimpleCacheRememberHelperTrait;
class UpdatedApproach {
use SimpleCacheRememberHelperTrait;
public function __construct() {
// Use a real backend for this. Probably something injected from the service container.
$this->setCacheRememberBackend(new \Drupal\Core\Cache\MemoryBackend());
}
public function myFunction() {
print $this->remember('test:cid', function($cid, &$expire, &$tags) {
$expire = \Drupal::time()->getCurrentTime() + 600;
$tags = ['custom:tag'];
return 'My test value';
});
print $this->remember('test:cid2', function($cid, &$expire) {
$expire = \Drupal::time()->getCurrentTime() + 600;
return 'My test value';
}, tags: ['custom:tag']);
}
}
(返回顶部)
路线图
经过充分的测试后,这将被作为补丁提供给Drupal核心,因为将其作为缓存后端的一部分而不是像此库那样附加到单个类中更有意义。
https://www.drupal.org/project/drupal/issues/3013177
(返回顶部)
贡献
非常欢迎贡献和反馈!请提交任何反馈、建议或改进此项目的改进。
(返回顶部)
许可证
由于这是针对Drupal核心,所有更改必须是GPL 2.0,否则将无法贡献回去。请参阅LICENSE.txt
获取更多信息。
(返回顶部)