sedpro/cachedecorator

为 zf2 项目提供的简单缓存装饰器模块

安装: 9

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 0

开放问题: 0

类型:zf2-module

dev-master 2015-12-11 12:58 UTC

This package is not auto-updated.

Last update: 2024-09-28 18:37:06 UTC


README

版本 0.1

此模块允许您简单缓存服务方法。

主要想法是创建一个缓存模块,可以在不更改代码的情况下连接到项目。您需要做的就是调整配置。

安装

使用 composer composer 进行安装。

php composer.phar require  sedpro/cachedecorator:dev-master

在您的 composer.json 中添加此项目

"require": { "sedpro/cachedecorator": "dev-master" }

安装后

配置

  • config/application.config.php 中的 modules 数组下添加模块 Cachedecorator

  • Module.php 文件中的 getServiceConfig 函数移除您想要缓存的服务

  • 在您的 config/autoload/global.php 文件中添加两个值

      'caches' => [
          \Cachedecorator\Module::STORAGE => [
              'adapter' => [
                  'name' => 'memcached',
              ],
              'options' => [
                  'ttl' => 3600,
                  'servers' => [
                      'node0' => [
                          'host' => '127.0.0.1',
                          'port' => 11211,
                      ],
                  ],
                  'namespace' => 'some_ns:',
              ],
          ],
      ],
      \Cachedecorator\Module::METHODS => [
          'Application\Service\Example' => [
              'getItems',
          ],
      ],

'caches' 包含项目中所使用的所有缓存。它们将在抽象工厂 Zend\Cache\Service\StorageCacheAbstractServiceFactory 中实例化,该工厂在 vendor/sedpro/cachedecorator/config/module.config.php 中被调用。如果您已经使用此工厂,则不会有冲突。

'\Cachedecorator\Module::STORAGE' 是缓存存储适配器,用于存储服务的输出。

'\Cachedecorator\Module::METHODS' 是您想要缓存的服务的列表。只有列出的函数才会被缓存。

示例

如果您使用上述配置,Application\Service\Example 类的 getItems 方法将被缓存。您可以像往常一样使用它。

  $exampleService = $this->getServiceLocator()->get('Application\Service\Example');
  $items = $exampleService->getItems(); // cached
  $values = $exampleService->getValues(); // not cached