PHPackages                             apricity/micro-cache - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Caching](/categories/caching)
4. /
5. apricity/micro-cache

ActiveLibrary[Caching](/categories/caching)

apricity/micro-cache
====================

Simple in-memory caching mechanism for PHP applications.

v1.10.2(1y ago)0221BSD-3-ClausePHPPHP &gt;=8.0

Since Jun 8Pushed 1y ago1 watchersCompare

[ Source](https://github.com/weroro-sk/apricity-micro-cache)[ Packagist](https://packagist.org/packages/apricity/micro-cache)[ RSS](/packages/apricity-micro-cache/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (1)Versions (2)Used By (1)

Micro-Cache
===========

[](#micro-cache)

[![Latest Stable Version](https://camo.githubusercontent.com/bffe15bd30d3e6abbe8363bd324e0b1958fd8067ec1a862be22963fb8bb563fb/687474703a2f2f706f7365722e707567782e6f72672f61707269636974792f6d6963726f2d63616368652f76)](https://packagist.org/packages/apricity/micro-cache)[![PHP Version Require](https://camo.githubusercontent.com/6f8cf62d16ae098afb7d7f2563692500194c0d5fa55872db0d2a1abd8a881cb2/687474703a2f2f706f7365722e707567782e6f72672f61707269636974792f6d6963726f2d63616368652f726571756972652f706870)](https://packagist.org/packages/apricity/micro-cache)[![License](https://camo.githubusercontent.com/46c36e5ce8b36872958872342efdd76d31ea1410e4581b4a9de1f7c5911ff05a/687474703a2f2f706f7365722e707567782e6f72672f61707269636974792f6d6963726f2d63616368652f6c6963656e7365)](LICENSE)

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
------------

[](#installation)

```
composer require apricity/micro-cache
```

Table of contents
-----------------

[](#table-of-contents)

1. Usage
    - API
        - [MicroCache::set](#microcacheset)
        - [MicroCache::get](#microcacheget)
        - [MicroCache::delete](#microcachedelete)
        - [MicroCache::clear](#microcacheclear)
        - [MicroCache::has](#microcachehas)
    - [Inheritance](#inheritance-and-creation-of-a-unique-cache)
2. [Tests](#run-tests)
3. [Contributing](#contributing)
4. [Changelog](CHANGELOG.md)
5. [License](#license)

---

### MicroCache::set

[](#microcacheset)

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.

```
public static function set(mixed $key, mixed $value, int $ttl = 0): string|int
```

**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

[](#example)

```
$key = MicroCache::set('my_key', 'my_value', 3600);
```

---

### MicroCache::get

[](#microcacheget)

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.

```
public static function get(mixed $key): mixed
```

**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

[](#example-1)

```
$value = MicroCache::get('my_key');
if (is_null($value)) {
    echo "Cache miss";
} else {
    echo "Cache value: " . $value;
}
```

---

### MicroCache::delete

[](#microcachedelete)

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.

```
public static function delete(mixed $key): void
```

**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

[](#example-2)

```
MicroCache::delete('my_key');
```

---

### MicroCache::clear

[](#microcacheclear)

Clear all items from the cache.

This method clears all items stored in the cache, effectively resetting the cache to an empty state.

```
public static function clear(): void
```

**Returns**: `void`

#### Example

[](#example-3)

```
MicroCache::clear();
```

---

### MicroCache::has

[](#microcachehas)

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.

```
public static function has(mixed $key): bool
```

**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

[](#example-4)

```
if (MicroCache::has('my_key')) {
    echo "Cache exists";
} else {
    echo "Cache does not exist or has expired";
}
```

---

Inheritance and creation of a unique cache
------------------------------------------

[](#inheritance-and-creation-of-a-unique-cache)

#### Shared cache

[](#shared-cache)

```
use Apricity\MicroCache;

class SharedCache extends MicroCache {}

SharedCache::set('shared_cache_key', 'shared_value');

SharedCache::get('shared_cache_key'); // shared_value
MicroCache::get('shared_cache_key'); // shared_value

MicroCache::set('cache_key', 'cached_value');

SharedCache::get('cache_key'); // cached_value
MicroCache::get('cache_key'); // cached_value
```

#### Unique cache

[](#unique-cache)

```
use Apricity\MicroCache;

class UniqueCache extends MicroCache {
    protected static array $microCache = []; // Non-shared unique cache.
}

UniqueCache::set('unique_cache_key', 'unique_value');

UniqueCache::get('unique_cache_key'); // unique_value
MicroCache::get('unique_cache_key'); // null

MicroCache::set('cache_key', 'cached_value');

UniqueCache::get('cache_key'); // null
MicroCache::get('cache_key'); // cached_value
```

---

### Run tests

[](#run-tests)

```
composer test
```

---

Contributing
------------

[](#contributing)

We welcome contributions from the community! For guidelines on how to contribute, please refer to the [CONTRIBUTING.md](CONTRIBUTING.md) file.

---

License
-------

[](#license)

This project is licensed under the BSD 3-Clause License - see the [LICENSE](LICENSE) file for details.

---

The repository has been migrated from GitLab.

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance31

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity45

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

708d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/6fb5f475e10d38a5a2de402a087594342d6be9bd42797d5d4819f33772009087?d=identicon)[weroro-sk](/maintainers/weroro-sk)

---

Top Contributors

[![weroro-sk](https://avatars.githubusercontent.com/u/29892165?v=4)](https://github.com/weroro-sk "weroro-sk (14 commits)")

---

Tags

phpperformancecachecachingmemoryIn Memoryweroro

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/apricity-micro-cache/health.svg)

```
[![Health](https://phpackages.com/badges/apricity-micro-cache/health.svg)](https://phpackages.com/packages/apricity-micro-cache)
```

###  Alternatives

[alekseykorzun/memcached-wrapper-php

Optimized PHP 5 wrapper for Memcached extension that supports dog-piling, igbinary and local storage

2984.6k1](/packages/alekseykorzun-memcached-wrapper-php)[voku/simple-cache

Simple Cache library

322.5M7](/packages/voku-simple-cache)[silverstripe/staticpublishqueue

Static publishing queue to create static versions of pages for enhanced performance and security

45135.4k4](/packages/silverstripe-staticpublishqueue)[rapidwebltd/rw-file-cache

RW File Cache is a PHP File-based Caching Library. Its syntax is designed to closely resemble the PHP memcache extension.

15191.3k7](/packages/rapidwebltd-rw-file-cache)[bnomei/kirby3-redis-cachedriver

Advanced Redis cache-driver with in-memory store, transactions and preloading

101.7k](/packages/bnomei-kirby3-redis-cachedriver)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
