Actualización

This commit is contained in:
Xes
2025-04-10 12:24:57 +02:00
parent 8969cc929d
commit 45420b6f0d
39760 changed files with 4303286 additions and 0 deletions

View File

@@ -0,0 +1,77 @@
<?php
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\Cache\Adapter\Counter;
use Sonata\Cache\Counter;
/**
* Handles APC cache.
*/
class ApcCounter extends BaseCounter
{
/**
* @var string
*/
protected $prefix;
/**
* Constructor.
*
* @param string $prefix A prefix to avoid clash between instances
*/
public function __construct(string $prefix)
{
$this->prefix = $prefix;
}
/**
* {@inheritdoc}
*/
public function increment(Counter $counter, int $number = 1): Counter
{
$counter = $this->transform($counter);
$value = apc_inc($this->prefix.'/'.$counter->getName(), $number);
return $this->handleIncrement($value, $counter, $number);
}
/**
* {@inheritdoc}
*/
public function decrement(Counter $counter, int $number = 1): Counter
{
$counter = $this->transform($counter);
$value = apc_dec($this->prefix.'/'.$counter->getName(), $number);
return $this->handleDecrement($value, $counter, $number);
}
/**
* {@inheritdoc}
*/
public function set(Counter $counter): Counter
{
apc_store($this->prefix.'/'.$counter->getName(), $counter->getValue());
return $counter;
}
/**
* {@inheritdoc}
*/
public function get(string $name): Counter
{
return Counter::create($name, (int) apc_fetch($this->prefix.'/'.$name));
}
}

View File

@@ -0,0 +1,68 @@
<?php
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\Cache\Adapter\Counter;
use Sonata\Cache\Counter;
use Sonata\Cache\CounterAdapterInterface;
abstract class BaseCounter implements CounterAdapterInterface
{
/**
* @param $counter
*
* @return Counter
*/
protected function transform($counter): Counter
{
if ($counter instanceof Counter) {
return $counter;
}
return Counter::create($counter);
}
/**
* @param mixed $value
* @param Counter $counter
* @param int $number
*
* @return Counter
*/
protected function handleIncrement($value, Counter $counter, int $number): Counter
{
if (false === $value) {
$counter = $this->set(Counter::create($counter->getName(), $counter->getValue() + $number));
} else {
$counter = Counter::create($counter->getName(), $value);
}
return $counter;
}
/**
* @param mixed $value
* @param Counter $counter
* @param int $number
*
* @return Counter
*/
protected function handleDecrement($value, Counter $counter, int $number): Counter
{
if (false === $value) {
$counter = $this->set(Counter::create($counter->getName(), $counter->getValue() + (-1 * $number)));
} else {
$counter = Counter::create($counter->getName(), $value);
}
return $counter;
}
}

View File

@@ -0,0 +1,91 @@
<?php
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\Cache\Adapter\Counter;
use Sonata\Cache\Counter;
class MemcachedCounter extends BaseCounter
{
protected $servers;
protected $prefix;
protected $collection;
/**
* @param $prefix
* @param array $servers
*/
public function __construct(string $prefix, array $servers)
{
$this->prefix = $prefix;
$this->servers = $servers;
}
/**
* {@inheritdoc}
*/
public function increment(Counter $counter, int $number = 1): Counter
{
$counter = $this->transform($counter);
$value = $this->getCollection()->increment($this->prefix.'.'.$counter->getName(), $number);
return $this->handleIncrement(0 !== $this->getCollection()->getResultCode() ? false : $value, $counter, $number);
}
/**
* {@inheritdoc}
*/
public function decrement(Counter $counter, int $number = 1): Counter
{
$counter = $this->transform($counter);
$value = $this->getCollection()->decrement($this->prefix.'.'.$counter->getName(), $number);
return $this->handleDecrement(0 !== $this->getCollection()->getResultCode() ? false : $value, $counter, $number);
}
/**
* {@inheritdoc}
*/
public function set(Counter $counter): Counter
{
$this->getCollection()->add($this->prefix.'.'.$counter->getName(), $counter->getValue());
return $counter;
}
/**
* {@inheritdoc}
*/
public function get(string $name): Counter
{
return Counter::create($name, (int) $this->getCollection()->get($this->prefix.'.'.$name));
}
/**
* {@inheritdoc}
*/
private function getCollection(): \Memcached
{
if (!$this->collection) {
$this->collection = new \Memcached();
foreach ($this->servers as $server) {
$this->collection->addServer($server['host'], $server['port'], $server['weight']);
}
}
return $this->collection;
}
}

View File

@@ -0,0 +1,114 @@
<?php
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\Cache\Adapter\Counter;
use Sonata\Cache\Adapter\Cache\MongoCache;
use Sonata\Cache\Counter;
class MongoCounter extends BaseCounter
{
protected $collection;
private $servers;
private $databaseName;
private $collectionName;
/**
* @param array $servers
* @param $database
* @param $collection
*/
public function __construct(array $servers, string $database, string $collection)
{
$this->servers = $servers;
$this->databaseName = $database;
$this->collectionName = $collection;
}
/**
* {@inheritdoc}
*/
public function increment(Counter $counter, int $number = 1): Counter
{
$counter = $this->transform($counter);
$result = $this->getCollection()->findAndModify(
['counter' => $counter->getName()],
['$inc' => ['value' => $number]],
[],
['new' => true]
);
return $this->handleIncrement(0 === count($result) ? false : $result['value'], $counter, $number);
}
/**
* {@inheritdoc}
*/
public function decrement(Counter $counter, int $number = 1): Counter
{
$counter = $this->transform($counter);
$result = $this->getCollection()->findAndModify(
['counter' => $counter->getName()],
['$inc' => ['value' => -1 * $number]],
[],
['new' => true]
);
return $this->handleDecrement(0 === count($result) ? false : $result['value'], $counter, $number);
}
/**
* {@inheritdoc}
*/
public function set(Counter $counter): Counter
{
$result = $this->getCollection()->findAndModify(
['counter' => $counter->getName()],
['$setOnInsert' => ['value' => $counter->getValue()]],
[],
['upsert' => true, 'new' => true]
);
return Counter::create($counter->getName(), $result['value']);
}
/**
* {@inheritdoc}
*/
public function get(string $name): Counter
{
$result = $this->getCollection()->findOne(['counter' => $name]);
return Counter::create($name, $result ? (int) $result['value'] : 0);
}
/**
* @return \MongoCollection
*/
private function getCollection(): \MongoCollection
{
if (!$this->collection) {
$class = MongoCache::getMongoClass();
$mongo = new $class(sprintf('mongodb://%s', implode(',', $this->servers)));
$this->collection = $mongo
->selectDB($this->databaseName)
->selectCollection($this->collectionName);
}
return $this->collection;
}
}

View File

@@ -0,0 +1,96 @@
<?php
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\Cache\Adapter\Counter;
use Predis\Client;
use Sonata\Cache\Counter;
class PRedisCounter extends BaseCounter
{
protected $options;
protected $parameters;
protected $client;
/**
* @param array $parameters
* @param array $options
*/
public function __construct(array $parameters = [], array $options = [])
{
$this->parameters = $parameters;
$this->options = $options;
}
/**
* {@inheritdoc}
*/
public function increment(Counter $counter, int $number = 1): Counter
{
$counter = $this->transform($counter);
if (null === $this->getClient()->get($counter->getName())) {
$this->getClient()->set($counter->getName(), $value = $counter->getValue() + $number);
} else {
$value = $this->getClient()->incrby($counter->getName(), $number);
}
return Counter::create($counter->getName(), $value);
}
/**
* {@inheritdoc}
*/
public function decrement(Counter $counter, int $number = 1): Counter
{
$counter = $this->transform($counter);
if (null === $this->getClient()->get($counter->getName())) {
$this->getClient()->set($counter->getName(), $value = $counter->getValue() - $number);
} else {
$value = $this->getClient()->decrby($counter->getName(), $number);
}
return Counter::create($counter->getName(), $value);
}
/**
* {@inheritdoc}
*/
public function set(Counter $counter): Counter
{
$this->getClient()->set($counter->getName(), $counter->getValue());
return $counter;
}
/**
* {@inheritdoc}
*/
public function get(string $name): Counter
{
return Counter::create($name, (int) $this->getClient()->get($name));
}
/**
* @return Client
*/
private function getClient(): Client
{
if (!$this->client) {
$this->client = new Client($this->parameters, $this->options);
}
return $this->client;
}
}