PHPackages                             davealex/laravel-service-caching - 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. davealex/laravel-service-caching

ActiveLibrary[Caching](/categories/caching)

davealex/laravel-service-caching
================================

A simple strategy for managing data services caching in Laravel applications

v0.1.0-alpha(8mo ago)10MITPHPPHP ^8.3.0CI failing

Since Oct 21Pushed 8mo agoCompare

[ Source](https://github.com/davealex/laravel-service-caching)[ Packagist](https://packagist.org/packages/davealex/laravel-service-caching)[ Docs](https://github.com/davealex/laravel-service-caching)[ RSS](/packages/davealex-laravel-service-caching/feed)WikiDiscussions main Synced today

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

[![Latest Version on Packagist](https://camo.githubusercontent.com/38f6082841bdbedd6da80b05fa3af78e69e08a89eafd1f4ce306a454fda03a3d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f64617665616c65782f6c61726176656c2d736572766963652d63616368696e672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/davealex/laravel-service-caching)[![Total Downloads](https://camo.githubusercontent.com/8b0e0d1c7860a6a7248a446e4105482a3e44439a64db37624b0d2a8267dfacab/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f64617665616c65782f6c61726176656c2d736572766963652d63616368696e672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/davealex/laravel-service-caching)[![GitHub Actions](https://github.com/davealex/laravel-service-caching/actions/workflows/main.yml/badge.svg)](https://github.com/davealex/laravel-service-caching/actions/workflows/main.yml/badge.svg)

**Laravel Service Caching**
===========================

[](#laravel-service-caching)

A simple, fluent, and driver-agnostic caching layer for your **Laravel service classes**. This package provides an easy way to cache the results of your service methods, automatically handling cache key generation based on request parameters and user context.

It intelligently detects whether your configured cache driver supports tags and falls back to a manual key-tracking system if it doesn't, making it safe to use with drivers like file or database.

**Installation**
----------------

[](#installation)

You can install the package via composer:

`composer require davealex/laravel-service-caching`

**Configuration**
-----------------

[](#configuration)

Publish the configuration file with this command:

`php artisan vendor:publish --provider="Davealex\LaravelServiceCaching\LaravelServiceCachingServiceProvider" --tag="config"`

This will create a `config/laravel-service-caching.php` file in your application, allowing you to configure the default behavior of the package.

```
// config/laravel-service-caching.php

return [
    // The default cache driver to use. 'null' will use Laravel's default.
    'driver' => env('SERVICE_CACHE_DRIVER'),

    // The default cache duration in seconds. (Default: 600 seconds / 10 minutes). Pass `duration` as **null or 0** in the options array to cache the result forever
    'cache_duration_in_seconds' => 600,

    // The attribute on the User model to use for unique user-based caching.
    'user_identifier_key' => 'id',
];

```

**Usage**
---------

[](#usage)

### **1. Implement the Contract**

[](#1-implement-the-contract)

First, your service class must implement the `Davealex\LaravelServiceCaching\Contracts\CacheableServiceInterface`. This is a simple **marker interface** that doesn't require you to implement any methods.

```
