Skip to content

Flysystem 3 support#1

Open
rskuipers wants to merge 10 commits intoobj63mc:masterfrom
wedevelopnl:feature/flysystem-3-support
Open

Flysystem 3 support#1
rskuipers wants to merge 10 commits intoobj63mc:masterfrom
wedevelopnl:feature/flysystem-3-support

Conversation

@rskuipers
Copy link

@rskuipers rskuipers commented Nov 9, 2023

As promised here is a PR with all the changes needed to support Flysystem 3.
We're currently using this in production and it works well.

I haven't touched any of the documentation yet so this is most definitely out-of-date.

We are currently running this with Redis as persistent cache:

---
Only:
  envvarset: REDIS_HOST
After:
  - 'silverstripegooglecloudstorage-flysystem'
  - '#corecache'
---
SilverStripe\Core\Injector\Injector:
  RedisClient:
    factory: App\Caching\RedisClientFactory
    constructor:
      host: '`REDIS_HOST`'
      port: '`REDIS_PORT`'
      db: '`REDIS_DB`'
      auth: '`REDIS_AUTH`'

  RedisCacheFactory:
    class: App\Caching\RedisCacheFactory
    constructor:
      client: '%$RedisClient'

  Symfony\Component\Cache\Adapter\FilesystemAdapter.protected:
    factory: '%$RedisCacheFactory'
    constructor:
      key: 'gcmetadata-protected'
      expire: 259200

  Symfony\Component\Cache\Adapter\FilesystemAdapter.public:
    factory: '%$RedisCacheFactory'
    constructor:
      key: 'gcmetadata-public'
      expire: 259200

The RedisCacheFactory looks like this:

<?php

declare(strict_types=1);

namespace App\Caching;

use Psr\Container\NotFoundExceptionInterface;
use Redis;
use SilverStripe\Core\Injector\Factory;
use SilverStripe\Core\Injector\Injector;
use Symfony\Component\Cache\Adapter\AdapterInterface;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Cache\Adapter\ChainAdapter;
use Symfony\Component\Cache\Adapter\RedisAdapter;

class RedisCacheFactory implements Factory
{
    public function __construct(private readonly Redis $redis)
    {
    }

    /**
     * @param $service
     * @param array<string, mixed> $params
     * @return AdapterInterface
     * @throws NotFoundExceptionInterface
     */
    public function create($service, array $params = [])
    {
        $key = $params['key'];
        $expire = (int)$params['expire'];

        return new ChainAdapter([
            new ArrayAdapter(),
            Injector::inst()
                ->createWithArgs(
                    RedisAdapter::class,
                    [
                        $this->redis,
                        $key,
                        $expire,
                    ]
                ),
        ]);
    }
}

Here you can see we use the ChainAdapter to also do in-memory caching for even more speeeeeed.

Let me know what you think.

I also noticed another branch were similar changes were done for Flysystem 3 support, I probably should have opened a draft PR to make our efforts more visible to prevent doing twice the work. We had a project to launch so that's what I focused on :)

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants