index/src/Cache/ICacheProvider.php
2024-04-10 22:23:34 +00:00

71 lines
2.2 KiB
PHP

<?php
// ICacheProvider.php
// Created: 2024-04-10
// Updated: 2024-04-10
namespace Index\Cache;
use Index\ICloseable;
/**
* Represents a cache provider.
*/
interface ICacheProvider extends ICloseable {
/**
* Retrieve an item from the cache.
*
* @param string $key Key under which the value is stored.
* @return mixed value stored in the cache or null otherwise.
*/
function get(string $key): mixed;
/**
* Store an item in the cache.
*
* @param string $key Key under which to store the value.
* @param mixed $value Value to store.
* @param int $ttl Amount of seconds to store the value for, 0 for indefinite.
* @throws \RuntimeException if storing the value in the cache failed.
* @throws \InvalidArgumentException if an argument is incorrectly formatted, e.g. $ttl set to a negative value.
*/
function set(string $key, mixed $value, int $ttl = 0): void;
/**
* Deletes an item from the cache.
*
* @param string $key Key to be deleted.
* @throws \RuntimeException if an error occurred during deletion.
*/
function delete(string $key): void;
/**
* Sets a new expiration time on an item.
*
* @param string $key Key to be touched.
* @param int $ttl Amount of seconds to store the value for, 0 for indefinite.
* @throws \RuntimeException if storing the value in the cache failed.
* @throws \InvalidArgumentException if an argument is incorrectly formatted, e.g. $ttl set to a negative value.
*/
function touch(string $key, int $ttl = 0): void;
/**
* Increments an item.
*
* @param string $key Key to be incremented.
* @param int $amount Amount to increment by. Defaults to 1.
* @throws \RuntimeException if storing the value in the cache failed.
* @return int new, incremented value.
*/
function increment(string $key, int $amount = 1): int;
/**
* Decrements an item.
*
* @param string $key Key to be decremented.
* @param int $amount Amount to decrement by. Defaults to 1.
* @throws \RuntimeException if storing the value in the cache failed.
* @return int new, decremented value.
*/
function decrement(string $key, int $amount = 1): int;
}