julienmonnerie/kirby/src/Cache/ApcuCache.php

84 lines
1.8 KiB
PHP
Raw Normal View History

2022-06-17 17:51:59 +02:00
<?php
namespace Kirby\Cache;
use APCUIterator;
/**
* APCu Cache Driver
*
* @package Kirby Cache
* @author Bastian Allgeier <bastian@getkirby.com>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://opensource.org/licenses/MIT
*/
class ApcuCache extends Cache
{
2022-12-19 14:56:05 +01:00
/**
* Returns whether the cache is ready to
* store values
*/
public function enabled(): bool
{
return apcu_enabled();
}
2022-08-31 15:02:43 +02:00
/**
* Determines if an item exists in the cache
*/
public function exists(string $key): bool
{
return apcu_exists($this->key($key));
}
2022-06-17 17:51:59 +02:00
2022-08-31 15:02:43 +02:00
/**
* Flushes the entire cache and returns
* whether the operation was successful
*/
public function flush(): bool
{
if (empty($this->options['prefix']) === false) {
return apcu_delete(new APCUIterator('!^' . preg_quote($this->options['prefix']) . '!'));
}
2022-12-19 14:56:05 +01:00
return apcu_clear_cache();
2022-08-31 15:02:43 +02:00
}
2022-06-17 17:51:59 +02:00
2022-08-31 15:02:43 +02:00
/**
* Removes an item from the cache and returns
* whether the operation was successful
*/
public function remove(string $key): bool
{
return apcu_delete($this->key($key));
}
2022-06-17 17:51:59 +02:00
2022-08-31 15:02:43 +02:00
/**
* Internal method to retrieve the raw cache value;
* needs to return a Value object or null if not found
*/
2022-12-19 14:56:05 +01:00
public function retrieve(string $key): Value|null
2022-08-31 15:02:43 +02:00
{
2022-12-19 14:56:05 +01:00
$value = apcu_fetch($this->key($key));
return Value::fromJson($value);
2022-08-31 15:02:43 +02:00
}
2022-06-17 17:51:59 +02:00
2022-08-31 15:02:43 +02:00
/**
* Writes an item to the cache for a given number of minutes and
* returns whether the operation was successful
*
* <code>
* // put an item in the cache for 15 minutes
* $cache->set('value', 'my value', 15);
* </code>
*/
public function set(string $key, $value, int $minutes = 0): bool
{
2022-12-19 14:56:05 +01:00
$key = $this->key($key);
$value = (new Value($value, $minutes))->toJson();
$expires = $this->expiration($minutes);
return apcu_store($key, $value, $expires);
2022-08-31 15:02:43 +02:00
}
2022-06-17 17:51:59 +02:00
}