lustmored / flysystem-v2-simple-cache-adapter
简单的Flysystem PSR缓存适配器装饰器。
Requires
- php: >=7.2
- league/flysystem: ^2.0 | ^3.0
- psr/cache: ^1.0 | ^2.0 | ^3.0
Requires (Dev)
- ext-redis: *
- friendsofphp/php-cs-fixer: ^3.0
- league/flysystem-adapter-test-utilities: ^2.0 | ^3.0
- league/flysystem-aws-s3-v3: ^2.0 | ^3.0
- league/flysystem-memory: ^2.0 | ^3.0
- phpbench/phpbench: ^1.0.2
- phpunit/phpunit: ^9.4
- symfony/cache: ^5.4 | ^6.0
- symfony/dotenv: ^5.4 | ^6.0
README
为Flysystem v2适配器提供简单的缓存装饰器。
安装
composer require lustmored/flysystem-v2-simple-cache-adapter
使用
CacheAdapter只需要2个参数
$adapter
- 要装饰的文件系统适配器$cachePool
- PSR-6兼容的缓存池对象
在Symfony中使用flysystem-bundle
的示例配置(感谢@weaverryan总结)
[可选] 创建要使用的缓存池
# config/packages/cache.yaml framework: cache: pools: cache.flysystem.psr6: adapter: cache.app
配置服务。在这里@flysystem.adapter.uploads_adapter
是Flysystem适配器(基于flysystem-bundle
对uploads_adapter
存储的配置)
# config/services.yaml services: # ... flysystem.cache.adapter: class: Lustmored\Flysystem\Cache\CacheAdapter arguments: $adapter: '@flysystem.adapter.uploads_adapter' $cachePool: '@cache.flysystem.psr6'
最后,将其连接到flysytem-bundle
# config/packages/flysystem.yaml flysystem: storages: # ... your other storages cached_public_uploads_storage: # point this at your service id from above adapter: 'flysystem.cache.adapter'
架构
请注意,这个库处于早期阶段,缓存格式或功能可能会改变。我为自己项目和需求创建了它。
想法来自flysystem-cached-driver
,但缓存逻辑被重新思考。它不是存储在一个大缓存中(当你有成千上万的文件时,这会杀死内存),而是按每个文件存储项。
因此,至少现在,在listContents
方法上没有获得任何好处,但在fileExists
和与元数据相关的方 法(尤其是在网络/云文件系统,如S3)上,可能带来巨大的好处。
性能
在Docker中的Redis(通过提供的docker-compose.yml
)上运行基准测试。S3基准测试需要设置环境变量(例如通过提供.env.bench.local
)。缓存基准测试通过调用listContents
来预热缓存。
以下是关于0.0.4版本的开发机器上的单个基准测试运行的报告。它们表明,使用本地适配器缓存元数据是没有意义的,而使用它来查询现有文件时,S3会带来潜在巨大的好处。fileExists
基准测试随机查询文件,其中大约有50%的文件存在。