phpsymfonyyamllockingphpdotenv

Symfony framework.lock with .env set-up


Problem

I need to set up Symfony framework.yaml parameter framework.lock with 2+ memcached servers using .env. But when I try to do so, it does not recognize 2 servers, but only one, and with a glitch.

Symfony lock docs: https://symfony.com/doc/current/lock.html

What I have tried (with code snippets):

Passing via .env

MEMCACHE_SERVERS='memcached://m1.docker:11211,memcached://m2.docker:11211'
framework:
    lock: '%env(MEMCACHE_SERVERS)%'
    /**
     * @Route("/example")
     */
    public function exampleAction(LockFactory $lockFactory): JsonResponse
    {
        dd($lockFactory->createLock('testing-lock', 2));
    }
-store: Symfony\Component\Lock\Store\MemcachedStore {#22171 ▼
    -memcached: Memcached {#23004 ▼
      servers: array:1 [▼
        0 => array:3 [▼
          "host" => "m1.docker:11211,memcached"
          "port" => 11211
          "type" => "TCP"
        ]
      ]
      options: {▶}
    }
    -initialTtl: 300
    -useExtendedReturn: null
  }

Passing directly to lock parameter

lock: ['memcached://m1.docker:11211','memcached://m2.docker:11211']
  -store: Symfony\Component\Lock\Store\CombinedStore {#22386 ▼
    -stores: array:2 [▼
      0 => Symfony\Component\Lock\Store\MemcachedStore {#23199 ▼
        -memcached: Memcached {#22471 ▼
          servers: array:1 [▼
            0 => array:3 [▼
              "host" => "m1.docker"
              "port" => 11211
              "type" => "TCP"
            ]
          ]
          options: {▶}
        }
        -initialTtl: 300
        -useExtendedReturn: null
      }
      1 => Symfony\Component\Lock\Store\MemcachedStore {#22514 ▼
        -memcached: Memcached {#22625 ▼
          servers: array:1 [▼
            0 => array:3 [▼
              "host" => "m2.docker"
              "port" => 11211
              "type" => "TCP"
            ]
          ]
          options: {▶}
        }
        -initialTtl: 300
        -useExtendedReturn: null
      }
    ]

Question

The question. How I can pass the 2+ memcached servers to framework. lock using .env?


Solution

  • lock: '%env(MEMCACHE_SERVERS)%' and lock: ['memcached://m1.docker:11211','memcached://m2.docker:11211'] looks pretty different to me - I would assume that the array makes the difference here.

    Please try to set the env variable such that it contains an array:

    MEMCACHE_SERVERS='["memcached://m1.docker:11211","memcached://m2.docker:11211"]'

    Afterwards, configure the lock such that it parses the given array:

    framework:
       lock: '%env(json:MEMCACHE_SERVERS)%'