Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
31 changes: 11 additions & 20 deletions system/Cache/CacheInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,15 @@ interface CacheInterface
{
/**
* Takes care of any handler-specific setup that must be done.
*
* @return void
*/
public function initialize();
public function initialize(): void;

/**
* Attempts to fetch an item from the cache store.
*
* @param string $key Cache item name
*
* @return mixed
*/
public function get(string $key);
public function get(string $key): mixed;

/**
* Saves an item to the cache store.
Expand All @@ -40,7 +36,7 @@ public function get(string $key);
*
* @return bool Success or failure
*/
public function save(string $key, $value, int $ttl = 60);
public function save(string $key, mixed $value, int $ttl = 60): bool;

/**
* Deletes a specific item from the cache store.
Expand All @@ -49,7 +45,7 @@ public function save(string $key, $value, int $ttl = 60);
*
* @return bool Success or failure
*/
public function delete(string $key);
public function delete(string $key): bool;

/**
* Deletes items from the cache store matching a given pattern.
Expand All @@ -65,27 +61,23 @@ public function deleteMatching(string $pattern): int;
*
* @param string $key Cache ID
* @param int $offset Step/value to increase by
*
* @return bool|int
*/
public function increment(string $key, int $offset = 1);
public function increment(string $key, int $offset = 1): bool|int;

/**
* Performs atomic decrementation of a raw stored value.
*
* @param string $key Cache ID
* @param int $offset Step/value to increase by
*
* @return bool|int
*/
public function decrement(string $key, int $offset = 1);
public function decrement(string $key, int $offset = 1): bool|int;

/**
* Will delete all items in the entire cache.
*
* @return bool Success or failure
*/
public function clean();
public function clean(): bool;

/**
* Returns information on the entire cache.
Expand All @@ -95,18 +87,17 @@ public function clean();
*
* @return array<array-key, mixed>|false|object|null
*/
public function getCacheInfo();
public function getCacheInfo(): array|false|object|null;

/**
* Returns detailed information about the specific item in the cache.
*
* @param string $key Cache item name.
*
* @return array<string, mixed>|false|null Returns null if the item does not exist, otherwise array<string, mixed>
* with at least the 'expire' key for absolute epoch expiry (or null).
* Some handlers may return false when an item does not exist, which is deprecated.
* @return array<string, mixed>|null Returns null if the item does not exist, otherwise array<string, mixed>
* with at least the 'expire' key for absolute epoch expiry (or null).
*/
public function getMetaData(string $key);
public function getMetaData(string $key): ?array;

/**
* Determines if the driver is supported on this system.
Expand Down
4 changes: 1 addition & 3 deletions system/Cache/Handlers/BaseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,8 @@ public static function validateKey($key, $prefix = ''): string
* @param string $key Cache item name
* @param int $ttl Time to live
* @param Closure(): mixed $callback Callback return value
*
* @return mixed
*/
public function remember(string $key, int $ttl, Closure $callback)
public function remember(string $key, int $ttl, Closure $callback): mixed
{
$value = $this->get($key);

Expand Down
56 changes: 10 additions & 46 deletions system/Cache/Handlers/DummyHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,96 +22,60 @@
*/
class DummyHandler extends BaseHandler
{
/**
* {@inheritDoc}
*/
public function initialize()
public function initialize(): void
{
}

/**
* {@inheritDoc}
*/
public function get(string $key)
public function get(string $key): mixed
{
return null;
}

/**
* {@inheritDoc}
*/
public function remember(string $key, int $ttl, Closure $callback)
public function remember(string $key, int $ttl, Closure $callback): mixed
{
return null;
}

/**
* {@inheritDoc}
*/
public function save(string $key, $value, int $ttl = 60)
public function save(string $key, mixed $value, int $ttl = 60): bool
{
return true;
}

/**
* {@inheritDoc}
*/
public function delete(string $key)
public function delete(string $key): bool
{
return true;
}

/**
* {@inheritDoc}
*/
public function deleteMatching(string $pattern): int
{
return 0;
}

/**
* {@inheritDoc}
*/
public function increment(string $key, int $offset = 1)
public function increment(string $key, int $offset = 1): bool
{
return true;
}

/**
* {@inheritDoc}
*/
public function decrement(string $key, int $offset = 1)
public function decrement(string $key, int $offset = 1): bool
{
return true;
}

/**
* {@inheritDoc}
*/
public function clean()
public function clean(): bool
{
return true;
}

/**
* {@inheritDoc}
*/
public function getCacheInfo()
public function getCacheInfo(): ?array
{
return null;
}

/**
* {@inheritDoc}
*/
public function getMetaData(string $key)
public function getMetaData(string $key): ?array
{
return null;
}

/**
* {@inheritDoc}
*/
public function isSupported(): bool
{
return true;
Expand Down
63 changes: 14 additions & 49 deletions system/Cache/Handlers/FileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,28 +72,19 @@ public function __construct(Cache $config)
helper('filesystem');
}

/**
* {@inheritDoc}
*/
public function initialize()
public function initialize(): void
{
}

/**
* {@inheritDoc}
*/
public function get(string $key)
public function get(string $key): mixed
{
$key = static::validateKey($key, $this->prefix);
$data = $this->getItem($key);

return is_array($data) ? $data['data'] : null;
}

/**
* {@inheritDoc}
*/
public function save(string $key, $value, int $ttl = 60)
public function save(string $key, mixed $value, int $ttl = 60): bool
{
$key = static::validateKey($key, $this->prefix);

Expand All @@ -119,19 +110,13 @@ public function save(string $key, $value, int $ttl = 60)
return false;
}

/**
* {@inheritDoc}
*/
public function delete(string $key)
public function delete(string $key): bool
{
$key = static::validateKey($key, $this->prefix);

return is_file($this->path . $key) && unlink($this->path . $key);
}

/**
* {@inheritDoc}
*/
public function deleteMatching(string $pattern): int
{
$deleted = 0;
Expand All @@ -145,10 +130,7 @@ public function deleteMatching(string $pattern): int
return $deleted;
}

/**
* {@inheritDoc}
*/
public function increment(string $key, int $offset = 1)
public function increment(string $key, int $offset = 1): bool|int
{
$prefixedKey = static::validateKey($key, $this->prefix);
$tmp = $this->getItem($prefixedKey);
Expand All @@ -168,39 +150,27 @@ public function increment(string $key, int $offset = 1)
return $this->save($key, $value, $ttl) ? $value : false;
}

/**
* {@inheritDoc}
*/
public function decrement(string $key, int $offset = 1)
public function decrement(string $key, int $offset = 1): bool|int
{
return $this->increment($key, -$offset);
}

/**
* {@inheritDoc}
*/
public function clean()
public function clean(): bool
{
return delete_files($this->path, false, true);
}

/**
* {@inheritDoc}
*/
public function getCacheInfo()
public function getCacheInfo(): array
{
return get_dir_file_info($this->path);
}

/**
* {@inheritDoc}
*/
public function getMetaData(string $key)
public function getMetaData(string $key): ?array
{
$key = static::validateKey($key, $this->prefix);

if (false === $data = $this->getItem($key)) {
return false; // @TODO This will return null in a future release
return null;
}

return [
Expand All @@ -210,9 +180,6 @@ public function getMetaData(string $key)
];
}

/**
* {@inheritDoc}
*/
public function isSupported(): bool
{
return is_writable($this->path);
Expand All @@ -224,7 +191,7 @@ public function isSupported(): bool
*
* @return array{data: mixed, ttl: int, time: int}|false
*/
protected function getItem(string $filename)
protected function getItem(string $filename): array|false
{
if (! is_file($this->path . $filename)) {
return false;
Expand Down Expand Up @@ -271,10 +238,8 @@ protected function getItem(string $filename)
* @param string $path
* @param string $data
* @param string $mode
*
* @return bool
*/
protected function writeFile($path, $data, $mode = 'wb')
protected function writeFile($path, $data, $mode = 'wb'): bool
{
if (($fp = @fopen($path, $mode)) === false) {
return false;
Expand Down Expand Up @@ -353,7 +318,7 @@ protected function deleteFiles(string $path, bool $delDir = false, bool $htdocs
* relative_path: string,
* }>|false
*/
protected function getDirFileInfo(string $sourceDir, bool $topLevelOnly = true, bool $_recursion = false)
protected function getDirFileInfo(string $sourceDir, bool $topLevelOnly = true, bool $_recursion = false): array|false
{
static $filedata = [];

Expand Down Expand Up @@ -412,7 +377,7 @@ protected function getDirFileInfo(string $sourceDir, bool $topLevelOnly = true,
* fileperms?: int
* }|false
*/
protected function getFileInfo(string $file, $returnedValues = ['name', 'server_path', 'size', 'date'])
protected function getFileInfo(string $file, $returnedValues = ['name', 'server_path', 'size', 'date']): array|false
{
if (! is_file($file)) {
return false;
Expand Down
Loading
Loading