ezsystems / stash-bundle
将Stash缓存库集成到Symfony中。
Requires
- php: ^7.3 || ^8.0
- symfony/config: ^5.0
- symfony/dependency-injection: ^5.0
- symfony/http-kernel: ^5.0
- tedivm/stash: ^0.17
Requires (Dev)
Replaces
- tedivm/stash-bundle: v0.9.1
README
TedivmStashBundle将Stash缓存库集成到Symfony中,为多种缓存引擎提供强大的抽象。该bundle提供了一个缓存服务,将Stash信息添加到Web Profiler工具栏,并添加了Doctrine Common Cache库的集成。
该bundle和Stash都采用新BSD许可协议。请在Github上给我们分叉!
安装
使用composer添加bundle,可以在命令行上要求它
composer require tedivm/stash-bundle
或者在您的composer.json
文件中直接添加它
"require": { "tedivm/stash-bundle": "0.7.*" }
将bundle添加到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 Cache对象的地方使用它
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
Session适配器
Stash提供了一个会话适配器,允许将Symfony会话直接存储在缓存中。要打开适配器,请设置参数
stash: drivers: [ Apc ] registerSessionHandler: true Apc: ~
一旦启用,在框架bundle中启用它,它将自动使用
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 包含一个模块,用于跟踪请求过程中所有缓存查询的键,以便进行调试。默认情况下,此模块在dev
和test
环境中启用,但在其他地方禁用。但是,如果您想覆盖默认行为,可以在配置中启用或禁用此行为。
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 }