Download the PHP package apricity/micro-cache without Composer
On this page you can find all versions of the php package apricity/micro-cache. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package micro-cache
Micro-Cache
MicroCache is a lightweight and simple in-memory caching solution that allows you to store key-value pairs in memory. It is particularly useful for caching data that is expensive to generate or retrieve from external sources.
Class provides a straightforward approach to caching data in memory, allowing for efficient storage and retrieval of cached items.
Installation
Table of contents
- Usage
- API
- MicroCache::set
- MicroCache::get
- MicroCache::delete
- MicroCache::clear
- MicroCache::has
- Inheritance
- API
- Tests
- Contributing
- Changelog
- License
MicroCache::set
Store an item in the cache.
This method stores an item in the cache under the specified key. If the key already exists, its value will be overwritten with the new value. If the TTL (time-to-live) parameter is provided and is greater than zero, the item will expire and be removed from the cache after the specified number of seconds. If the TTL is zero, the item will be cached indefinitely.
Parameters:
mixed $key
: The key under which to store the item. Can be of any type except Closure.mixed $value
: The value to store.int $ttl
: Time-to-live in seconds. If zero, the item is cached indefinitely.
Returns: string|int
- The key under which the item is stored.
Throws: InvalidArgumentException
- If the key is callable or if the key is empty and not zero or false.
Example
MicroCache::get
Retrieve an item value from the cache.
This method retrieves the value of an item stored in the cache under the specified key. If the item exists in the cache and has not expired, its value will be returned. If the item does not exist or has expired, null will be returned.
Parameters:
mixed $key
: The key under which to store the item. Can be of any type except Closure.
Returns: mixed|null
- The cached item value or null if the key doesn't exist or has expired.
Throws: InvalidArgumentException
- If the key is callable or if the key is empty and not zero or false.
Example
MicroCache::delete
Delete an item from the cache.
This method removes the item stored in the cache under the specified key. If the item exists in the cache, it will be deleted. If the item does not exist, no action will be taken.
Parameters:
mixed $key
: The key under which to store the item. Can be of any type except Closure.
Returns: void
Throws: InvalidArgumentException
- If the key is callable or if the key is empty and not zero or false.
Example
MicroCache::clear
Clear all items from the cache.
This method clears all items stored in the cache, effectively resetting the cache to an empty state.
Returns: void
Example
MicroCache::has
Check if an item exists in the cache and is not expired.
This method checks whether the specified key exists in the cache and if it is not expired. If the item exists and is not expired, it returns true; otherwise, it returns false.
Parameters:
mixed $key
: The key under which to store the item. Can be of any type except Closure.
Returns: bool
- True if the item exists and is not expired, false otherwise.
Throws: InvalidArgumentException
- If the key is callable or if the key is empty and not zero or false.
Example
Inheritance and creation of a unique cache
Shared cache
Unique cache
Run tests
Contributing
We welcome contributions from the community! For guidelines on how to contribute, please refer to the CONTRIBUTING.md file.
License
This project is licensed under the BSD 3-Clause License - see the LICENSE file for details.
The repository has been migrated from GitLab.