naroga/stash-bundle

将 Stash 缓存库集成到 Symfony 中。

安装: 19

依赖项: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 43

类型:symfony-bundle

0.7.1 2020-02-25 13:24 UTC

README

License Latest Stable Version Coverage Status Total Downloads

TedivmStashBundleStash 缓存库 集成到 Symfony 中,为各种缓存引擎提供了强大的抽象。此包提供缓存服务,将 Stash 信息添加到 Web Profiler 工具栏,并添加了 Doctrine Common Cache 库的集成。

此包和 Stash 都采用新的 BSD 许可证。请在 Github 上给我们 Fork 一下!

安装

使用 composer 添加此包,要么在命令行中要求它

composer require tedivm/stash-bundle

要么直接将其添加到您的 composer.json 文件中

"require": {
    "tedivm/stash-bundle": "0.7.*"
}

将包添加到 app/AppKernel.php

public function registerBundles()
{
    return array(
        new Tedivm\StashBundle\TedivmStashBundle(),
    );
}

然后在 app/config/config.yml 中设置基本配置

stash: ~

使用

只需获取默认缓存池服务

$pool = $this->container->get('stash');

或自定义定义的缓存池

$pool = $this->container->get('stash.custom_cache');

然后您可以直接使用缓存服务

$item = $pool->getItem($id, 'info'); // cache keys, more than one can be used

$info = $item->get();

if($item->isMiss())
{
    $info = loadInfo($id);
    $item->set($userInfo, 60); // second parameter is TTL (in seconds or future \Datetime object)
}

return $info;

(有关使用缓存服务的更多信息,请参阅Stash 文档)

配置

默认缓存服务

为了快速开始,您可以使用单个驱动定义单个缓存服务

stash:
    drivers: [ FileSystem ]
    FileSystem: ~

此缓存服务将注册为 stash.default_cache,它也将自动别名为 cache

配置驱动器

您可以直接在配置中设置缓存驱动器的单个参数

stash:
    drivers: [ FileSystem ]
    FileSystem:
        dirSplit: 3
        path: /tmp

多个驱动器

如果您想按顺序使用多个驱动器,您可以分别列出它们

stash:
    drivers: [ Apc, FileSystem ]
    Apc: ~
    FileSystem:
        path: /tmp

缓存服务将自动配置为复合驱动器,驱动器的查询顺序为指定的顺序(例如,在这个例子中,Apc 将首先查询,然后是 FileSystem,如果该查询失败)

内存中

默认情况下,每个缓存服务都包括内存缓存:在单个请求的生命周期内,存储或从缓存服务检索的任何值都将存储在内存中,在检查任何其他驱动器之前将检查内存表示。但在某些情况下(例如,长时间运行的 CLI 批处理脚本),这可能不是所希望的。在这些情况下,可以禁用内存驱动器

stash:
    drivers: [ Apc ]
    inMemory: false
    Apc: ~

Doctrine 适配器

Stash 提供了一个 Doctrine 缓存适配器,以便您的 Stash 缓存服务可以注入到任何接受 DoctrineCacheInterface 对象的服务中。要为服务打开适配器,设置参数

stash:
    drivers: [ Apc ]
    registerDoctrineAdapter: true
    Apc: ~

对于默认缓存,适配器服务将添加到容器中,名称为 stash.adapter.doctrine.default_cache。您可以在任何使用常规 Doctrine 缓存对象的地方使用它

doctrine:
    orm:
        metadata_cache_driver:
            type: service
            id: stash.adapter.doctrine.default_cache
        query_cache_driver:
            type: service
            id: stash.adapter.doctrine.default_cache
        result_cache_driver:
            type: service
            id: stash.adapter.doctrine.default_cache

会话适配器

Stash 提供了一个会话适配器,允许将 Symfony 会话直接存储在缓存中。要打开适配器,设置参数

stash:
    drivers: [ Apc ]
    registerSessionHandler: true
    Apc: ~

一旦启用,在框架包中启用它,它将自动使用

framework:
    session:
        handler_id: stash.adapter.session.default_cache

记录器

Stash 支持 PSR 兼容的日志库,您可以通过传递记录器的服务名称作为参数来指定使用的库

stash:
	drivers: [ FileSystem ]
	logger: logger
	FileSystem: ~ 

多个服务

您还可以配置多个服务,每个服务都是完全独立的

stash:
    caches:
        first:
            drivers: [ FileSystem ]
            registerDoctrineAdapter: true
            FileSystem: ~
        second:
            drivers: [ Apc, FileSystem ]
            inMemory: false
            logger: logger
            FileSystem: ~

每个服务都使用一个单独的、不同的内部名称空间中的键进行定义,因此您可以使用多个服务来避免在具有单个后端的情况下不同服务之间的键冲突。

当定义多个缓存时,您可以手动定义一个默认值,该默认值将别名为 stash 服务

stash:
    default_cache: first
    first:
        ...
    second:
        ...

如果您不这样做,第一个定义的服务将被设置为默认值。

跟踪

StashBundle 包含一个模块,用于跟踪在请求期间所有缓存查询的键,以便进行调试。默认情况下,此模块在 devtest 环境中启用,但在其他地方禁用。然而,如果您想覆盖默认行为,您可以在配置中启用或禁用此行为。

stash:
    tracking: true # enables query logging, false to disable
    tracking_values: true # enables query logging of full cache values, false to disable

Stash 驱动配置

每个驱动程序都附带一组默认选项,可以单独覆盖。

FileSystem:
    dirSplit:               2
    encoder:                Native
    path:                   %kernel.cache_dir%/stash
    filePermissions:        0660
    dirPermissions:         0770
    memKeyLimit:            20
SQLite:
    path:                   %kernel.cache_dir%/stash
    filePermissions:        0660
    dirPermissions:         0770
    busyTimeout:            500
    nesting:                0
    subdriver:              PDO
Apc:
    ttl:                    300
    namespace:              <none>
Memcache:
    servers:
        - { server: 127.0.0.1, port: 11211, weight: 1 }