Lustmored / flysystem-v2-simple-cache-adapter

Simple flysystem PSR cache adapter decorator.
MIT License
16 stars 6 forks source link

How to use it ? #1

Closed bastien70 closed 3 years ago

bastien70 commented 3 years ago

Hello, I would like to integrate your adapter into my Symfony project to combine it with FlySystem and LiipImagineBundle. But I don't understand how to use your adapter?

Lustmored commented 3 years ago

Hi,

In Symfony with flysystem-bundle you'd just need to configure the service like this:

flysystem.cache.adapter:
        class: Lustmored\Flysystem\Cache\CacheAdapter
        arguments:
                $adapter: '@flysystem.adapter.public.storage.source'
                $cachePool: '@flysystem.public'

And then use it in config/packages/flysystem.yaml like any other:

public.storage:
        adapter: 'flysystem.cache.adapter'
bastien70 commented 3 years ago

I have a little trouble understanding.

Basically, my setup is as follows:

flysystem.yaml :

flysystem:
    storages:
        uploads_adapter:
            adapter: 'aws'
            options:
                client: Aws\S3\S3Client
                bucket: '%env(AWS_S3_BUCKET_NAME)%'

services.yaml:

Aws\S3\S3Client:
        arguments:
            -   version: '2006-03-01'
                region: "%env(AWS_S3_REGION)%"
                credentials:
                    key: "%env(AWS_S3_ACCESS_ID)%"
                    secret: "%env(AWS_S3_ACCESS_SECRET)%"

liip_imagine.yaml:

liip_imagine:
    # valid drivers options include "gd" or "gmagick" or "imagick"
    driver: "gd"

    loaders:
        flysystem_loader:
            flysystem:
                filesystem_service: uploads_adapter

    # Default loader to use for all filter sets
    data_loader: flysystem_loader

    resolvers:
        flysystem_resolver:
            flysystem:
                filesystem_service: uploads_adapter
                root_url: '%uploads_base_url%'
                cache_prefix: media/cache

    cache: flysystem_resolver

So, if I understand, I should make this :

Add in services.yaml :

flysystem.cache.adapter:
        class: Lustmored\Flysystem\Cache\CacheAdapter
        arguments:
                $adapter: '@flysystem.adapter.public.storage.source'
                $cachePool: '@flysystem.public'

And add in flysystem.yaml :

flysystem.cache.adapter:
        class: Lustmored\Flysystem\Cache\CacheAdapter
        arguments:
                $adapter: '@flysystem.adapter.public.storage.source'
                $cachePool: '@flysystem.public'

? What about my other adapt regarding aws? And for the cache to be effective with liip_imagine?

weaverryan commented 3 years ago

Hi there!

To adapt the code from @Lustmored to your app, I think it would look like this:

1) Setup a cache pool

# config/packages/cache.yaml
framework:
    cache:
        pools:
            cache.flysystem.psr6:
                adapter: cache.app

(this is the same that we have here: https://symfonycasts.com/screencast/symfony-uploads/cached-s3-filesystem#codeblock-1a69ab8273

2) Define a cache adapter. The key thing here is that you need to point it to the REAL adapter that you want this to use, in case there is nothing in the cache yet. In your case, it looks like you're using https://github.com/thephpleague/flysystem-bundle to define you "storages", which I believe means you will have a service with the id flysystem.adapter.uploads_adapter, which is your adapter service. So:

# config/services.yaml
services:
    # ...

    flysystem.cache.adapter:
        class: Lustmored\Flysystem\Cache\CacheAdapter
        arguments:
                $adapter: '@flysystem.adapter.uploads_adapter'
                $cachePool: '@cache.flysystem.psr6'

3) At this point, you have a cache adapter. But you need a Filesystem that USES that adapter. Since you're using the PHPLeague package, I think it would look like this:

# 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'

4) Finally, the above should give you a service id called exactly cached_public_uploads_storage I believe. You can now plug this into Liip:

liip_imagine:
    # ...
    loaders:
        flysystem_loader:
            flysystem:
                filesystem_service: cached_public_uploads_storage

    # ...

    resolvers:
        flysystem_resolver:
            flysystem:
                filesystem_service: cached_public_uploads_storage
                # ....

Yes, it IS complex :). This probably won't be quite right - I was reading from source code and typing here, but it should be close.

Cheers!

bastien70 commented 3 years ago

Okay it works thanks !

@Lustmored Maybe could you upgrade 'psr/cache' to v2 ?

Lustmored commented 3 years ago

Thanks @weaverryan for great summary of usage. I must admit I'm not using this adapter in my main project so it was very helpful. I have updated README.

@bastien70 I have published 0.2.0 with only significant change being allowing psr/cache 1|2|3 :+1:

bastien70 commented 3 years ago

Very good mens ! thank you both, everything is perfect now, and long live this super useful bundle! <3

laferte-tech commented 3 years ago

Just to say thank you so much @weaverryan and @Lustmored for your work and help!

and one question: this cache.app defined in cache.yaml does it need to be defined like in your example ? (app: cache.adapter.apcu) Or maybe should we assign it a special value?

Thanks a lot

yellow1912 commented 2 years ago

Hello,

Sorry for asking a question in a closed issue, but I wonder if you can further explain the usage. For example, lets say I have an aws adapter, now I want to use the cached adapter for that aws adapter, how should I do it?

Do I define the aws adapter service separate and pass the bucket, and client option to it?