Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding symfony cache #62

Merged
merged 9 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"php": ">=8.0",
"ext-json": "*",
"guzzlehttp/guzzle": "^6.3||^7.0",
"sabre/cache": "^1.0",
"symfony/cache": "^6.3",
"symfony/http-foundation": "^6.2"
},
"require-dev": {
Expand Down
33 changes: 23 additions & 10 deletions src/cache/DefaultCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace ipinfo\ipinfo\cache;

use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Cache\ItemInterface;

/**
* Default implementation of the CacheInterface. Provides in-memory caching.
*/
Expand All @@ -14,9 +18,9 @@ class DefaultCache implements CacheInterface
private $cache;
private $element_queue;

public function __construct(int $maxsize, int $ttl)
public function __construct(FilesystemAdapter $cache, int $maxsize, int $ttl)
Samuel-Gill marked this conversation as resolved.
Show resolved Hide resolved
{
$this->cache = new \Sabre\Cache\Memory();
$this->cache = $cache;
$this->element_queue = array();
$this->maxsize = $maxsize;
$this->ttl = $ttl;
Expand All @@ -29,21 +33,30 @@ public function __construct(int $maxsize, int $ttl)
*/
public function has(string $name)
{
return $this->cache->has($name);
return $this->cache->get($name);
}

public function delete(string $name): bool
Samuel-Gill marked this conversation as resolved.
Show resolved Hide resolved
{
return $this->$cache->delete($name);
}


Samuel-Gill marked this conversation as resolved.
Show resolved Hide resolved
/**
* Set the IP address key to the specified value.
* @param string $ip_address IP address to cache data for.
* @param mixed $value Data for specified IP address.
*/
public function set(string $name, $value)
{
if (!$this->cache->has($name)) {
$this->element_queue[] = $name;
}

$this->cache->set($name, $value, $this->ttl);
// The callable will only be executed on a cache miss.
$this->$cache->get($name, function (ItemInterface $item): string {
$item->expiresAfter($this->ttl);
// On chache miss update queue.
Samuel-Gill marked this conversation as resolved.
Show resolved Hide resolved
$this->$element_queue[] = $name;

return $value;
});

$this->manageSize();
}
Expand All @@ -53,7 +66,7 @@ public function set(string $name, $value)
* @param string $ip_address IP address to lookup in cache.
* @return mixed IP address data.
*/
public function get(string $name)
public function get(string $key, callable $callback, ?float $beta = null, ?array &$metadata = null): mixed
Samuel-Gill marked this conversation as resolved.
Show resolved Hide resolved
{
return $this->cache->get($name);
}
Expand All @@ -66,7 +79,7 @@ private function manageSize()
$overflow = count($this->element_queue) - $this->maxsize;
if ($overflow > 0) {
foreach (array_slice($this->element_queue, 0, $overflow) as $name) {
if ($this->cache->has($name)) {
if ($this->cache->get($name)) {
$this->cache->delete($name);
}
}
Expand Down
11 changes: 6 additions & 5 deletions tests/DefaultCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
namespace ipinfo\ipinfo\tests;

use ipinfo\ipinfo\cache\DefaultCache;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
Samuel-Gill marked this conversation as resolved.
Show resolved Hide resolved
use PHPUnit\Framework\TestCase;

class DefaultCacheTest extends TestCase
{
public function testHasValue()
{
$cache = new DefaultCache($maxsize = 4, $ttl = 2);
$cache = new DefaultCache(new FilesystemAdapter(), $maxsize = 4, $ttl = 2);
$key1 = 'test';
$value1 = 'obama';
$cache->set($key1, $value1);
Expand All @@ -24,15 +25,15 @@ public function testHasValue()

public function testDoesNotHaveValue()
{
$cache = new DefaultCache($maxsize = 4, $ttl = 2);
$cache = new DefaultCache(new FilesystemAdapter(), $maxsize = 4, $ttl = 2);
$key = 'test';

$this->assertFalse($cache->has($key));
}

public function testGetValue()
{
$cache = new DefaultCache($maxsize = 4, $ttl = 2);
$cache = new DefaultCache(new FilesystemAdapter(), $maxsize = 4, $ttl = 2);
$key1 = 'test';
$value1 = 'obama';
$cache->set($key1, $value1);
Expand All @@ -47,7 +48,7 @@ public function testGetValue()

public function testMaxSizeExceeded()
{
$cache = new DefaultCache($maxsize = 2, $ttl = 2);
$cache = new DefaultCache(new FilesystemAdapter(), $maxsize = 2, $ttl = 2);

$key1 = 'test';
$value1 = 'obama';
Expand All @@ -70,7 +71,7 @@ public function testMaxSizeExceeded()

public function testTimeToLiveExceeded()
{
$cache = new DefaultCache($maxsize = 2, $ttl = 1);
$cache = new DefaultCache(new FilesystemAdapter(), $maxsize = 2, $ttl = 1);

$key = 'test';
$value = 'obama';
Expand Down
Loading