rikudou / psr6-dynamo-db-bundle
使用 AWS DynamoDB 实现的 PSR-6 和 PSR-16 缓存,适用于 Symfony
Requires
- php: ^8.0
- ext-json: *
- rikudou/psr6-dynamo-db: ^3.1
- symfony/cache: ^6.0 || ^7.0
- symfony/framework-bundle: ^6.0 || ^7.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- phpstan/phpstan: ^1.5
- phpunit/phpunit: ^9.5
README
不使用 Symfony?请使用 独立库。
自版本 2 以来,此库使用轻量级的 async-aws/dynamo-db,而不是完整的 AWS SDK。
安装
composer require rikudou/psr6-dynamo-db-bundle
配置
创建文件 config/packages/dynamo_db_cache.yaml
并至少放置表名
rikudou_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)- 没有其他选项可用,如果您需要配置更多,请创建并分配自定义
client_service
encoder
- 包含编码器的设置service
- 将用作编码器的服务- 可以是内置服务之一(
rikudou.dynamo_cache.encoder.serialize
、rikudou.dynamo_cache.encoder.json
) - 可以是实现
\Rikudou\DynamoDbCache\Encoder\CacheItemEncoderInterface
接口的自定义服务 - 默认值:rikudou.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 rikudou_dynamo_db_cache
)
# Default configuration for extension with alias: "rikudou_dynamo_db_cache" rikudou_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 encoder: # The service to be used as the encoder/decoder service: rikudou.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
使用方法
您可以使用两个可用服务之一
Rikudou\DynamoDbCache\DynamoDbCache
- 此服务不实现 Symfony 的AdapterInterface
或CacheInterface
Rikudou\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 Rikudou\DynamoDbCacheBundle\Cache\DynamoDbCacheAdapter; use Rikudou\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); } }
转换器
此包支持所有 \Psr\Cache\CacheItemInterface
实例,通过使用转换器将对象转换为 \Rikudou\DynamoDbCache\DynamoCacheItem
。请注意,转换过程中可能会丢失一些信息,尤其是过期日期。
此包有一个处理程序,用于处理默认的 Symfony \Symfony\Component\Cache\CacheItem
,其中也保留了关于过期日期的信息。
如果您使用任何其他 CacheItemInterface
实现,您可能需要编写自己的处理程序
<?php use Rikudou\DynamoDbCache\Converter\CacheItemConverterInterface; use Psr\Cache\CacheItemInterface; use Rikudou\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
实例时使用。
如果您不使用自动配置,请使用
rikudou.dynamo_cache.converter
标记您的服务
默认的转换器作为最后一个选项将使用,可以转换所有 CacheItemInterface
对象,但由于接口没有提供此类信息,因此无法获取过期日期。
会话
此包包含一个会话处理程序,用于将您的会话数据存储在 DynamoDB 中。
要使用它,您需要在 framework.yaml
中指定 handler_id
为 rikudou.dynamo_cache.session
,或将 replace_default_adapter
设置为 true
并且 从 framework.yaml
中删除 handler_id
(flex 创建的默认配置明确指定 handler_id
为 null
,如果您想使用 replace_default_adapter
,请从配置中删除它)。