PHPackages                             adamcrampton/object-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. adamcrampton/object-cache

ActiveLibrary[Caching](/categories/caching)

adamcrampton/object-cache
=========================

Redis object cache for Laravel

v4.2.6(5y ago)01.3kMITPHPPHP &gt;=7CI failing

Since Nov 19Pushed 5y ago1 watchersCompare

[ Source](https://github.com/adamcrampton/object-cache)[ Packagist](https://packagist.org/packages/adamcrampton/object-cache)[ Docs](https://github.com/adamcrampton/object-cache)[ RSS](/packages/adamcrampton-object-cache/feed)WikiDiscussions master Synced 6d ago

READMEChangelog (10)Dependencies (5)Versions (39)Used By (0)

object-cache
============

[](#object-cache)

This Laravel package will allow you to set and forget objects in your Redis cache, with automated Middleware and convenient pre-bundled TTL settings. All you need to supply is a TTL, key name, and method for regenerating the value.

Upgrading from v3.x.x
---------------------

[](#upgrading-from-v3xx)

If upgrading from v3, you will need to make the following changes to your implementation:

- Create a PHP class for storing your methods and method configs, and migrate the `$this->objects` array and your cache methods across. Be sure to move across any dependencies to the new class.
- Add a `methodClass` key to your `config/object_cache.php` file, and set that value to the fully namespaced class name you created in the previous step.
- Delete your object cache Middleware.
- Change the Middleware settings in `app/Http/Kernel.php` to use the package Middleware: `\AdamCrampton\ObjectCache\Middleware\CheckObjectCache::class`
- See the Configuration section below for additional config options you may not have set.

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

[](#installation)

- Run `composer require adamcrampton/object-cache` in your project directory
- Add library service provider `AdamCrampton\ObjectCache\ObjectCacheServiceProvider::class` and facade `'ObjectCache' => AdamCrampton\ObjectCache\ObjectCacheFacade::class` to `config/app.php`
- Create a class for storing your methods, adding use statement for `AdamCrampton\ObjectCache\ObjectCache`
- Add the package Middleware `\AdamCrampton\ObjectCache\Middleware\CheckObjectCache::class` to `app/Http/Kernel.php`
- Configure your routes to use middleware - see
- Run `php artisan vendor:publish` - this will add a `object_cache.php` file to your project's config directory
- Add your Redis host to the app's .env (or localhost for local dev - ensure your environment has Redis installed)

Configuration
-------------

[](#configuration)

Configuration can be found in the published `config/object_cache.php` file.

- Edit the `parameters` and `options` values for the Redis connection - see
- Add the class name with full namespacing to the `methodClass` key
- The settings under `fallback` relate to the Get Helper (details further down). If you would like the library to manually fetch the data if the Redis get fails, set this to true. The `passRequest` option forces the helper to pass the request object into the constructor of your cache method class (only set to true if this is necessary for your application)
- Setting `logErrors` to true will enable the Get Helper to log any failed attempts to get values from Redis and/or fallback attempts

Usage
-----

[](#usage)

To initialise a connection:

- Add `use AdamCrampton\ObjectCache\ObjectCache` to the class
- Declare a variable to use for your connection, e.g. `$redis = ObjectCache::init()`

Once initialised, you can use all Predis commands. See:

-
-

Middleware
----------

[](#middleware)

The CheckObjectCache Middleware that ships with the package allows you to add cache objects to be automatically checked on each request, and if missing, set. You will need to add an `$this->objects` array (configuration) and each corresponding method to your cache store class (mentioned above in installation).

An array of 3 values are required for each item in the `$this->objects` array. They are:

- **cacheKey:** Name of the key for your object in the Redis store.
- **cacheTtl:** Using either a pre-defined named TTL (see below), or a TTL in seconds.
- **cacheMethod:** The name of the method to regenerate the cache data if not found (i.e. if it is missing or expired).

An example of an implementation using a cache store class:

```

use App\Models\PartPrice;
use AdamCrampton\ObjectCache\ObjectCache;

class CacheMethodStore
{
    public $objects;

    public function __construct()
    {
        $this->objectCache = new ObjectCache();

        $this->objects = [
            [
                'cacheKey' => 'exampleKey',
                'cacheTtl' => $this->objectCache->ttl['hours']['twentyFour'],
                'cacheMethod' => 'setExample'
            ]
        ];
    }

    public function setExample()
    {
        return PartPrice::select(['part_id', 'price'])
            ->get()
            ->toJson();
    }
}

```

Get Helper
----------

[](#get-helper)

Once initialised, you can fetch an object from the cache using the provided helper. The two parameters you need to pass in are the key and JSON decode option.

If you are passing in the decode option, set this to 'array' or 'object' for the correct return format. Example:

```
    /**
     * Fetch part data for the front end.
     *
     * @return array
     */
    protected function buildPartData(Request $request)
    {
        return ObjectCache::get('dealer_123456_part_data', 'array');
    }

```

Pre-configured TTL
------------------

[](#pre-configured-ttl)

A convenience array of TTL values are included with the package. To use one of the values, initalise the Redis connection as mentioned above, and call the `ttl` property with the appropriate key name. Structure of this TTL array is as follows:

```
ttl">```
    'default' => [
        'default' => 86400
    ],
    'apc' => [
        'default' => 86400
    ],
    'array' => [
        'default' => 86400
    ],
    'database' => [
        'default' => 86400
    ],
    'file' => [
        'default' => 86400
    ],
    'memcached' => [
        'default' => 86400
    ],
    'redis' => [
        'default' => 86400
    ],
    'minutes' => [
        'one' => 60,
        'five' => 300,
        'ten' => 600,
        'fifteen' => 900,
        'twenty' => 1200,
        'thirty' => 1800,
        'forty' => 2400,
        'fortyFive' => 2700,
        'fifty' => 3000,
        'sixty' => 3600
    ],
    'hours' => [
        'one' => 3600,
        'two' => 7200,
        'three' => 10800,
        'four' => 14400,
        'five' => 18000,
        'six' => 21600,
        'seven' => 25200,
        'eight' => 28800,
        'nine' => 32400,
        'ten' => 36000,
        'eleven' => 39600,
        'twelve' => 43200,
        'twentyFour' => 86400
    ],
    'days' => [
        'one' => 86400,
        'two' => 172800,
        'three' => 259200,
        'four' => 345600,
        'five' => 432000,
        'six' => 518400,
        'seven' => 604800,
        'fourteen' => 1209600,
        'twentyEight' => 2419200
    ],
    'weeks' => [
        'one' => 604800,
        'two' => 1209600,
        'three' => 1814400,
        'four' => 2419200,
        'five' => 3024000,
        'six' => 3628800
    ]
];

```
```

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity69

Established project with proven stability

 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

Every ~6 days

Recently: every ~43 days

Total

38

Last Release

2147d ago

Major Versions

v0.1.0 → v1.0.02019-11-19

v1.0.1 → v2.0.02019-11-20

v2.3.8 → v3.0.02019-12-08

v3.1.1 → v4.0.02019-12-19

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/5696904?v=4)[Adam Crampton](/maintainers/adamcrampton)[@adamcrampton](https://github.com/adamcrampton)

---

Top Contributors

[![adamcrampton](https://avatars.githubusercontent.com/u/5696904?v=4)](https://github.com/adamcrampton "adamcrampton (60 commits)")

---

Tags

middlewarelaravelrediscache

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/adamcrampton-object-cache/health.svg)

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

###  Alternatives

[awssat/laravel-visits

Laravel Redis visits counter for Eloquent models

975163.6k2](/packages/awssat-laravel-visits)[byerikas/cache-tags

Allows for Redis/Valkey cache flushing multiple tagged items by a single tag.

1413.9k](/packages/byerikas-cache-tags)

PHPackages © 2026

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