Skip to content

Commit

Permalink
Add Types
Browse files Browse the repository at this point in the history
  • Loading branch information
byjg committed Sep 12, 2024
1 parent cf632b7 commit dd80109
Show file tree
Hide file tree
Showing 20 changed files with 267 additions and 106 deletions.
3 changes: 3 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# These are supported funding model platforms

github: byjg
1 change: 1 addition & 0 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ jobs:
- uses: actions/checkout@v4
- run: composer install
- run: ./vendor/bin/phpunit --stderr
- run: ./vendor/bin/psalm

Documentation:
if: github.ref == 'refs/heads/master'
Expand Down
6 changes: 6 additions & 0 deletions .run/PHPUnit.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="PHPUnit" type="PHPUnitRunConfigurationType" factoryName="PHPUnit">
<TestRunner bootstrap_file="$PROJECT_DIR$/phpunit.xml.dist" configuration_file="$PROJECT_DIR$/phpunit.xml.dist" directory="$PROJECT_DIR$" scope="XML" options="--stderr" use_alternative_configuration_file="true" />
<method v="2" />
</configuration>
</component>
5 changes: 5 additions & 0 deletions .run/PSalm.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="PSalm" type="PhpLocalRunConfigurationType" factoryName="PHP Console" path="$PROJECT_DIR$/vendor/bin/psalm">
<method v="2" />
</configuration>
</component>
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
"ByJG\\Cache\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"require": {
"php": ">=8.1",
"psr/cache": "^1.0|^2.0|^3.0",
Expand All @@ -15,7 +20,7 @@
},
"require-dev": {
"phpunit/phpunit": "^9.6",
"vimeo/psalm": "^6.0"
"vimeo/psalm": "^5.9"
},
"suggest": {
"ext-memcached": "*",
Expand Down
18 changes: 18 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0"?>
<psalm
errorLevel="4"
resolveFromConfigFile="true"
findUnusedBaselineEntry="true"
findUnusedCode="false"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="src" />
<directory name="tests" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
</psalm>
7 changes: 4 additions & 3 deletions src/Psr16/ArrayCacheEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@
use DateInterval;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

class ArrayCacheEngine extends BaseCacheEngine
{

protected $cache = array();
protected array $cache = [];

protected $logger = null;
protected LoggerInterface|null $logger = null;

public function __construct($logger = null)
public function __construct(LoggerInterface|null $logger = null)
{
$this->logger = $logger;
if (is_null($logger)) {
Expand Down
14 changes: 5 additions & 9 deletions src/Psr16/BaseCacheEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ abstract class BaseCacheEngine implements CacheInterface, CacheAvailabilityInter

/**
* @param string|iterable $keys
* @param null $default
* @return iterable
* @param mixed $default
* @return iterable<string, mixed>
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
public function getMultiple(string|iterable $keys, mixed $default = null): iterable
Expand All @@ -36,11 +36,11 @@ public function getMultiple(string|iterable $keys, mixed $default = null): itera

/**
* @param iterable $values
* @param null $ttl
* @param DateInterval|int|null $ttl
* @return bool
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
public function setMultiple(iterable $values, $ttl = null): bool
public function setMultiple(iterable $values, DateInterval|int|null $ttl = null): bool
{
foreach ($values as $key => $value) {
$this->set($key, $value, $ttl);
Expand Down Expand Up @@ -87,11 +87,7 @@ protected function convertToSeconds(DateInterval|int|null $ttl): DateInterval|in
return $ttl;
}

if ($ttl instanceof DateInterval) {
return $ttl->days*86400 + $ttl->h*3600 + $ttl->i*60 + $ttl->s;
}

throw new InvalidArgumentException('Invalid TTL');
return $ttl->days*86400 + $ttl->h*3600 + $ttl->i*60 + $ttl->s;
}


Expand Down
41 changes: 30 additions & 11 deletions src/Psr16/FileSystemCacheEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@
use ByJG\Cache\CacheLockInterface;
use DateInterval;
use Exception;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Psr\SimpleCache\InvalidArgumentException;

class FileSystemCacheEngine extends BaseCacheEngine implements CacheLockInterface
{

protected $logger = null;
protected ?LoggerInterface $logger = null;

protected $prefix = null;
protected $path = null;
protected ?string $prefix = null;
protected ?string $path = null;

public function __construct($prefix = 'cache', $path = null, $logger = null)
public function __construct(string $prefix = 'cache', ?string $path = null, ?LoggerInterface $logger = null)
{
$this->prefix = $prefix;
$this->path = $path ?? sys_get_temp_dir();
Expand All @@ -30,7 +34,9 @@ public function __construct($prefix = 'cache', $path = null, $logger = null)
* @param string $key The object KEY
* @param mixed $default IGNORED IN MEMCACHED.
* @return mixed Description
* @throws \Psr\SimpleCache\InvalidArgumentException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws \ByJG\Cache\Exception\InvalidArgumentException
*/
public function get(string $key, mixed $default = null): mixed
{
Expand Down Expand Up @@ -104,7 +110,7 @@ public function set(string $key, mixed $value, DateInterval|int|null $ttl = null

$validUntil = $this->addToNow($ttl);
if (!empty($validUntil)) {
file_put_contents($fileKey . ".ttl", $validUntil);
file_put_contents($fileKey . ".ttl", (string)$validUntil);
}
} catch (Exception $ex) {
$this->logger->warning("[Filesystem cache] I could not write to cache on file '" . basename($key) . "'. Switching to nocache=true mode.");
Expand All @@ -117,7 +123,6 @@ public function set(string $key, mixed $value, DateInterval|int|null $ttl = null
/**
* @param string $key
* @return bool
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
public function delete(string $key): bool
{
Expand Down Expand Up @@ -146,7 +151,7 @@ public function lock(string $key): void
* UnLock resource after set it.
* @param string $key
*/
public function unlock($key): void
public function unlock(string $key): void
{

$this->logger->info("[Filesystem cache] Unlock '$key'");
Expand All @@ -158,12 +163,22 @@ public function unlock($key): void
}
}

/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws \ByJG\Cache\Exception\InvalidArgumentException
*/
public function isAvailable(): bool
{
return is_writable(dirname($this->fixKey('test')));
}

protected function fixKey($key)
/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws \ByJG\Cache\Exception\InvalidArgumentException
*/
protected function fixKey(string $key): string
{
$key = $this->getKeyFromContainer($key);

Expand All @@ -177,6 +192,9 @@ protected function fixKey($key)
* Wipes clean the entire cache's keys.
*
* @return bool True on success and false on failure.
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws \ByJG\Cache\Exception\InvalidArgumentException
*/
public function clear(): bool
{
Expand All @@ -197,8 +215,9 @@ public function clear(): bool
*
* @param string $key The cache item key.
* @return bool
* @throws \Psr\SimpleCache\InvalidArgumentException
* MUST be thrown if the $key string is not a legal value.
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws \ByJG\Cache\Exception\InvalidArgumentException
*/
public function has(string $key): bool
{
Expand Down
44 changes: 33 additions & 11 deletions src/Psr16/MemcachedEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,29 @@

namespace ByJG\Cache\Psr16;

use ByJG\Cache\Exception\InvalidArgumentException;
use ByJG\Cache\Exception\StorageErrorException;
use DateInterval;
use Memcached;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

class MemcachedEngine extends BaseCacheEngine
{

/**
*
* @var Memcached
* @var Memcached|null
*/
protected $memCached = null;
protected Memcached|null $memCached = null;

protected $logger = null;
protected LoggerInterface|null $logger = null;

protected $servers = null;
protected ?array $servers = null;

public function __construct($servers = null, $logger = null)
public function __construct(?array $servers = null, $logger = null)
{
$this->servers = (array)$servers;
if (is_null($servers)) {
Expand All @@ -35,21 +39,27 @@ public function __construct($servers = null, $logger = null)
}
}

protected function fixKey($key) {
/**
* @throws ContainerExceptionInterface
* @throws InvalidArgumentException
* @throws NotFoundExceptionInterface
*/
protected function fixKey(string $key): string
{
$key = $this->getKeyFromContainer($key);
return "cache-" . $key;
}

/**
* @throws StorageErrorException
*/
protected function lazyLoadMemCachedServers()
protected function lazyLoadMemCachedServers(): void
{
if (is_null($this->memCached)) {
$this->memCached = new Memcached();
foreach ($this->servers as $server) {
$data = explode(":", $server);
$this->memCached->addServer($data[0], $data[1]);
$this->memCached->addServer($data[0], intval($data[1]));

$stats = $this->memCached->getStats();
if (!isset($stats[$server]) || $stats[$server]['pid'] === -1) {
Expand All @@ -60,9 +70,12 @@ protected function lazyLoadMemCachedServers()
}

/**
* @param string $key The object KEY
* @param int $default IGNORED IN MEMCACHED.
* @return mixed Description
* @param string $key
* @param mixed|null $default
* @return mixed
* @throws ContainerExceptionInterface
* @throws InvalidArgumentException
* @throws NotFoundExceptionInterface
* @throws StorageErrorException
*/
public function get(string $key, mixed $default = null): mixed
Expand All @@ -83,6 +96,9 @@ public function get(string $key, mixed $default = null): mixed
* @param mixed $value The object to be cached
* @param DateInterval|int|null $ttl The time to live in seconds of this objects
* @return bool If the object is successfully posted
* @throws ContainerExceptionInterface
* @throws InvalidArgumentException
* @throws NotFoundExceptionInterface
* @throws StorageErrorException
*/
public function set(string $key, mixed $value, DateInterval|int|null $ttl = null): bool
Expand All @@ -103,6 +119,9 @@ public function set(string $key, mixed $value, DateInterval|int|null $ttl = null
/**
* @param string $key
* @return bool
* @throws ContainerExceptionInterface
* @throws InvalidArgumentException
* @throws NotFoundExceptionInterface
* @throws StorageErrorException
*/
public function delete(string $key): bool
Expand Down Expand Up @@ -141,6 +160,9 @@ public function clear(): bool
/**
* @param string $key
* @return bool
* @throws ContainerExceptionInterface
* @throws InvalidArgumentException
* @throws NotFoundExceptionInterface
* @throws StorageErrorException
*/
public function has(string $key): bool
Expand Down
Loading

0 comments on commit dd80109

Please sign in to comment.