mirror of
https://github.com/shlinkio/shlink.git
synced 2025-03-29 21:12:36 +03:00
Improved CacheFactory class
This commit is contained in:
parent
258f12f684
commit
a562bc661d
2 changed files with 42 additions and 58 deletions
|
@ -13,6 +13,7 @@ use Zend\ServiceManager\Exception\ServiceNotFoundException;
|
||||||
use Zend\ServiceManager\Factory\FactoryInterface;
|
use Zend\ServiceManager\Factory\FactoryInterface;
|
||||||
use function Functional\contains;
|
use function Functional\contains;
|
||||||
use function Shlinkio\Shlink\Common\env;
|
use function Shlinkio\Shlink\Common\env;
|
||||||
|
use function sys_get_temp_dir;
|
||||||
|
|
||||||
class CacheFactory implements FactoryInterface
|
class CacheFactory implements FactoryInterface
|
||||||
{
|
{
|
||||||
|
@ -23,6 +24,7 @@ class CacheFactory implements FactoryInterface
|
||||||
Cache\PhpFileCache::class,
|
Cache\PhpFileCache::class,
|
||||||
Cache\MemcachedCache::class,
|
Cache\MemcachedCache::class,
|
||||||
];
|
];
|
||||||
|
private const DEFAULT_MEMCACHED_PORT = 11211;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create an object
|
* Create an object
|
||||||
|
@ -40,7 +42,7 @@ class CacheFactory implements FactoryInterface
|
||||||
{
|
{
|
||||||
$appOptions = $container->get(AppOptions::class);
|
$appOptions = $container->get(AppOptions::class);
|
||||||
$adapter = $this->getAdapter($container);
|
$adapter = $this->getAdapter($container);
|
||||||
$adapter->setNamespace($appOptions->__toString());
|
$adapter->setNamespace((string) $appOptions);
|
||||||
|
|
||||||
return $adapter;
|
return $adapter;
|
||||||
}
|
}
|
||||||
|
@ -65,25 +67,35 @@ class CacheFactory implements FactoryInterface
|
||||||
return new $cacheConfig['adapter']();
|
return new $cacheConfig['adapter']();
|
||||||
case Cache\FilesystemCache::class:
|
case Cache\FilesystemCache::class:
|
||||||
case Cache\PhpFileCache::class:
|
case Cache\PhpFileCache::class:
|
||||||
return new $cacheConfig['adapter']($cacheConfig['options']['dir']);
|
return new $cacheConfig['adapter']($cacheConfig['options']['dir'] ?? sys_get_temp_dir());
|
||||||
case Cache\MemcachedCache::class:
|
case Cache\MemcachedCache::class:
|
||||||
$memcached = new Memcached();
|
|
||||||
$servers = $cacheConfig['options']['servers'] ?? [];
|
|
||||||
|
|
||||||
foreach ($servers as $server) {
|
|
||||||
if (! isset($server['host'])) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$port = isset($server['port']) ? (int) $server['port'] : 11211;
|
|
||||||
|
|
||||||
$memcached->addServer($server['host'], $port);
|
|
||||||
}
|
|
||||||
|
|
||||||
$cache = new Cache\MemcachedCache();
|
$cache = new Cache\MemcachedCache();
|
||||||
$cache->setMemcached($memcached);
|
$cache->setMemcached($this->buildMemcached($cacheConfig));
|
||||||
return $cache;
|
return $cache;
|
||||||
default:
|
default:
|
||||||
return new Cache\ArrayCache();
|
return new Cache\ArrayCache();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function buildMemcached(array $cacheConfig): Memcached
|
||||||
|
{
|
||||||
|
$memcached = new Memcached();
|
||||||
|
$servers = $cacheConfig['options']['servers'] ?? [];
|
||||||
|
|
||||||
|
foreach ($servers as $server) {
|
||||||
|
$this->addMemcachedServer($memcached, $server);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $memcached;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function addMemcachedServer(Memcached $memcached, array $server): void
|
||||||
|
{
|
||||||
|
if (! isset($server['host'])) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$port = (int) ($server['port'] ?? self::DEFAULT_MEMCACHED_PORT);
|
||||||
|
|
||||||
|
$memcached->addServer($server['host'], $port);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,69 +15,48 @@ class AppOptions extends AbstractOptions
|
||||||
private $name = '';
|
private $name = '';
|
||||||
/** @var string */
|
/** @var string */
|
||||||
private $version = '1.0';
|
private $version = '1.0';
|
||||||
/** @var string */
|
/**
|
||||||
|
* @var string
|
||||||
|
* @deprecated
|
||||||
|
*/
|
||||||
private $secretKey = '';
|
private $secretKey = '';
|
||||||
/** @var string|null */
|
/** @var string|null */
|
||||||
private $disableTrackParam;
|
private $disableTrackParam;
|
||||||
|
|
||||||
/**
|
public function getName(): string
|
||||||
* AppOptions constructor.
|
|
||||||
* @param array|null|\Traversable $options
|
|
||||||
*/
|
|
||||||
public function __construct($options = null)
|
|
||||||
{
|
|
||||||
parent::__construct($options);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getName()
|
|
||||||
{
|
{
|
||||||
return $this->name;
|
return $this->name;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
protected function setName(string $name): self
|
||||||
* @param string $name
|
|
||||||
* @return $this
|
|
||||||
*/
|
|
||||||
protected function setName($name)
|
|
||||||
{
|
{
|
||||||
$this->name = $name;
|
$this->name = $name;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function getVersion(): string
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getVersion()
|
|
||||||
{
|
{
|
||||||
return $this->version;
|
return $this->version;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
protected function setVersion(string $version): self
|
||||||
* @param string $version
|
|
||||||
* @return $this
|
|
||||||
*/
|
|
||||||
protected function setVersion($version)
|
|
||||||
{
|
{
|
||||||
$this->version = $version;
|
$this->version = $version;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mixed
|
* @deprecated
|
||||||
*/
|
*/
|
||||||
public function getSecretKey()
|
public function getSecretKey(): string
|
||||||
{
|
{
|
||||||
return $this->secretKey;
|
return $this->secretKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $secretKey
|
* @deprecated
|
||||||
* @return $this
|
|
||||||
*/
|
*/
|
||||||
protected function setSecretKey($secretKey)
|
protected function setSecretKey(string $secretKey): self
|
||||||
{
|
{
|
||||||
$this->secretKey = $secretKey;
|
$this->secretKey = $secretKey;
|
||||||
return $this;
|
return $this;
|
||||||
|
@ -86,25 +65,18 @@ class AppOptions extends AbstractOptions
|
||||||
/**
|
/**
|
||||||
* @return string|null
|
* @return string|null
|
||||||
*/
|
*/
|
||||||
public function getDisableTrackParam()
|
public function getDisableTrackParam(): ?string
|
||||||
{
|
{
|
||||||
return $this->disableTrackParam;
|
return $this->disableTrackParam;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
protected function setDisableTrackParam(?string $disableTrackParam): self
|
||||||
* @param string|null $disableTrackParam
|
|
||||||
* @return $this|self
|
|
||||||
*/
|
|
||||||
protected function setDisableTrackParam($disableTrackParam): self
|
|
||||||
{
|
{
|
||||||
$this->disableTrackParam = $disableTrackParam;
|
$this->disableTrackParam = $disableTrackParam;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function __toString(): string
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function __toString()
|
|
||||||
{
|
{
|
||||||
return sprintf('%s:v%s', $this->name, $this->version);
|
return sprintf('%s:v%s', $this->name, $this->version);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue