Skip to content

How to use it ? #1

@bastien70

Description

@bastien70

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?

Activity

Lustmored

Lustmored commented on Jun 21, 2021

@Lustmored
Owner

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'
self-assigned this
on Jun 21, 2021
bastien70

bastien70 commented on Jun 25, 2021

@bastien70
Author

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

weaverryan commented on Jun 25, 2021

@weaverryan

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

  1. 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'
  1. 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'
  1. 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

bastien70 commented on Jun 29, 2021

@bastien70
Author

Okay it works thanks !

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

Lustmored

Lustmored commented on Jul 1, 2021

@Lustmored
Owner

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 👍

bastien70

bastien70 commented on Jul 1, 2021

@bastien70
Author

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

laferte-tech

laferte-tech commented on Sep 9, 2021

@laferte-tech

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

yellow1912 commented on Sep 30, 2022

@yellow1912

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

documentationImprovements or additions to documentation

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @weaverryan@yellow1912@Lustmored@bastien70@laferte-tech

      Issue actions

        How to use it ? · Issue #1 · Lustmored/flysystem-v2-simple-cache-adapter