Skip to content

Instantly share code, notes, and snippets.

@dspe
Created February 13, 2018 21:41
Show Gist options
  • Save dspe/759d065b032fa26301fde699e55ce757 to your computer and use it in GitHub Desktop.
Save dspe/759d065b032fa26301fde699e55ce757 to your computer and use it in GitHub Desktop.
Symfony 3 Cache Pool - SiteAccess awareness
# File: app/config/cache_pool/cache.redis.yml
# Reusable service for redis cache for use in tests/docker/platform.sh
services:
# create a new service
cache.adapter.redis.sa_aware:
class: eZ\DemoBundle\Cache\RedisSAAdapter
abstract: true
tags:
- { name: cache.pool, provider: "cache.default_redis_provider", clearer: "cache.default_clearer" }
- { name: monolog.logger, channel: "@cache" }
arguments:
- ""
- ""
- "0"
- "@ezpublish.siteaccess"
calls:
- { method: "setLogger", arguments: [ "@?logger" ] }
# Default configuration
cache.redis:
parent: cache.adapter.redis.sa_aware
tags:
- name: cache.pool
clearer: cache.app_clearer
# Examples from vendor/symfony/symfony/src/Symfony/Component/Cache/Traits/RedisTrait.php:
# redis://localhost:6379
# redis://secret@example.com:1234/13
# redis://secret@/var/run/redis.sock/13?persistent_id=4&class=Redis&timeout=3&retry_interval=3
# provider: 'redis://%cache_dsn%'
provider: 'redis://localhost:6379'
# namespace: caribou
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace eZ\DemoBundle\Cache;
use Symfony\Component\Cache\Adapter\AbstractAdapter;
use Symfony\Component\Cache\Traits\RedisTrait;
class RedisSAAdapter extends AbstractAdapter
{
use RedisTrait;
/**
* @param \Redis|\RedisArray|\RedisCluster|\Predis\Client $redisClient The redis client
* @param string $namespace The default namespace
* @param int $defaultLifetime The default lifetime
* @param \eZ\Publish\Core\MVC\Symfony\SiteAccess $siteAccessService
*/
public function __construct($redisClient, $namespace = '', $defaultLifetime = 0, $siteAccessService)
{
// maybe ugly but works ;)
if ($siteAccessService != null ) {
$namespace = $siteAccessService->name;
}
$this->init($redisClient, $namespace, $defaultLifetime);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment