Skip to content

Commit

Permalink
Updated serialization logic
Browse files Browse the repository at this point in the history
  • Loading branch information
sunspikes committed Nov 7, 2017
1 parent fcf7df7 commit f780b9b
Show file tree
Hide file tree
Showing 14 changed files with 146 additions and 3,455 deletions.
3,294 changes: 0 additions & 3,294 deletions build/logs/clover.xml

This file was deleted.

38 changes: 38 additions & 0 deletions src/Cache/AbstractCacheItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Sunspikes\Ratelimit\Cache;

abstract class AbstractCacheItem
{
/**
* @param string $serialized
* @return ThrottlerItemInterface
*/
public function unserialize($serialized)
{
$array = json_decode($serialized, true);
$this->fromArray($array);
}

/**
* @return array
*/
public function serialize()
{
return json_encode($this->toArray());
}

/**
* Wake up call to build the cache item object from array representation
*
* @param array $array
*/
abstract protected function fromArray(array $array);

/**
* Get the array representation of the object
*
* @return array
*/
abstract protected function toArray(): array;
}
92 changes: 29 additions & 63 deletions src/Cache/ThrottlerCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,77 +48,39 @@ public function __construct(CacheItemPoolInterface $cacheItemPool)
}

/**
* @param string $key
*
* @return ThrottlerItemInterface
* @inheritdoc
*/
public function getItem(string $key): ThrottlerItemInterface
{
$item = $this->getThrottlerItem($key);

return $item;
}

/**
* @param string $key
*
* @return bool
*/
public function hasItem(string $key): bool
{
return $this->cacheItemPool->hasItem($key);
}
try {
$item = $this->cacheItemPool->getItem($key);

/**
* @param string $key
* @param mixed $item
* @return bool
*/
public function setItem(string $key, ThrottlerItemInterface $item): bool
{
return $this->setThrottlerItem($key, $item);
}
if ($item->isHit()) {
return unserialize($item->get());
}
} catch (PsrCacheException $e) {
throw new CacheAdapterException($e->getMessage(), $e->getCode(), $e->getPrevious());
}

/**
* @param string $key
*/
public function removeItem(string $key)
{
$this->cacheItemPool->deleteItem($key);
throw new ItemNotFoundException('Item not found.');
}

/**
* @param string $key
*
* @return ThrottlerItemInterface
* @throws CacheAdapterException
* @throws ItemNotFoundException
* @inheritdoc
*/
private function getThrottlerItem(string $key): ThrottlerItemInterface
public function hasItem(string $key): bool
{
try {
$item = $this->cacheItemPool->getItem($key);

if ($item->isHit()) {
$params = json_decode($item->get(), true);
$class = $params['class'];

return $class::createFromArray($params['data']);
}
return $this->cacheItemPool->hasItem($key);
} catch (PsrCacheException $e) {
throw new CacheAdapterException($e->getMessage(), $e->getCode(), $e->getPrevious());
}

throw new ItemNotFoundException('Item not found.');
}

/**
* @param string $key
* @param ThrottlerItemInterface $item
*
* @return bool
* @throws CacheAdapterException
* @inheritdoc
*/
private function setThrottlerItem(string $key, ThrottlerItemInterface $item)
public function setItem(string $key, ThrottlerItemInterface $item): bool
{
try {
$cacheItem = $this->cacheItemPool->getItem($key);
Expand All @@ -130,17 +92,21 @@ private function setThrottlerItem(string $key, ThrottlerItemInterface $item)
throw new CacheAdapterException($e->getMessage(), $e->getCode(), $e->getPrevious());
}

$cacheItem->set(
json_encode(
[
'class' => get_class($item),
'data' => $item,
]
)
);

$cacheItem->set(serialize($item));
$cacheItem->expiresAfter($item->getTtl());

return $this->cacheItemPool->save($cacheItem);
}

/**
* @inheritdoc
*/
public function removeItem(string $key)
{
try {
$this->cacheItemPool->deleteItem($key);
} catch (PsrCacheException $e) {
throw new CacheAdapterException($e->getMessage(), $e->getCode(), $e->getPrevious());
}
}
}
8 changes: 8 additions & 0 deletions src/Cache/ThrottlerCacheInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,39 @@

namespace Sunspikes\Ratelimit\Cache;

use Sunspikes\Ratelimit\Cache\Exception\CacheAdapterException;
use Sunspikes\Ratelimit\Cache\Exception\ItemNotFoundException;

interface ThrottlerCacheInterface
{
/**
* @param string $key
*
* @return ThrottlerItemInterface
* @throws CacheAdapterException
* @throws ItemNotFoundException
*/
public function getItem(string $key): ThrottlerItemInterface;

/**
* @param string $key
* @param ThrottlerItemInterface $item
* @return bool
* @throws CacheAdapterException
*/
public function setItem(string $key, ThrottlerItemInterface $item): bool;

/**
* @param string $key
*
* @return bool
* @throws CacheAdapterException
*/
public function hasItem(string $key): bool;

/**
* @param string $key
* @throws CacheAdapterException
*/
public function removeItem(string $key);
}
9 changes: 1 addition & 8 deletions src/Cache/ThrottlerItemInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,8 @@

namespace Sunspikes\Ratelimit\Cache;

interface ThrottlerItemInterface extends \JsonSerializable
interface ThrottlerItemInterface extends \Serializable
{
/**
* @param array $array
*
* @return ThrottlerItemInterface
*/
public static function createFromArray(array $array): ThrottlerItemInterface;

/**
* @return int|null
*/
Expand Down
41 changes: 19 additions & 22 deletions src/Throttle/Entity/CacheCount.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

namespace Sunspikes\Ratelimit\Throttle\Entity;

use Sunspikes\Ratelimit\Cache\AbstractCacheItem;
use Sunspikes\Ratelimit\Cache\ThrottlerItemInterface;

class CacheCount implements ThrottlerItemInterface
class CacheCount extends AbstractCacheItem implements ThrottlerItemInterface
{
/** @var int $count */
private $count;

/** @var int $ttl */
/** @var int|null $ttl */
private $ttl;

/**
Expand All @@ -23,42 +24,38 @@ public function __construct(int $count, int $ttl = null)
}

/**
* @param array $array
*
* @return ThrottlerItemInterface
* @return int
*/
public static function createFromArray(array $array): ThrottlerItemInterface
public function getCount(): int
{
return new static(
$array['count'],
$array['ttl']
);
return $this->count;
}

/**
* @return array
* @inheritdoc
*/
public function jsonSerialize()
public function getTtl()
{
return [
'count' => $this->count,
'ttl' => $this->ttl,
];
return $this->ttl;
}

/**
* @return int
* @inheritdoc
*/
public function getCount(): int
protected function fromArray(array $array)
{
return $this->count;
$this->count = $array['count'];
$this->ttl = $array['ttl'];
}

/**
* @return int|null
* @inheritdoc
*/
public function getTtl()
protected function toArray(): array
{
return $this->ttl;
return [
'count' => $this->count,
'ttl' => $this->ttl,
];
}
}
41 changes: 19 additions & 22 deletions src/Throttle/Entity/CacheHitMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
namespace Sunspikes\Ratelimit\Throttle\Entity;

use Sunspikes\Ratelimit\Cache\ThrottlerItemInterface;
use Sunspikes\Ratelimit\Cache\AbstractCacheItem;

class CacheHitMapping implements ThrottlerItemInterface
class CacheHitMapping extends AbstractCacheItem implements ThrottlerItemInterface
{
/** @var array $hitMapping */
private $hitMapping;

/** @var int $ttl */
/** @var int|null $ttl */
private $ttl;

/**
Expand All @@ -23,42 +24,38 @@ public function __construct(array $hitMapping, int $ttl = null)
}

/**
* @param array $array
*
* @return ThrottlerItemInterface
* @return array
*/
public static function createFromArray(array $array): ThrottlerItemInterface
public function getHitMapping(): array
{
return new static(
$array['hitMapping'],
$array['ttl']
);
return $this->hitMapping;
}

/**
* @return array
* @inheritdoc
*/
public function jsonSerialize()
public function getTtl()
{
return [
'hitMapping' => $this->hitMapping,
'ttl' => $this->ttl,
];
return $this->ttl;
}

/**
* @return array
* @inheritdoc
*/
public function getHitMapping(): array
protected function fromArray(array $array)
{
return $this->hitMapping;
$this->hitMapping = $array['hitMapping'];
$this->ttl = $array['ttl'];
}

/**
* @return int|null
* @inheritdoc
*/
public function getTtl()
protected function toArray(): array
{
return $this->ttl;
return [
'hitMapping' => $this->hitMapping,
'ttl' => $this->ttl,
];
}
}
Loading

0 comments on commit f780b9b

Please sign in to comment.