omnilog / psr6-dynamo-db-bundle
使用 AWS DynamoDB 为 Symfony 实现的 PSR-6 和 PSR-16 缓存实现
Requires
- php: ^7.2 | ^8.0
- omnilog/psr6-dynamo-db: 0.0.1
- symfony/cache: ^4.4|^5.0
- symfony/framework-bundle: ^4.4|^5.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16
- phpstan/phpstan: ^0.12.44
- phpunit/phpunit: ^9.3
This package is auto-updated.
Last update: 2024-08-26 22:07:03 UTC
README
不使用 Symfony?请使用 独立库。
安装
composer require omnilog/psr6-dynamo-db-bundle
配置
创建文件 config/packages/dynamo_db_cache.yaml
并至少放置表名
omnilog_dynamo_db_cache: table: myCacheTableName
其他所有设置都有默认值,尽管你可能还想要配置 DynamoDB 客户端。
配置选项
replace_default_adapter
- 设置为true
以替换默认的AdapterInterface
和CacheInterface
实现(默认: false)table
- 用于存储缓存的表(必需)client_service
- 将用作 DynamoDB 客户端的服务的名称,如果没有设置,此包将尝试创建一个(更多详细信息请参阅下面的client_config
选项),但使用有限client_config
- 如果未配置client_service
,它将根据此配置的值创建。它包含两个子键region
- AWS 区域(默认: us-east-1)version
- 服务版本(默认: latest)- 没有其他选项可用,如果您需要配置更多,请创建并分配自定义
client_service
encoder
- 包含编码器的设置service
- 将用作编码器的服务- 可以是内置服务(
omnilog.dynamo_cache.encoder.serialize
,omnilog.dynamo_cache.encoder.json
) - 可以是实现
\Omnilog\DynamoDbCache\Encoder\CacheItemEncoderInterface
接口的自定义服务 - 默认值: omnilog.dynamo_cache.encoder.serialize
- 可以是内置服务(
json_options
- JSON 编码器的设置,如果使用其他编码器则忽略encode_flags
- 相当于传递给json_encode()
的标志decode_flags
- 相当于传递给json_decode()
的标志depth
-json_encode()
和json_decode()
的深度参数
primary_key_field
- 将用作主键的字段名称(默认: id)ttl_field
- 将用作 ttl 字段的名字(默认: ttl)value_field
- 将用作值字段的名称(默认: value)key_prefix
- 项键前的前缀(默认: null 表示无)
自动生成的默认配置(通过 bin/console config:dump omnilog_dynamo_db_cache
)
# Default configuration for extension with alias: "omnilog_dynamo_db_cache" omnilog_dynamo_db_cache: # Replace default cache adapter with this one replace_default_adapter: false # The DynamoDB table to use as cache table: null # The service to use as the Dynamo DB client client_service: null # The Dynamo DB client configuration. If you need finer tuning, create the service yourself and assign it in client_service client_config: # The AWS region region: us-east-1 # The service version version: latest encoder: # The service to be used as the encoder/decoder service: omnilog.dynamo_cache.encoder.serialize # Settings for the json encoder json_options: # The flags that will be passed when encoding encode_flags: 0 # The flags that will be passed when decoding decode_flags: 0 # The depth of the JSON parsing for encoding/decoding depth: 512 # Session related configuration session: # The ttl for the session, defaults to ini setting session.gc_maxlifetime ttl: null # The prefix for sessions prefix: session_ # The field to be used as primary key primary_key_field: id # The field to be used as ttl ttl_field: ttl # The field to be used as value value_field: value # The prefix used in front of keys when storing key_prefix: null
使用方法
您可以使用两个可用的服务之一
Omnilog\DynamoDbCache\DynamoDbCache
- 此服务不实现 Symfony 的AdapterInterface
或CacheInterface
Omnilog\DynamoDbCacheBundle\Cache\DynamoDbCacheAdapter
- 此服务实现了 Symfony 的AdapterInterface
和CacheInterface
如果将 replace_default_adapter
设置为 true
,您还可以将这些接口用作服务
Symfony\Component\Cache\Adapter\AdapterInterface
- PSR-6 缓存的 Symfony 接口Symfony\Contracts\Cache\CacheInterface
- 简单缓存的 Symfony 接口Psr\Cache\CacheItemPoolInterface
- PSR-6 接口Psr\SimpleCache\CacheInterface
- PSR-16 接口
示例
<?php use Omnilog\DynamoDbCacheBundle\Cache\DynamoDbCacheAdapter; use Omnilog\DynamoDbCache\DynamoDbCache; use Symfony\Contracts\Cache\ItemInterface; class MyService { public function __construct(DynamoDbCache $cache, DynamoDbCacheAdapter $adapter) { // it doesn't matter whether you use the adapter or not, the usage for PSR-6 is the same, the // only difference is that adapter implements the Symfony interface and thus you can // use it to replace the default AdapterInterface implementation $item = $cache->getItem('test'); $item2 = $adapter->getItem('test'); $item->set('some value'); $adapter->save($item); // or using the CacheInterface api $value = $adapter->get('test', function (ItemInterface $item) { $item->expiresAfter(3600); return 'new-cache-value'; }); $cache->delete('test'); } }
当使用 replace_default_adapter
选项时
<?php use Symfony\Component\Cache\Adapter\AdapterInterface; use Symfony\Contracts\Cache\CacheInterface; use Symfony\Contracts\Cache\ItemInterface; class MyService { public function __construct(AdapterInterface $cache) { $item = $cache->getItem('test'); // do stuff with cache item $cache->save($item); } } class MyService2 { public function __construct(CacheInterface $cache) { $cache->get('test', function (ItemInterface $item) { return 'new-value'; }); } }
或者使用 PSR 接口
<?php use Psr\Cache\CacheItemPoolInterface; use Psr\SimpleCache\CacheInterface; class MyService { public function __construct(CacheItemPoolInterface $psr6cache, CacheInterface $psr16cache) { $item = $psr6cache->getItem('test'); $value = $item->get(); $item->set('newValue'); $item->expiresAfter(120); $psr6cache->save($item); $value = $psr16cache->get('test'); $psr16cache->set('test', 'newValue', 120); } }
转换器
该包支持所有使用转换器将对象转换为\Omnilog\DynamoDbCache\DynamoCacheItem
的\Psr\Cache\CacheItemInterface
实例。注意,转换过程中可能会丢失一些信息,尤其是过期日期。
此包具有处理默认Symfony \Symfony\Component\Cache\CacheItem
的处理器,同时保留过期日期的信息。
如果您使用其他任何CacheItemInterface
实现,您可能需要编写自己的处理器
<?php use Omnilog\DynamoDbCache\Converter\CacheItemConverterInterface; use Psr\Cache\CacheItemInterface; use Omnilog\DynamoDbCache\DynamoCacheItem; class MyCacheItemConverter implements CacheItemConverterInterface { /** * If this methods returns true, the converter will be used */ public function supports(CacheItemInterface $cacheItem): bool { return $cacheItem instanceof MyCacheItem; } public function convert(CacheItemInterface $cacheItem): DynamoCacheItem { assert($cacheItem instanceof MyCacheItem); return new DynamoCacheItem( $cacheItem->getKey(), $cacheItem->isHit(), $cacheItem->get(), $cacheItem->getExpirationDate() // this is a custom method from the hypothetical MyCacheItem ); } }
您的转换器现在已自动注册到转换器系统中,并在尝试保存MyCacheItem
实例时使用。
如果您不使用自动配置,请使用
omnilog.dynamo_cache.converter
标签您的服务
默认转换器作为最后一种选择可以使用,可以转换所有CacheItemInterface
对象,但由于接口不提供此类信息,因此无法获取过期日期。