Skip to content

Commit

Permalink
Improved caching performance and issues
Browse files Browse the repository at this point in the history
  • Loading branch information
divineniiquaye committed Aug 21, 2020
1 parent d0d245c commit 26c0ce5
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 437 deletions.
32 changes: 20 additions & 12 deletions src/AdapterFactory.php
Expand Up @@ -17,7 +17,7 @@

namespace Biurad\Cache;

use Biurad\Cache\Exceptions\InvalidArgumentException;
use Biurad\Cache\Exceptions\CacheException;
use Doctrine\Common\Cache as DoctrineCache;
use Memcache;
use Memcached;
Expand All @@ -26,6 +26,8 @@
use TypeError;

/**
* @codeCoverageIgnore
*
* @author Divine Niiquaye Ibok <divineibok@gmail.com>
*/
class AdapterFactory
Expand Down Expand Up @@ -82,28 +84,34 @@ public static function createHandler($connection): DoctrineCache\Cache
return new DoctrineCache\ZendDataCache();

case self::isPrefixedAdapter($connection, 'redis://'):
$adapter = new DoctrineCache\RedisCache();

$adapter = new DoctrineCache\RedisCache();
[$host, $port] = self::getPrefixedAdapter($connection, 8);
($redis = new Redis())->connect($host, (int) $port);

$redis = new Redis();
$redis->connect($host, (int) $port);

$adapter->setRedis($redis);

return $adapter;

case self::isPrefixedAdapter($connection, 'memcache://'):
$adapter = new DoctrineCache\MemcacheCache();

$adapter = new DoctrineCache\MemcacheCache();
[$host, $port] = self::getPrefixedAdapter($connection, 11);
($memcache = new Memcache())->addServer($host, (int) $port);

$memcache = new Memcache();
$memcache->addServer($host, (int) $port);

$adapter->setMemcache($memcache);

return $adapter;

case self::isPrefixedAdapter($connection, 'memcached://'):
$adapter = new DoctrineCache\MemcachedCache();

$adapter = new DoctrineCache\MemcachedCache();
[$host, $port] = self::getPrefixedAdapter($connection, 12);
($memcached = new Memcached())->addServer($host, (int) $port);

$memcached = new Memcached();
$memcached->addServer($host, (int) $port);

$adapter->setMemcached($memcached);

return $adapter;
Expand All @@ -124,7 +132,7 @@ public static function createHandler($connection): DoctrineCache\Cache
return new DoctrineCache\SQLite3Cache(new SQLite3($filename), $table);
}

throw new InvalidArgumentException(
throw new CacheException(
\sprintf('Unsupported Cache Adapter: %s.', \is_object($connection) ? \get_class($connection) : $connection)
);
}
Expand All @@ -142,7 +150,7 @@ private static function getPrefixedAdapter($connection, int $limit, bool $host =
return \explode(':', \substr((string) $connection, $limit));
}

if (\strpos(':', $tempDir = \substr((string) $connection, $limit))) {
if (false !== \strpos(':', $tempDir = \substr((string) $connection, $limit))) {
return \explode(':', $tempDir);
}

Expand Down
56 changes: 33 additions & 23 deletions src/CacheItem.php
Expand Up @@ -31,19 +31,19 @@ final class CacheItem implements CacheItemInterface
public const RESERVED_CHARACTERS = '{}()/\@:';

/** @var string */
protected $key;
private $key;

/** @var mixed */
protected $value;
private $value;

/** @var bool */
protected $isHit = false;
private $isHit = false;

/** @var float|int|null */
protected $expiry;
/** @var null|float|int */
private $expiry;

/** @var int */
protected $defaultLifetime;
private $defaultLifetime;

/**
* {@inheritdoc}
Expand Down Expand Up @@ -71,10 +71,8 @@ public function isHit(): bool

/**
* {@inheritdoc}
*
* @return $this
*/
public function set($value): self
public function set($value)
{
$this->value = $value;

Expand All @@ -83,33 +81,34 @@ public function set($value): self

/**
* {@inheritdoc}
*
* @return $this
*/
public function expiresAt($expiration): self
public function expiresAt($expiration)
{
if (null === $expiration) {
$this->expiry = $this->defaultLifetime > 0 ? \microtime(true) + $this->defaultLifetime : null;
} elseif ($expiration instanceof DateTimeInterface) {
$this->expiry = (float) $expiration->format('U.u');
} else {
return $this->setDefaultExpiration();
}

if (!$expiration instanceof DateTimeInterface) {
throw new InvalidArgumentException('Expiration date must implement DateTimeInterface or be null.');
}

$this->expiry = (float) $expiration->format('U.u');

return $this;
}

/**
* {@inheritdoc}
*
* @return $this
*/
public function expiresAfter($time): self
public function expiresAfter($time)
{
if (null === $time) {
$this->expiry = $this->defaultLifetime > 0 ? \microtime(true) + $this->defaultLifetime : null;
} elseif ($time instanceof DateInterval) {
$this->expiry = \microtime(true) + (int) DateTime::createFromFormat('U', '0')->add($time)->format('U.u');
return $this->setDefaultExpiration();
}

if ($time instanceof DateInterval) {
$interval = DateTime::createFromFormat('U', '0')->add($time);
$this->expiry = \microtime(true) + (int) $interval->format('U.u');
} elseif (\is_int($time)) {
$this->expiry = $time + \microtime(true);
} else {
Expand All @@ -124,8 +123,9 @@ public function expiresAfter($time): self
*
* @param string $key The key to validate
*
* @return string
* @throws InvalidArgumentException When $key is not valid
*
* @return string
*/
public static function validateKey($key): string
{
Expand All @@ -145,4 +145,14 @@ public static function validateKey($key): string

return $key;
}

/**
* @return static
*/
private function setDefaultExpiration(): self
{
$this->expiry = $this->defaultLifetime > 0 ? \microtime(true) + $this->defaultLifetime : null;

return $this;
}
}

0 comments on commit 26c0ce5

Please sign in to comment.