HEX
Server: LiteSpeed
System: Linux sv4.hami.host 5.14.0-611.54.3.el9_7.x86_64 #1 SMP PREEMPT_DYNAMIC Thu May 7 16:31:24 EDT 2026 x86_64
User: armgroup (1008)
PHP: 8.2.32
Disabled: show_source, system, shell_exec, passthru, exec, popen, proc_open, mail, socket_create, socket_create_listen, socket_create_pair, link, dl, openlog, syslog, stream_socket_server, curl_multi_init
Upload Files
File: /home/armgroup/libs/vendor/chillerlan/php-cache/src/APCUCache.php
<?php
/**
 * Class APCUCache
 *
 * @created      27.05.2017
 * @author       Smiley <smiley@chillerlan.net>
 * @copyright    2017 Smiley
 * @license      MIT
 *
 * @noinspection PhpComposerExtensionStubsInspection
 */

declare(strict_types=1);

namespace chillerlan\SimpleCache;

use DateInterval;
use function apcu_clear_cache, apcu_delete, apcu_fetch, apcu_store, implode, is_array, is_bool, sprintf;

/**
 * Implements a cache via the APCu extension
 *
 * @see https://www.php.net/manual/en/book.apcu.php
 * @see https://github.com/krakjoe/apcu
 */
class APCUCache extends CacheDriverAbstract{

	/** @inheritdoc */
	public function get(string $key, mixed $default = null):mixed{
		$value = apcu_fetch($this->checkKey($key));

		if($value !== false){
			return $value;
		}

		return $default;
	}

	/**
	 * @inheritdoc
	 * @throws \Psr\SimpleCache\CacheException
	 */
	public function set(string $key, mixed $value, int|DateInterval|null $ttl = null):bool{
		$ret = apcu_store($this->checkKey($key), $value, ($this->getTTL($ttl) ?? 0));

		if(is_bool($ret)){
			return $ret;
		}

		if(is_array($ret)){
			throw new CacheException(sprintf('error keys: %s', implode(', ', $ret)));
		}

		throw new CacheException('unknown apcu_store() error');
	}

	/**
	 * @inheritdoc
	 * @throws \chillerlan\SimpleCache\CacheException
	 */
	public function delete(string $key):bool{
		$ret = apcu_delete($this->checkKey($key));

		if(is_bool($ret)){
			return $ret;
		}

		if(is_array($ret)){
			throw new CacheException(sprintf('error keys: %s', implode(', ', $ret)));
		}

		throw new CacheException('unknown apcu_delete() error');
	}

	/** @inheritdoc */
	public function clear():bool{
		return apcu_clear_cache();
	}

}