Initial commit: CloudOps infrastructure platform
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mautic\CacheBundle\Cache;
|
||||
|
||||
use Mautic\CoreBundle\Helper\CoreParametersHelper;
|
||||
use Psr\Cache\CacheItemInterface;
|
||||
use Symfony\Component\Cache\Adapter\AdapterInterface;
|
||||
use Symfony\Component\Cache\CacheItem;
|
||||
use Symfony\Component\Cache\Psr16Cache;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
/**
|
||||
* Class CacheProvider provides a caching mechanism using adapters, it provides both PSR-6 and PSR-16.
|
||||
*/
|
||||
abstract class AbstractCacheProvider implements CacheProviderInterface
|
||||
{
|
||||
private ?AdapterInterface $adapter = null;
|
||||
private ?Psr16Cache $psr16 = null;
|
||||
|
||||
public function __construct(
|
||||
private CoreParametersHelper $coreParametersHelper,
|
||||
private ContainerInterface $container,
|
||||
) {
|
||||
}
|
||||
|
||||
abstract public function getCacheAdapter(): AdapterInterface;
|
||||
|
||||
public function getSimpleCache(): Psr16Cache
|
||||
{
|
||||
if (is_null($this->psr16)) {
|
||||
$this->psr16 = new Psr16Cache($this->getCacheAdapter());
|
||||
}
|
||||
|
||||
return $this->psr16;
|
||||
}
|
||||
|
||||
public function getItem($key): CacheItem
|
||||
{
|
||||
return $this->getCacheAdapter()->getItem($key);
|
||||
}
|
||||
|
||||
public function getItems(array $keys = []): iterable
|
||||
{
|
||||
return $this->getCacheAdapter()->getItems($keys);
|
||||
}
|
||||
|
||||
public function hasItem($key): bool
|
||||
{
|
||||
return $this->getCacheAdapter()->hasItem($key);
|
||||
}
|
||||
|
||||
public function clear(string $prefix = ''): bool
|
||||
{
|
||||
return $this->getCacheAdapter()->clear();
|
||||
}
|
||||
|
||||
public function deleteItem($key): bool
|
||||
{
|
||||
return $this->getCacheAdapter()->deleteItem($key);
|
||||
}
|
||||
|
||||
public function deleteItems(array $keys): bool
|
||||
{
|
||||
return $this->getCacheAdapter()->deleteItems($keys);
|
||||
}
|
||||
|
||||
public function save(CacheItemInterface $item): bool
|
||||
{
|
||||
return $this->getCacheAdapter()->save($item);
|
||||
}
|
||||
|
||||
public function saveDeferred(CacheItemInterface $item): bool
|
||||
{
|
||||
return $this->getCacheAdapter()->saveDeferred($item);
|
||||
}
|
||||
|
||||
public function commit(): bool
|
||||
{
|
||||
return $this->getCacheAdapter()->commit();
|
||||
}
|
||||
|
||||
protected function cacheAdapterFactory(string $parameter): AdapterInterface
|
||||
{
|
||||
if (null === $this->adapter) {
|
||||
$service = $this->coreParametersHelper->get($parameter);
|
||||
$this->adapter = $this->container->get($service);
|
||||
}
|
||||
|
||||
return $this->adapter;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mautic\CacheBundle\Cache\Adapter;
|
||||
|
||||
use Symfony\Component\Cache\Adapter\TagAwareAdapter;
|
||||
|
||||
class FilesystemTagAwareAdapter extends TagAwareAdapter
|
||||
{
|
||||
public function __construct(?string $prefix, int $lifetime = 0, ?string $directory = null)
|
||||
{
|
||||
$prefix = 'app_cache_'.$prefix;
|
||||
|
||||
parent::__construct(
|
||||
new \Symfony\Component\Cache\Adapter\FilesystemAdapter($prefix, $lifetime, $directory),
|
||||
new \Symfony\Component\Cache\Adapter\FilesystemAdapter($prefix.'_tags')
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mautic\CacheBundle\Cache\Adapter;
|
||||
|
||||
use Mautic\CacheBundle\Exceptions\InvalidArgumentException;
|
||||
use Symfony\Component\Cache\Adapter\MemcachedAdapter;
|
||||
use Symfony\Component\Cache\Adapter\TagAwareAdapter;
|
||||
|
||||
class MemcachedTagAwareAdapter extends TagAwareAdapter
|
||||
{
|
||||
public function __construct(array $servers, string $namespace, int $lifetime)
|
||||
{
|
||||
if (!isset($servers['servers'])) {
|
||||
throw new InvalidArgumentException('Invalid memcached configuration. No servers specified.');
|
||||
}
|
||||
|
||||
$options = array_key_exists('options', $servers) ? $servers['options'] : [];
|
||||
$client = MemcachedAdapter::createConnection($servers['servers'], $options);
|
||||
|
||||
parent::__construct(
|
||||
new MemcachedAdapter($client, $namespace, $lifetime),
|
||||
new MemcachedAdapter($client, $namespace, $lifetime)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mautic\CacheBundle\Cache\Adapter;
|
||||
|
||||
use Symfony\Component\Cache\Adapter\RedisAdapter as SymfonyRedisAdapter;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
|
||||
class RedisAdapter extends SymfonyRedisAdapter
|
||||
{
|
||||
use RedisAdapterTrait;
|
||||
|
||||
/**
|
||||
* @param mixed[] $servers
|
||||
*/
|
||||
public function __construct(
|
||||
#[Autowire(env: 'json:MAUTIC_CACHE_ADAPTER_REDIS')]
|
||||
array $servers,
|
||||
|
||||
#[Autowire(env: 'string:MAUTIC_CACHE_PREFIX')]
|
||||
string $namespace,
|
||||
|
||||
#[Autowire(env: 'int:MAUTIC_CACHE_LIFETIME')]
|
||||
int $lifetime,
|
||||
|
||||
#[Autowire(env: 'bool:MAUTIC_REDIS_PRIMARY_ONLY')]
|
||||
bool $primaryOnly)
|
||||
{
|
||||
parent::__construct($this->createClient($servers, $primaryOnly), $namespace, $lifetime);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mautic\CacheBundle\Cache\Adapter;
|
||||
|
||||
use Mautic\CacheBundle\Exceptions\InvalidArgumentException;
|
||||
use Mautic\CoreBundle\Helper\PRedisConnectionHelper;
|
||||
use Predis\Client;
|
||||
|
||||
trait RedisAdapterTrait
|
||||
{
|
||||
/**
|
||||
* @param mixed[] $servers
|
||||
*/
|
||||
private function createClient(array $servers, bool $primaryOnly): Client
|
||||
{
|
||||
if (!isset($servers['dsn'])) {
|
||||
throw new InvalidArgumentException('Invalid redis configuration. No server specified.');
|
||||
}
|
||||
|
||||
$options = array_key_exists('options', $servers) ? $servers['options'] : [];
|
||||
$options['primaryOnly'] = $primaryOnly;
|
||||
|
||||
return PRedisConnectionHelper::createClient(PRedisConnectionHelper::getRedisEndpoints($servers['dsn']), $options);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mautic\CacheBundle\Cache\Adapter;
|
||||
|
||||
use Symfony\Component\Cache\Adapter\RedisAdapter;
|
||||
use Symfony\Component\Cache\Adapter\TagAwareAdapter;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
|
||||
class RedisTagAwareAdapter extends TagAwareAdapter
|
||||
{
|
||||
use RedisAdapterTrait;
|
||||
|
||||
/**
|
||||
* @param mixed[] $servers
|
||||
*/
|
||||
public function __construct(
|
||||
#[Autowire(env: 'json:MAUTIC_CACHE_ADAPTER_REDIS')]
|
||||
array $servers,
|
||||
|
||||
#[Autowire(env: 'string:MAUTIC_CACHE_PREFIX')]
|
||||
string $namespace,
|
||||
|
||||
#[Autowire(env: 'int:MAUTIC_CACHE_LIFETIME')]
|
||||
int $lifetime,
|
||||
|
||||
#[Autowire(env: 'bool:MAUTIC_REDIS_PRIMARY_ONLY')]
|
||||
bool $primaryOnly)
|
||||
{
|
||||
$client = $this->createClient($servers, $primaryOnly);
|
||||
|
||||
parent::__construct(
|
||||
new RedisAdapter($client, $namespace, $lifetime),
|
||||
new RedisAdapter($client, $namespace.'.tags.', $lifetime)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mautic\CacheBundle\Cache;
|
||||
|
||||
use Symfony\Component\Cache\Adapter\AdapterInterface;
|
||||
|
||||
final class CacheProvider extends AbstractCacheProvider
|
||||
{
|
||||
public function getCacheAdapter(): AdapterInterface
|
||||
{
|
||||
return $this->cacheAdapterFactory('cache_adapter');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mautic\CacheBundle\Cache;
|
||||
|
||||
use Symfony\Component\Cache\Adapter\AdapterInterface;
|
||||
use Symfony\Component\Cache\Psr16Cache;
|
||||
|
||||
interface CacheProviderInterface extends AdapterInterface
|
||||
{
|
||||
/**
|
||||
* @return Psr16Cache
|
||||
*/
|
||||
public function getSimpleCache();
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mautic\CacheBundle\Cache;
|
||||
|
||||
use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
|
||||
|
||||
final class CacheProviderTagAware extends AbstractCacheProvider implements CacheProviderTagAwareInterface
|
||||
{
|
||||
public function getCacheAdapter(): TagAwareAdapterInterface
|
||||
{
|
||||
$adapter = $this->cacheAdapterFactory('cache_adapter_tag_aware');
|
||||
\assert($adapter instanceof TagAwareAdapterInterface);
|
||||
|
||||
return $adapter;
|
||||
}
|
||||
|
||||
public function invalidateTags(array $tags): bool
|
||||
{
|
||||
return $this->getCacheAdapter()->invalidateTags($tags);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mautic\CacheBundle\Cache;
|
||||
|
||||
use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
|
||||
|
||||
interface CacheProviderTagAwareInterface extends CacheProviderInterface, TagAwareAdapterInterface
|
||||
{
|
||||
}
|
||||
Reference in New Issue
Block a user