first version

This commit is contained in:
Bastian Allgeier
2019-01-13 23:17:34 +01:00
commit 01277f79f2
595 changed files with 82913 additions and 0 deletions

77
kirby/src/Cache/ApcuCache.php Executable file
View File

@@ -0,0 +1,77 @@
<?php
namespace Kirby\Cache;
/**
* APCu Cache Driver
*
* @package Kirby Cache
* @author Bastian Allgeier <bastian@getkirby.com>
* @link http://getkirby.com
* @copyright Bastian Allgeier
* @license MIT
*/
class ApcuCache extends Cache
{
/**
* Write an item to the cache for a given number of minutes.
*
* <code>
* // Put an item in the cache for 15 minutes
* Cache::set('value', 'my value', 15);
* </code>
*
* @param string $key
* @param mixed $value
* @param int $minutes
* @return void
*/
public function set(string $key, $value, int $minutes = 0)
{
return apcu_store($key, $this->value($value, $minutes)->toJson(), $this->expiration($minutes));
}
/**
* Retrieve an item from the cache.
*
* @param string $key
* @return mixed
*/
public function retrieve(string $key)
{
return Value::fromJson(apcu_fetch($key));
}
/**
* Checks if the current key exists in cache
*
* @param string $key
* @return boolean
*/
public function exists(string $key): bool
{
return apcu_exists($key);
}
/**
* Remove an item from the cache
*
* @param string $key
* @return boolean
*/
public function remove(string $key): bool
{
return apcu_delete($key);
}
/**
* Flush the entire cache directory
*
* @return boolean
*/
public function flush(): bool
{
return apcu_clear_cache();
}
}

237
kirby/src/Cache/Cache.php Executable file
View File

@@ -0,0 +1,237 @@
<?php
namespace Kirby\Cache;
/**
* Cache foundation
* This class doesn't do anything
* and is perfect as foundation for
* other cache drivers and to be used
* when the cache is disabled
*
* @package Kirby Cache
* @author Bastian Allgeier <bastian@getkirby.com>
* @link http://getkirby.com
* @copyright Bastian Allgeier
* @license MIT
*/
class Cache
{
/**
* stores all options for the driver
* @var array
*/
protected $options = [];
/**
* Set all parameters which are needed to connect to the cache storage
*
* @param array $options
*/
public function __construct(array $options = [])
{
$this->options = $options;
}
/**
* Write an item to the cache for a given number of minutes.
*
* <code>
* // Put an item in the cache for 15 minutes
* Cache::set('value', 'my value', 15);
* </code>
*
* @param string $key
* @param mixed $value
* @param int $minutes
* @return void
*/
public function set(string $key, $value, int $minutes = 0)
{
return null;
}
/**
* Private method to retrieve the cache value
* This needs to be defined by the driver
*
* @param string $key
* @return mixed
*/
public function retrieve(string $key)
{
return null;
}
/**
* Get an item from the cache.
*
* <code>
* // Get an item from the cache driver
* $value = Cache::get('value');
*
* // Return a default value if the requested item isn't cached
* $value = Cache::get('value', 'default value');
* </code>
*
* @param string $key
* @param mixed $default
* @return mixed
*/
public function get(string $key, $default = null)
{
// get the Value
$value = $this->retrieve($key);
// check for a valid cache value
if (!is_a($value, 'Kirby\Cache\Value')) {
return $default;
}
// remove the item if it is expired
if (time() >= $value->expires()) {
$this->remove($key);
return $default;
}
// get the pure value
$cache = $value->value();
// return the cache value or the default
return $cache ?? $default;
}
/**
* Calculates the expiration timestamp
*
* @param int $minutes
* @return int
*/
protected function expiration(int $minutes = 0): int
{
// keep forever if minutes are not defined
if ($minutes === 0) {
$minutes = 2628000;
}
// calculate the time
return time() + ($minutes * 60);
}
/**
* Checks when an item in the cache expires
*
* @param string $key
* @return mixed
*/
public function expires(string $key)
{
// get the Value object
$value = $this->retrieve($key);
// check for a valid Value object
if (!is_a($value, 'Kirby\Cache\Value')) {
return false;
}
// return the expires timestamp
return $value->expires();
}
/**
* Checks if an item in the cache is expired
*
* @param string $key
* @return boolean
*/
public function expired(string $key): bool
{
return $this->expires($key) <= time();
}
/**
* Checks when the cache has been created
*
* @param string $key
* @return mixed
*/
public function created(string $key)
{
// get the Value object
$value = $this->retrieve($key);
// check for a valid Value object
if (!is_a($value, 'Kirby\Cache\Value')) {
return false;
}
// return the expires timestamp
return $value->created();
}
/**
* Alternate version for Cache::created($key)
*
* @param string $key
* @return mixed
*/
public function modified(string $key)
{
return static::created($key);
}
/**
* Returns Value object
*
* @param mixed $value The value, which should be cached
* @param int $minutes The number of minutes before expiration
* @return Value
*/
protected function value($value, int $minutes): Value
{
return new Value($value, $minutes);
}
/**
* Determine if an item exists in the cache.
*
* @param string $key
* @return boolean
*/
public function exists(string $key): bool
{
return !$this->expired($key);
}
/**
* Remove an item from the cache
*
* @param string $key
* @return boolean
*/
public function remove(string $key): bool
{
return true;
}
/**
* Flush the entire cache
*
* @return boolean
*/
public function flush(): bool
{
return true;
}
/**
* Returns all passed cache options
*
* @return array
*/
public function options(): array
{
return $this->options;
}
}

126
kirby/src/Cache/FileCache.php Executable file
View File

@@ -0,0 +1,126 @@
<?php
namespace Kirby\Cache;
use Exception;
use Kirby\Toolkit\Dir;
use Kirby\Toolkit\F;
/**
* File System Cache Driver
*
* @package Kirby Cache
* @author Bastian Allgeier <bastian@getkirby.com>
* @link http://getkirby.com
* @copyright Bastian Allgeier
* @license MIT
*/
class FileCache extends Cache
{
/**
* Set all parameters which are needed for the file cache
* see defaults for available parameters
*
* @param array $params
*/
public function __construct(array $params)
{
$defaults = [
'root' => null,
'extension' => null
];
parent::__construct(array_merge($defaults, $params));
// try to create the directory
Dir::make($this->options['root'], true);
// check for a valid cache directory
if (is_dir($this->options['root']) === false) {
throw new Exception('The cache directory does not exist');
}
}
/**
* Returns the full path to a file for a given key
*
* @param string $key
* @return string
*/
protected function file(string $key): string
{
$extension = isset($this->options['extension']) ? '.' . $this->options['extension'] : '';
return $this->options['root'] . '/' . $key . $extension;
}
/**
* Write an item to the cache for a given number of minutes.
*
* <code>
* // Put an item in the cache for 15 minutes
* Cache::set('value', 'my value', 15);
* </code>
*
* @param string $key
* @param mixed $value
* @param int $minutes
*/
public function set(string $key, $value, int $minutes = 0)
{
return F::write($this->file($key), $this->value($value, $minutes)->toJson());
}
/**
* Retrieve an item from the cache.
*
* @param string $key
* @return mixed
*/
public function retrieve(string $key)
{
return Value::fromJson(F::read($this->file($key)));
}
/**
* Checks when the cache has been created
*
* @param string $key
* @return int
*/
public function created(string $key): int
{
// use the modification timestamp
// as indicator when the cache has been created/overwritten
clearstatcache();
// get the file for this cache key
$file = $this->file($key);
return file_exists($file) ? filemtime($this->file($key)) : 0;
}
/**
* Remove an item from the cache
*
* @param string $key
* @return boolean
*/
public function remove(string $key): bool
{
return F::remove($this->file($key));
}
/**
* Flush the entire cache directory
*
* @return boolean
*/
public function flush(): bool
{
if (Dir::remove($this->options['root']) === true && Dir::make($this->options['root']) === true) {
return true;
}
return false;
}
}

137
kirby/src/Cache/MemCached.php Executable file
View File

@@ -0,0 +1,137 @@
<?php
namespace Kirby\Cache;
/**
* Memcached Driver
*
* @package Kirby Cache
* @author Bastian Allgeier <bastian@getkirby.com>
* @link http://getkirby.com
* @copyright Bastian Allgeier
* @license MIT
*/
class MemCached extends Cache
{
/**
* store for the memache connection
* @var Memcached
*/
protected $connection;
/**
* Set all parameters which are needed for the memcache client
* see defaults for available parameters
*
* @param array $params
*/
public function __construct(array $params = [])
{
$defaults = [
'host' => 'localhost',
'port' => 11211,
'prefix' => null,
];
parent::__construct(array_merge($defaults, $params));
$this->connection = new \Memcached();
$this->connection->addServer($this->options['host'], $this->options['port']);
}
/**
* Write an item to the cache for a given number of minutes.
*
* <code>
* // Put an item in the cache for 15 minutes
* Cache::set('value', 'my value', 15);
* </code>
*
* @param string $key
* @param mixed $value
* @param int $minutes
* @return void
*/
public function set(string $key, $value, int $minutes = 0)
{
return $this->connection->set($this->key($key), $this->value($value, $minutes)->toJson(), $this->expiration($minutes));
}
/**
* Returns the full keyname
* including the prefix (if set)
*
* @param string $key
* @return string
*/
public function key(string $key): string
{
return $this->options['prefix'] . $key;
}
/**
* Retrieve the CacheValue object from the cache.
*
* @param string $key
* @return object CacheValue
*/
public function retrieve(string $key)
{
return Value::fromJson($this->connection->get($this->key($key)));
}
/**
* Remove an item from the cache
*
* @param string $key
* @return boolean
*/
public function remove(string $key): bool
{
return $this->connection->delete($this->key($key));
}
/**
* Checks when an item in the cache expires
*
* @param string $key
* @return int
*/
public function expires(string $key): int
{
return parent::expires($this->key($key));
}
/**
* Checks if an item in the cache is expired
*
* @param string $key
* @return boolean
*/
public function expired(string $key): bool
{
return parent::expired($this->key($key));
}
/**
* Checks when the cache has been created
*
* @param string $key
* @return int
*/
public function created(string $key): int
{
return parent::created($this->key($key));
}
/**
* Flush the entire cache directory
*
* @return boolean
*/
public function flush(): bool
{
return $this->connection->flush();
}
}

139
kirby/src/Cache/Value.php Executable file
View File

@@ -0,0 +1,139 @@
<?php
namespace Kirby\Cache;
use Throwable;
/**
* Cache Value
* Stores the value, creation timestamp and expiration timestamp
* and makes it possible to store all three with a single cache key.
*
* @package Kirby Cache
* @author Bastian Allgeier <bastian@getkirby.com>
* @link http://getkirby.com
* @copyright Bastian Allgeier
* @license MIT
*/
class Value
{
/**
* the cached value
* @var mixed
*/
protected $value;
/**
* the expiration timestamp
* @var int
*/
protected $expires;
/**
* the creation timestamp
* @var int
*/
protected $created;
/**
* Constructor
*
* @param mixed $value
* @param int $minutes the number of minutes until the value expires
* @param int $created the unix timestamp when the value has been created
*/
public function __construct($value, int $minutes = 0, $created = null)
{
// keep forever if minutes are not defined
if ($minutes === 0) {
$minutes = 2628000;
}
$this->value = $value;
$this->minutes = $minutes;
$this->created = $created ?? time();
}
/**
* Returns the creation date as UNIX timestamp
*
* @return int
*/
public function created(): int
{
return $this->created;
}
/**
* Returns the expiration date as UNIX timestamp
*
* @return int
*/
public function expires(): int
{
return $this->created + ($this->minutes * 60);
}
/**
* Creates a value object from an array
*
* @param array $array
* @return array
*/
public static function fromArray(array $array): self
{
return new static($array['value'] ?? null, $array['minutes'] ?? 0, $array['created'] ?? null);
}
/**
* Creates a value object from a json string
*
* @param string $json
* @return array
*/
public static function fromJson($json): self
{
try {
$array = json_decode($json, true) ?? [];
} catch (Throwable $e) {
$array = [];
}
return static::fromArray($array);
}
/**
* Convert the object to a json string
*
* @return string
*/
public function toJson(): string
{
return json_encode($this->toArray());
}
/**
* Convert the object to an array
*
* @return array
*/
public function toArray(): array
{
return [
'created' => $this->created,
'minutes' => $this->minutes,
'value' => $this->value,
];
}
/**
* Returns the value
*
* @return mixed
*/
public function value()
{
return $this->value;
}
}