PHPackages                             bit-mx/cache-entities - 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. bit-mx/cache-entities

ActiveLibrary[Caching](/categories/caching)

bit-mx/cache-entities
=====================

Creates cacheable entities

2.0.0(2mo ago)24.4k↓85.3%1MITPHPPHP ^8.3

Since May 2Pushed 2mo agoCompare

[ Source](https://github.com/bit-mx/cache-entities)[ Packagist](https://packagist.org/packages/bit-mx/cache-entities)[ RSS](/packages/bit-mx-cache-entities/feed)WikiDiscussions master Synced today

READMEChangelog (9)Dependencies (18)Versions (10)Used By (0)

Cache Entities
==============

[](#cache-entities)

Manage your cache easily with Cache Entities.

Table of Contents
=================

[](#table-of-contents)

- [Introduction](#introduction)
- [Installation](#installation)
- [Compatibility](#compatibility)
- [Getting Started](#getting-started)
    - [Create a Cache Entity class](#create-a-cache-entity-class)
    - [Driver](#driver)
    - [Get the cached value](#get-the-cached-value)
- [Helper methods](#helper-methods)
    - [exists](#exists)
    - [doesNotExist](#doesnotexist)
    - [forget](#forget)
- [Memoization](#memoization)

Introduction
------------

[](#introduction)

Cache Entities is a package that allows you to manage your cache using a simple and clean API.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require bit-mx/cache-entities
```

Compatibility
-------------

[](#compatibility)

This package is compatible with Laravel 10.x and above.

Due laravel 11 requires php 8.2, this package is compatible with php 8.2 and above.

Getting Started
---------------

[](#getting-started)

### Create a Cache Entity class

[](#create-a-cache-entity-class)

To create a Cache Entity, you need to extend the CacheEntity class and implement the resolveKey, resolveTtl, and resolveValue methods.

```
namespace App\CacheEntities;

use BitMx\CacheEntities\CacheEntity;
use App\Models\User;
use Carbon\CarbonInterval;

class CurrentUserCache extends CacheEntity
{
    public function __construct(
        protected int $id,
    )
    {
    }

    protected function resolveKey(): string
    {
        return sprintf('current_user:%s', $this->id);
    }

    protected function resolveTtl(): CarbonInterval
    {
        return CarbonInterval::days(12);
    }

    protected function resolveValue(): mixed
    {
        return User::find($this->id);
    }
}
```

You can use the artisan command to create a new Cache Entity:

```
php artisan make:cache-entity CurrentUserCache
```

This command will create a new Cache Entity in the `app/cacheEntities` directory.

### Driver

[](#driver)

You can set the driver name overriding the resolveCacheStore method.

```
namespace App\CacheEntities;

use BitMx\CacheEntities\CacheEntity;
use App\Models\User;
use Carbon\CarbonInterval;

class CurrentUserCache extends CacheEntity
{

    protected function resolveCacheStore(): string
    {
        return 'redis';
    }
}
```

### Get the cached value

[](#get-the-cached-value)

To get the cached value, you can use the get method.

```
use App\CacheEntities\CurrentUserCache;

$cacheEntity = new CurrentUserCache(1);

$user = $cacheEntity->get();
```

Helper methods
--------------

[](#helper-methods)

You can use the following helper methods to work with the cache:

### exists

[](#exists)

The exists method returns true if the cache key exists, and false otherwise.

```
use App\CacheEntities\CurrentUserCache;

$cacheEntity = new CurrentUserCache(1);

$user = $cacheEntity->get();

if ($cacheEntity->exists()) {
    // The cache key exists
} else {
    // The cache key does not exist
}
```

### doesNotExist

[](#doesnotexist)

The doesNotExist method returns true if the cache key does not exist, and false otherwise.

```
use App\CacheEntities\CurrentUserCache;

$cacheEntity = new CurrentUserCache(1);

$user = $cacheEntity->get();

if ($cacheEntity->doesNotExist()) {
    // The cache key does not exist
} else {
    // The cache key exists
}
```

### forget

[](#forget)

The forget method removes the cache key.

```
use App\CacheEntities\CurrentUserCache;

$cacheEntity = new CurrentUserCache(1);

$user = $cacheEntity->get();

$cacheEntity->forget();
```

Memoization
-----------

[](#memoization)

By default, Cache Entities do not use memoization. However, you can enable it by using the `HasMemoization` trait in your Cache Entity class.

When memoization is enabled, the cache entity will use Laravel's `memo()` method to store the cached value in memory during the request lifecycle. This prevents redundant cache lookups for the same key within a single request, improving performance.

### Enabling Memoization

[](#enabling-memoization)

To enable memoization, simply add the `HasMemoization` trait to your Cache Entity class:

```
namespace App\CacheEntities;

use BitMx\CacheEntities\CacheEntity;
use BitMx\CacheEntities\Traits\HasMemoization;
use App\Models\User;
use Carbon\CarbonInterval;

class CurrentUserCache extends CacheEntity
{
    use HasMemoization;

    public function __construct(
        protected int $id,
    )
    {
    }

    protected function resolveKey(): string
    {
        return sprintf('current_user:%s', $this->id);
    }

    protected function resolveTtl(): CarbonInterval
    {
        return CarbonInterval::days(12);
    }

    protected function resolveValue(): mixed
    {
        return User::find($this->id);
    }
}
```

With memoization enabled:

- The first call to `get()` will fetch the value from cache or execute `resolveValue()`
- Subsequent calls to `get()` within the same request will return the memoized value from memory
- This avoids redundant cache lookups, improving performance when accessing the same cache entity multiple times

### Without Memoization

[](#without-memoization)

If you don't use the `HasMemoization` trait, each call to `get()` will perform a cache lookup, even within the same request.

###  Health Score

48

—

FairBetter than 93% of packages

Maintenance83

Actively maintained with recent releases

Popularity24

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 90% 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

Every ~88 days

Recently: every ~72 days

Total

9

Last Release

85d ago

Major Versions

1.2.1 → 2.0.02026-04-08

PHP version history (2 changes)1.0.0PHP ^8.2

2.0.0PHP ^8.3

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/37640480?v=4)[jenriquez](/maintainers/jenriquez)[@jenriquez](https://github.com/jenriquez)

---

Top Contributors

[![jenriquez-bit](https://avatars.githubusercontent.com/u/44071711?v=4)](https://github.com/jenriquez-bit "jenriquez-bit (9 commits)")[![jpatino-bit](https://avatars.githubusercontent.com/u/113532561?v=4)](https://github.com/jpatino-bit "jpatino-bit (1 commits)")

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/bit-mx-cache-entities/health.svg)

```
[![Health](https://phpackages.com/badges/bit-mx-cache-entities/health.svg)](https://phpackages.com/packages/bit-mx-cache-entities)
```

###  Alternatives

[spatie/laravel-responsecache

Speed up a Laravel application by caching the entire response

2.8k9.0M69](/packages/spatie-laravel-responsecache)[illuminate/cache

The Illuminate Cache package.

12937.0M1.8k](/packages/illuminate-cache)[mike-bronner/laravel-model-caching

Automatic caching for Eloquent models.

2.4k90.5k1](/packages/mike-bronner-laravel-model-caching)[iazaran/smart-cache

Smart Cache is a caching optimization package designed to enhance the way your Laravel application handles data caching. It intelligently manages large data sets by compressing, chunking, or applying other optimization strategies to keep your application performant and efficient.

21111.6k](/packages/iazaran-smart-cache)

PHPackages © 2026

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