Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use it ? #1

Closed
bastien70 opened this issue Jun 18, 2021 · 8 comments
Closed

How to use it ? #1

bastien70 opened this issue Jun 18, 2021 · 8 comments
Assignees
Labels
documentation Improvements or additions to documentation

Comments

@bastien70
Copy link

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
Copy link
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'

@Lustmored Lustmored self-assigned this Jun 21, 2021
@Lustmored Lustmored added the documentation Improvements or additions to documentation label Jun 21, 2021
@bastien70
Copy link
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
Copy link

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
Copy link
Author

Okay it works thanks !

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

@Lustmored
Copy link
Owner

Lustmored commented Jul 1, 2021

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
Copy link
Author

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

@r-laferte
Copy link

r-laferte commented Sep 9, 2021

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
Copy link

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
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

5 participants