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

1.2.1(6mo ago)24.0k↓42.9%1MITPHPPHP ^8.2

Since May 2Pushed 6mo 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 1mo ago

READMEChangelog (8)Dependencies (9)Versions (9)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

43

—

FairBetter than 91% of packages

Maintenance66

Regular maintenance activity

Popularity24

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 88.9% 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 ~78 days

Recently: every ~131 days

Total

8

Last Release

196d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/1ec257b9dda4e1e687cdb07de95577588d5804841fc56089bfc01b7f70a2aa77?d=identicon)[jenriquez](/maintainers/jenriquez)

---

Top Contributors

[![jenriquez-bit](https://avatars.githubusercontent.com/u/44071711?v=4)](https://github.com/jenriquez-bit "jenriquez-bit (8 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.8k8.2M51](/packages/spatie-laravel-responsecache)[genealabs/laravel-model-caching

Automatic caching for Eloquent models.

2.4k4.8M26](/packages/genealabs-laravel-model-caching)[mikebronner/laravel-model-caching

Automatic caching for Eloquent models.

2.4k127.1k1](/packages/mikebronner-laravel-model-caching)[illuminate/cache

The Illuminate Cache package.

12835.6M1.4k](/packages/illuminate-cache)[laragear/cache-query

Remember your query results using only one method. Yes, only one.

272122.8k](/packages/laragear-cache-query)[namoshek/laravel-redis-sentinel

An extension of Laravels Redis driver which supports connecting to a Redis master through Redis Sentinel.

38679.0k](/packages/namoshek-laravel-redis-sentinel)

PHPackages © 2026

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