initial commit; version 22.5.12042

This commit is contained in:
2022-12-12 23:28:25 -05:00
commit af1b03d79f
17653 changed files with 22692970 additions and 0 deletions

View File

@ -0,0 +1,164 @@
<?php
/**
*
* This file is part of phpFastCache.
*
* @license MIT License (MIT)
*
* For full copyright and license information, please see the docs/CREDITS.txt file.
*
* @author Khoa Bui (khoaofgod) <khoaofgod@gmail.com> https://www.phpfastcache.com
* @author Georges.L (Geolim4) <contact@geolim4.com>
*
*/
declare(strict_types=1);
namespace Phpfastcache\Drivers\Memcache;
use Phpfastcache\Config\ConfigurationOption;
use Phpfastcache\Exceptions\PhpfastcacheInvalidConfigurationException;
class Config extends ConfigurationOption
{
/**
* @var array
*
* Multiple server can be added this way:
* $cfg->setServers([
* [
* 'host' => '127.0.0.1',
* 'port' => 11211,
* 'saslUser' => false,
* 'saslPassword' => false,
* ]
* ]);
*/
protected $servers = [];
/**
* @var string
*/
protected $host = '127.0.0.1';
/**
* @var int
*/
protected $port = 11211;
/**
* @var string
*/
protected $saslUser = '';
/**
* @var string
*/
protected $saslPassword = '';
/**
* @return bool
*/
public function getSaslUser(): string
{
return $this->saslUser;
}
/**
* @param string $saslUser
* @return self
*/
public function setSaslUser(string $saslUser): self
{
$this->saslUser = $saslUser;
return $this;
}
/**
* @return string
*/
public function getSaslPassword(): string
{
return $this->saslPassword;
}
/**
* @param string $saslPassword
* @return self
*/
public function setSaslPassword(string $saslPassword): self
{
$this->saslPassword = $saslPassword;
return $this;
}
/**
* @return array
*/
public function getServers(): array
{
return $this->servers;
}
/**
* @param array $servers
* @return self
* @throws PhpfastcacheInvalidConfigurationException
*/
public function setServers(array $servers): self
{
foreach ($servers as $server) {
if ($diff = array_diff(['host', 'port', 'saslUser', 'saslPassword'], array_keys($server))) {
throw new PhpfastcacheInvalidConfigurationException('Missing keys for memcached server: ' . implode(', ', $diff));
}
if ($diff = array_diff(array_keys($server), ['host', 'port', 'saslUser', 'saslPassword'])) {
throw new PhpfastcacheInvalidConfigurationException('Unknown keys for memcached server: ' . implode(', ', $diff));
}
if (!is_string($server['host'])) {
throw new PhpfastcacheInvalidConfigurationException('Host must be a valid string in "$server" configuration array');
}
if (!is_int($server['port'])) {
throw new PhpfastcacheInvalidConfigurationException('Port must be a valid integer in "$server" configuration array');
}
}
$this->servers = $servers;
return $this;
}
/**
* @return string
*/
public function getHost(): string
{
return $this->host;
}
/**
* @param string $host
* @return self
*/
public function setHost(string $host): self
{
$this->host = $host;
return $this;
}
/**
* @return int
*/
public function getPort(): int
{
return $this->port;
}
/**
* @param int $port
* @return self
*/
public function setPort(int $port): self
{
$this->port = $port;
return $this;
}
}

View File

@ -0,0 +1,213 @@
<?php
/**
*
* This file is part of phpFastCache.
*
* @license MIT License (MIT)
*
* For full copyright and license information, please see the docs/CREDITS.txt file.
*
* @author Khoa Bui (khoaofgod) <khoaofgod@gmail.com> https://www.phpfastcache.com
* @author Georges.L (Geolim4) <contact@geolim4.com>
*
*/
declare(strict_types=1);
namespace Phpfastcache\Drivers\Memcache;
use DateTime;
use Exception;
use Memcache as MemcacheSoftware;
use Phpfastcache\Cluster\AggregatablePoolInterface;
use Phpfastcache\Config\ConfigurationOption;
use Phpfastcache\Core\Pool\{DriverBaseTrait, ExtendedCacheItemPoolInterface};
use Phpfastcache\Entities\DriverStatistic;
use Phpfastcache\Exceptions\{PhpfastcacheDriverException, PhpfastcacheInvalidArgumentException};
use Phpfastcache\Util\{MemcacheDriverCollisionDetectorTrait};
use Psr\Cache\CacheItemInterface;
/**
* Class Driver
* @package phpFastCache\Drivers
* @property MemcacheSoftware $instance
* @property Config $config Config object
* @method Config getConfig() Return the config object
*/
class Driver implements ExtendedCacheItemPoolInterface, AggregatablePoolInterface
{
use DriverBaseTrait {
__construct as protected __parentConstruct;
}
use MemcacheDriverCollisionDetectorTrait;
/**
* @var int
*/
protected $memcacheFlags = 0;
/**
* Driver constructor.
* @param ConfigurationOption $config
* @param string $instanceId
* @throws PhpfastcacheDriverException
*/
public function __construct(ConfigurationOption $config, string $instanceId)
{
self::checkCollision('Memcache');
$this->__parentConstruct($config, $instanceId);
}
/**
* @return bool
*/
public function driverCheck(): bool
{
return class_exists('Memcache');
}
/**
* @return DriverStatistic
*/
public function getStats(): DriverStatistic
{
$stats = (array)$this->instance->getstats();
$stats['uptime'] = (isset($stats['uptime']) ? $stats['uptime'] : 0);
$stats['version'] = (isset($stats['version']) ? $stats['version'] : 'UnknownVersion');
$stats['bytes'] = (isset($stats['bytes']) ? $stats['version'] : 0);
$date = (new DateTime())->setTimestamp(time() - $stats['uptime']);
return (new DriverStatistic())
->setData(implode(', ', array_keys($this->itemInstances)))
->setInfo(sprintf("The memcache daemon v%s is up since %s.\n For more information see RawData.", $stats['version'], $date->format(DATE_RFC2822)))
->setRawData($stats)
->setSize((int)$stats['bytes']);
}
/**
* @return bool
*/
protected function driverConnect(): bool
{
$this->instance = new MemcacheSoftware();
$servers = $this->getConfig()->getServers();
if (count($servers) < 1) {
$servers = [
[
'host' => $this->getConfig()->getHost(),
'path' => $this->getConfig()->getPath(),
'port' => $this->getConfig()->getPort(),
'saslUser' => $this->getConfig()->getSaslUser() ?: false,
'saslPassword' => $this->getConfig()->getSaslPassword() ?: false,
],
];
}
foreach ($servers as $server) {
try {
/**
* If path is provided we consider it as an UNIX Socket
*/
if (!empty($server['path']) && !$this->instance->addServer($server['path'], 0)) {
$this->fallback = true;
} else {
if (!empty($server['host']) && !$this->instance->addServer($server['host'], $server['port'])) {
$this->fallback = true;
}
}
if (!empty($server['saslUser']) && !empty($server['saslPassword'])) {
throw new PhpfastcacheDriverException('Unlike Memcached, Memcache does not support SASL authentication');
}
} catch (Exception $e) {
$this->fallback = true;
}
/**
* Since Memcached does not throw
* any error if not connected ...
*/
if (!$this->instance->getServerStatus(
!empty($server['path']) ? $server['path'] : $server['host'],
!empty($server['port']) ? $server['port'] : 0
)) {
throw new PhpfastcacheDriverException('Memcache seems to not be connected');
}
}
return true;
}
/**
* @param CacheItemInterface $item
* @return null|array
*/
protected function driverRead(CacheItemInterface $item)
{
$val = $this->instance->get($item->getKey());
if ($val === false) {
return null;
}
return $val;
}
/**
* @param CacheItemInterface $item
* @return mixed
* @throws PhpfastcacheInvalidArgumentException
*/
protected function driverWrite(CacheItemInterface $item): bool
{
/**
* Check for Cross-Driver type confusion
*/
if ($item instanceof Item) {
$ttl = $item->getExpirationDate()->getTimestamp() - time();
// Memcache will only allow a expiration timer less than 2592000 seconds,
// otherwise, it will assume you're giving it a UNIX timestamp.
if ($ttl > 2592000) {
$ttl = time() + $ttl;
}
return $this->instance->set($item->getKey(), $this->driverPreWrap($item), $this->memcacheFlags, $ttl);
}
throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected');
}
/**
* @param CacheItemInterface $item
* @return bool
* @throws PhpfastcacheInvalidArgumentException
*/
protected function driverDelete(CacheItemInterface $item): bool
{
/**
* Check for Cross-Driver type confusion
*/
if ($item instanceof Item) {
return $this->instance->delete($item->getKey());
}
throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected');
}
/********************
*
* PSR-6 Extended Methods
*
*******************/
/**
* @return bool
*/
protected function driverClear(): bool
{
return $this->instance->flush();
}
}

View File

@ -0,0 +1,60 @@
<?php
/**
*
* This file is part of phpFastCache.
*
* @license MIT License (MIT)
*
* For full copyright and license information, please see the docs/CREDITS.txt file.
*
* @author Khoa Bui (khoaofgod) <khoaofgod@gmail.com> https://www.phpfastcache.com
* @author Georges.L (Geolim4) <contact@geolim4.com>
*
*/
declare(strict_types=1);
namespace Phpfastcache\Drivers\Memcache;
use Phpfastcache\Core\Item\{ExtendedCacheItemInterface, ItemBaseTrait};
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
use Phpfastcache\Drivers\Memcache\Driver as MemcacheDriver;
use Phpfastcache\Exceptions\{PhpfastcacheInvalidArgumentException};
/**
* Class Item
* @package phpFastCache\Drivers\Memcache
*/
class Item implements ExtendedCacheItemInterface
{
use ItemBaseTrait {
ItemBaseTrait::__construct as __BaseConstruct;
}
/**
* Item constructor.
* @param Driver $driver
* @param $key
* @throws PhpfastcacheInvalidArgumentException
*/
public function __construct(MemcacheDriver $driver, $key)
{
$this->__BaseConstruct($driver, $key);
}
/**
* @param ExtendedCacheItemPoolInterface $driver
* @return static
* @throws PhpfastcacheInvalidArgumentException
*/
public function setDriver(ExtendedCacheItemPoolInterface $driver)
{
if ($driver instanceof MemcacheDriver) {
$this->driver = $driver;
return $this;
}
throw new PhpfastcacheInvalidArgumentException('Invalid driver instance');
}
}