lustmored/flysystem-v2-simple-cache-adapter

简单的Flysystem PSR缓存适配器装饰器。

0.3.0 2022-01-17 09:53 UTC

This package is auto-updated.

Last update: 2024-09-17 15:28:55 UTC


README

Build Status

为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-bundleuploads_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%的文件存在。