Skip to content

Commit

Permalink
Cache WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Serban Ghita committed Dec 9, 2024
1 parent c348bc5 commit 1633213
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 18 deletions.
8 changes: 6 additions & 2 deletions src/Cache/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace Detection\Cache;

use DateTime;
use Psr\SimpleCache\CacheInterface;

/**
* Generic implementation of a cache system using an associative array.
* Generic implementation of a Simple Cache system using an associative array.
* The cache items are PSR-6 compatible.
*/
class Cache implements CacheInterface
{
Expand Down Expand Up @@ -47,7 +49,9 @@ public function set(string $key, mixed $value, \DateInterval|int|null $ttl = nul
if (empty($key)) {
throw new CacheException('Invalid cache key');
}
$item = new CacheItem($key, $value, $ttl);
$item = new CacheItem($key, $value);
$item->expiresAt($ttl);
$item->expiresAfter($ttl);
$this->cache_db[$key] = $item;
return true;
}
Expand Down
62 changes: 49 additions & 13 deletions src/Cache/CacheItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Detection\Cache;

use DateInterval;
use DateTime;
use DateTimeInterface;
use Psr\Cache\CacheItemInterface;

/**
Expand All @@ -18,14 +21,23 @@ class CacheItem implements CacheItemInterface
* @var bool|null Mobile Detect only needs to store booleans (e.g. "isMobile" => true)
*/
protected bool|null $value = null;
protected int|null $ttl = 0;
public function __construct($key, $value = null, $ttl = null)
/**
* @var DateTimeInterface|null
*/
public DateTimeInterface|null $expiresAt = null;
/**
* @var DateInterval|null
*/
public DateInterval|null $expiresAfter = null;

public function __construct($key, $value = null, $expiresAt = null, $expiresAfter = null)
{
$this->key = $key;
if (!is_null($value)) {
$this->value = $value;
}
$this->ttl = $ttl;
$this->expiresAt = $expiresAt;
$this->expiresAfter = $expiresAfter;
}

public function getKey(): string
Expand All @@ -43,23 +55,47 @@ public function set($value): void
$this->value = $value;
}

public function getTtl(): int|null
public function isHit(): bool
{
return $this->ttl;
}
// Item never expires.
if ($this->expiresAt === null) {
return true;
}

public function isHit()
{
// TODO: Implement isHit() method.
if ($this->expiresAt > new DateTime()) {
return true;
}

return false;
}

public function expiresAt($expiration)
public function expiresAt($expiration): void

Check failure on line 72 in src/Cache/CacheItem.php

View workflow job for this annotation

GitHub Actions / run (8.1, ubuntu-latest, latest, ^9.6.18)

Return type void of method Detection\Cache\CacheItem::expiresAt() is not covariant with return type static(Psr\Cache\CacheItemInterface) of method Psr\Cache\CacheItemInterface::expiresAt().

Check failure on line 72 in src/Cache/CacheItem.php

View workflow job for this annotation

GitHub Actions / run (8.2, ubuntu-latest, latest, ^9.6.18)

Return type void of method Detection\Cache\CacheItem::expiresAt() is not covariant with return type static(Psr\Cache\CacheItemInterface) of method Psr\Cache\CacheItemInterface::expiresAt().

Check failure on line 72 in src/Cache/CacheItem.php

View workflow job for this annotation

GitHub Actions / run (8.3, ubuntu-latest, latest, ^9.6.18)

Return type void of method Detection\Cache\CacheItem::expiresAt() is not covariant with return type static(Psr\Cache\CacheItemInterface) of method Psr\Cache\CacheItemInterface::expiresAt().

Check failure on line 72 in src/Cache/CacheItem.php

View workflow job for this annotation

GitHub Actions / run (8.4, ubuntu-latest, latest, ^9.6.18)

Return type void of method Detection\Cache\CacheItem::expiresAt() is not covariant with return type static(Psr\Cache\CacheItemInterface) of method Psr\Cache\CacheItemInterface::expiresAt().
{
// TODO: Implement expiresAt() method.
$expiresAt = null;

if ($expiration instanceof \DateInterval) {
$expiresAt = (new DateTime())->add($expiration);
} elseif (is_int($expiration)) {
if ($expiration > 0) {
$expiresAt = new DateTime("{$expiration} seconds");
}
}

$this->expiresAt = $expiresAt;
}

public function expiresAfter($time)
public function expiresAfter($time): void

Check failure on line 87 in src/Cache/CacheItem.php

View workflow job for this annotation

GitHub Actions / run (8.1, ubuntu-latest, latest, ^9.6.18)

Return type void of method Detection\Cache\CacheItem::expiresAfter() is not covariant with return type static(Psr\Cache\CacheItemInterface) of method Psr\Cache\CacheItemInterface::expiresAfter().

Check failure on line 87 in src/Cache/CacheItem.php

View workflow job for this annotation

GitHub Actions / run (8.2, ubuntu-latest, latest, ^9.6.18)

Return type void of method Detection\Cache\CacheItem::expiresAfter() is not covariant with return type static(Psr\Cache\CacheItemInterface) of method Psr\Cache\CacheItemInterface::expiresAfter().

Check failure on line 87 in src/Cache/CacheItem.php

View workflow job for this annotation

GitHub Actions / run (8.3, ubuntu-latest, latest, ^9.6.18)

Return type void of method Detection\Cache\CacheItem::expiresAfter() is not covariant with return type static(Psr\Cache\CacheItemInterface) of method Psr\Cache\CacheItemInterface::expiresAfter().

Check failure on line 87 in src/Cache/CacheItem.php

View workflow job for this annotation

GitHub Actions / run (8.4, ubuntu-latest, latest, ^9.6.18)

Return type void of method Detection\Cache\CacheItem::expiresAfter() is not covariant with return type static(Psr\Cache\CacheItemInterface) of method Psr\Cache\CacheItemInterface::expiresAfter().
{
// TODO: Implement expiresAfter() method.
$expiresAfter = null;

if ($time instanceof \DateInterval) {
$expiresAfter = $time;
} elseif (is_int($time)) {
if ($time > 0) {
$expiresAfter = new \DateInterval("PT{$time}S");
}
}

$this->expiresAfter = $expiresAfter;
}
}
15 changes: 12 additions & 3 deletions tests/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,26 @@ public function testGetTTL0()
{
$this->cache->set('isMobile', true, 0);
$this->assertInstanceOf(CacheItem::class, $this->cache->get('isMobile'));
$this->assertEquals(0, $this->cache->get('isMobile')->getTtl());
$this->assertNull($this->cache->get('isMobile')->expiresAt);
$this->assertNull($this->cache->get('isMobile')->expiresAfter);
}

public function testGetTtlIsInteger()
{
$this->cache->set('isMobile', true, 1000);
$this->assertInstanceOf(CacheItem::class, $this->cache->get('isMobile'));
$this->assertInstanceOf(\DateTime::class, $this->cache->get('isMobile')->expiresAt);
$this->assertInstanceOf(\DateInterval::class, $this->cache->get('isMobile')->expiresAfter);
}

/**
* @throws CacheException
*/
public function testGetTTLNull()
public function testGetExpiresAfter()
{
$this->cache->set('isMobile', true);
$this->assertInstanceOf(CacheItem::class, $this->cache->get('isMobile'));
$this->assertNull($this->cache->get('isMobile')->getTtl());
$this->assertNull($this->cache->get('isMobile')->expiresAfter);
}

/**
Expand Down

0 comments on commit 1633213

Please sign in to comment.