julienmonnerie/kirby/src/Cache/Cache.php

239 lines
5 KiB
PHP
Raw Normal View History

2022-06-17 17:51:59 +02:00
<?php
namespace Kirby\Cache;
2022-12-19 14:56:05 +01:00
use Closure;
2022-06-17 17:51:59 +02:00
/**
* Cache foundation
* This abstract class is used as
* foundation for other cache drivers
* by extending it
*
* @package Kirby Cache
* @author Bastian Allgeier <bastian@getkirby.com>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://opensource.org/licenses/MIT
*/
abstract class Cache
{
2022-08-31 15:02:43 +02:00
/**
* Stores all options for the driver
*/
2022-12-19 14:56:05 +01:00
protected array $options = [];
2022-08-31 15:02:43 +02:00
/**
* Sets all parameters which are needed to connect to the cache storage
*/
public function __construct(array $options = [])
{
$this->options = $options;
}
/**
2022-12-19 14:56:05 +01:00
* Checks when the cache has been created;
* returns the creation timestamp on success
* and false if the item does not exist
2022-08-31 15:02:43 +02:00
*/
2022-12-19 14:56:05 +01:00
public function created(string $key): int|false
2022-08-31 15:02:43 +02:00
{
2022-12-19 14:56:05 +01:00
// get the Value object
$value = $this->retrieve($key);
// check for a valid Value object
if ($value instanceof Value === false) {
return false;
2022-08-31 15:02:43 +02:00
}
2022-12-19 14:56:05 +01:00
// return the expires timestamp
return $value->created();
2022-08-31 15:02:43 +02:00
}
/**
2022-12-19 14:56:05 +01:00
* Returns whether the cache is ready to
* store values
2022-08-31 15:02:43 +02:00
*/
2022-12-19 14:56:05 +01:00
public function enabled(): bool
{
// TODO: Make this method abstract in a future
// release to ensure that cache drivers override it;
// until then, we assume that the cache is enabled
return true;
}
2022-08-31 15:02:43 +02:00
/**
2022-12-19 14:56:05 +01:00
* Determines if an item exists in the cache
2022-08-31 15:02:43 +02:00
*/
2022-12-19 14:56:05 +01:00
public function exists(string $key): bool
2022-08-31 15:02:43 +02:00
{
2022-12-19 14:56:05 +01:00
return $this->expired($key) === false;
2022-08-31 15:02:43 +02:00
}
2022-12-19 14:56:05 +01:00
2022-08-31 15:02:43 +02:00
/**
* Calculates the expiration timestamp
*/
protected function expiration(int $minutes = 0): int
{
// 0 = keep forever
if ($minutes === 0) {
return 0;
}
// calculate the time
return time() + ($minutes * 60);
}
/**
* Checks when an item in the cache expires;
* returns the expiry timestamp on success, null if the
* item never expires and false if the item does not exist
*/
2022-12-19 14:56:05 +01:00
public function expires(string $key): int|false|null
2022-08-31 15:02:43 +02:00
{
// get the Value object
$value = $this->retrieve($key);
// check for a valid Value object
2022-12-19 14:56:05 +01:00
if ($value instanceof Value === false) {
2022-08-31 15:02:43 +02:00
return false;
}
// return the expires timestamp
return $value->expires();
}
/**
* Checks if an item in the cache is expired
*/
public function expired(string $key): bool
{
$expires = $this->expires($key);
if ($expires === null) {
return false;
2022-12-19 14:56:05 +01:00
}
if (is_int($expires) === false) {
2022-08-31 15:02:43 +02:00
return true;
}
2022-12-19 14:56:05 +01:00
return time() >= $expires;
2022-08-31 15:02:43 +02:00
}
/**
2022-12-19 14:56:05 +01:00
* Flushes the entire cache and returns
* whether the operation was successful;
* this needs to be defined by the driver
*/
abstract public function flush(): bool;
/**
* Gets an item from the cache
*
* <code>
* // get an item from the cache driver
* $value = $cache->get('value');
2022-08-31 15:02:43 +02:00
*
2022-12-19 14:56:05 +01:00
* // return a default value if the requested item isn't cached
* $value = $cache->get('value', 'default value');
* </code>
2022-08-31 15:02:43 +02:00
*/
2022-12-19 14:56:05 +01:00
public function get(string $key, $default = null)
2022-08-31 15:02:43 +02:00
{
2022-12-19 14:56:05 +01:00
// get the Value
2022-08-31 15:02:43 +02:00
$value = $this->retrieve($key);
2022-12-19 14:56:05 +01:00
// check for a valid cache value
if ($value instanceof Value === false) {
return $default;
2022-08-31 15:02:43 +02:00
}
2022-12-19 14:56:05 +01:00
// remove the item if it is expired
if ($value->expires() > 0 && time() >= $value->expires()) {
$this->remove($key);
return $default;
}
// return the pure value
return $value->value();
}
/**
* Returns a value by either getting it from the cache
* or via the callback function which then is stored in
* the cache for future retrieval. This method cannot be
* used for `null` as value to be cached.
* @since 3.8.0
*/
public function getOrSet(
string $key,
Closure $result,
int $minutes = 0
) {
$value = $this->get($key);
$result = $value ?? $result();
if ($value === null) {
$this->set($key, $result, $minutes);
}
return $result;
}
/**
* Adds the prefix to the key if given
*/
protected function key(string $key): string
{
if (empty($this->options['prefix']) === false) {
$key = $this->options['prefix'] . '/' . $key;
}
return $key;
2022-08-31 15:02:43 +02:00
}
/**
* Alternate version for Cache::created($key)
*/
2022-12-19 14:56:05 +01:00
public function modified(string $key): int|false
2022-08-31 15:02:43 +02:00
{
return static::created($key);
}
/**
2022-12-19 14:56:05 +01:00
* Returns all passed cache options
2022-08-31 15:02:43 +02:00
*/
2022-12-19 14:56:05 +01:00
public function options(): array
2022-08-31 15:02:43 +02:00
{
2022-12-19 14:56:05 +01:00
return $this->options;
2022-08-31 15:02:43 +02:00
}
/**
* Removes an item from the cache and returns
* whether the operation was successful;
* this needs to be defined by the driver
*/
abstract public function remove(string $key): bool;
/**
2022-12-19 14:56:05 +01:00
* Internal method to retrieve the raw cache value;
* needs to return a Value object or null if not found;
2022-08-31 15:02:43 +02:00
* this needs to be defined by the driver
*/
2022-12-19 14:56:05 +01:00
abstract public function retrieve(string $key): Value|null;
2022-08-31 15:02:43 +02:00
/**
2022-12-19 14:56:05 +01:00
* Writes an item to the cache for a given number of minutes and
* returns whether the operation was successful;
* this needs to be defined by the driver
2022-08-31 15:02:43 +02:00
*
2022-12-19 14:56:05 +01:00
* <code>
* // put an item in the cache for 15 minutes
* $cache->set('value', 'my value', 15);
* </code>
2022-08-31 15:02:43 +02:00
*/
2022-12-19 14:56:05 +01:00
abstract public function set(string $key, $value, int $minutes = 0): bool;
2022-06-17 17:51:59 +02:00
}