PHPackages                             programmernomad/lscache-laravel - 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. programmernomad/lscache-laravel

ActiveLibrary[Caching](/categories/caching)

programmernomad/lscache-laravel
===============================

LSCache implementation for Laravel with modern Laravel and OpenLiteSpeed support

13↓88.9%PHP

Since Jun 2Pushed 1mo agoCompare

[ Source](https://github.com/ProgrammerNomad/lscache-laravel)[ Packagist](https://packagist.org/packages/programmernomad/lscache-laravel)[ RSS](/packages/programmernomad-lscache-laravel/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependenciesVersions (1)Used By (0)

Laravel LSCache
---------------

[](#laravel-lscache)

This package allows you to use LSCache together with Laravel. It works with both **LiteSpeed Web Server** (all editions, including the free OpenLiteSpeed) and the commercial **LiteSpeed Enterprise** server.

It provides two middlewares and one facade:

- LSCache Middleware to control the cache-control header for LiteSpeed LSCache
- LSTags Middleware to control the tag header for LiteSpeed LSCache
- LSCache facade to handle purging

Requirements
------------

[](#requirements)

Package versionLaravelPHP2.x10 – 13≥ 8.1Installation
------------

[](#installation)

Require this package using composer.

```
composer require programmernomad/lscache-laravel

```

Laravel uses Auto-Discovery, so you won't have to make any changes to your application, the two middlewares and facade will be available right from the beginning.

#### Steps for Laravel 5.5 – 10 (legacy)

[](#steps-for-laravel-55--10-legacy)

You should publish the package configuration, which allows you to set the defaults for the `X-LiteSpeed-Cache-Control` header:

```
php artisan vendor:publish --provider="Litespeed\LSCache\LSCacheServiceProvider"

```

#### Steps for Laravel 11 and above

[](#steps-for-laravel-11-and-above)

Laravel 11 removed `app/Http/Kernel.php` and moved middleware registration to `bootstrap/app.php`. This package registers its global middleware automatically via the service provider, so **no manual kernel edits are needed**. Publishing the config is still optional but recommended:

```
php artisan vendor:publish --provider="Litespeed\LSCache\LSCacheServiceProvider"

```

### Enable CacheLookup — LiteSpeed Web Server (Enterprise)

[](#enable-cachelookup--litespeed-web-server-enterprise)

To enable CacheLookup for LiteSpeed Cache, add the following to your `.htaccess` (or at server/vhost level in the LiteSpeed admin):

```

   CacheLookup on

```

### Enable CacheLookup — OpenLiteSpeed

[](#enable-cachelookup--openlitespeed)

OpenLiteSpeed supports the same LSCache response headers (`X-LiteSpeed-Cache-Control`, `X-LiteSpeed-Tag`, `X-LiteSpeed-Purge`) as the commercial server, so this package works with OpenLiteSpeed without any code changes.

**Required steps in the OpenLiteSpeed admin panel:**

1. Log in to the OLS WebAdmin console (default: `https://your-server:7080`).
2. Go to **Server** → **Cache** and set **Enable Cache** to `Yes`. Save and perform a graceful restart.
3. Optionally configure **Cache Storage Path**, **Default Cache TTL**, etc. in the same section.
4. Add the `CacheLookup` directive to your `.htaccess` (OLS honours `.htaccess` files):

```

   CacheLookup on

```

Or enable it at the virtual-host level via **Virtual Hosts** → (your vhost) → **Cache** → **Enable Cache** = `Yes`.

> **Note:** Cache purge via `X-LiteSpeed-Purge` response headers works in OpenLiteSpeed the same way as in LiteSpeed Enterprise — no extra configuration is needed beyond enabling the cache module.

Usage
-----

[](#usage)

The package comes with 3 functionalities: Setting the cache control headers for lscache, settings specific tags and purging.

### cache-control

[](#cache-control)

You'll be able to configure defaults in the `config/lscache.php` file, here you can set the max-age (`default_ttl`), the cacheability (`default_cacheability`) such as public, private or no-cache or enable esi (`esi`) in the `X-LiteSpeed-Cache-Control` response header.

If the `default_ttl` is set to `0`, then we won't return the `X-LiteSpeed-Cache-Control` response header.

You can control the config settings in your `.env` file as such:

- `LSCACHE_ESI_ENABLED` - accepts `true` or `false` to whether you want ESI enabled or not globally; Default `false`
- `LSCACHE_DEFAULT_TTL` - accepts an integer, this value is in seconds; Default: `0`
- `LSCACHE_DEFAULT_CACHEABILITY` - accepts a string, you can use values such as `private`, `no-cache`, `public` or `no-vary`; Default: `no-cache`
- `LSCACHE_GUEST_ONLY` - accepts `true` or `false` to decide if the cache should be enabled for guests only; Defaults to `false`

You set the cache-control header for lscache using a middleware, so we can in our routes do something like this:

```
Route::get('/', function() {
    return view('frontpage');
});

Route::get('/about-us', function() {
    return view('about-us');
})->middleware('lscache:max-age=300;public');

Route::get('/contact', function() {
    return view('contact');
})->middleware('lscache:max-age=10;private;esi=on');

Route::get('/admin', function() {
    return view('admin');
})->middleware('lscache:no-cache');
```

Below is 4 examples:

- the `/` route will use the default X-LiteSpeed-Cache-Control header that you've configured in `config/lscache.php`.
- the `/about-us` route sets a max-age of 300 seconds as well as setting the cacheability to `public`, keep in mind you'll use semi-colon (`;`) to separate these values.
- the `/contact` route uses a max-age of 10 seconds, uses private cacheability and turns ESI on. Turning ESI on, allows you to use `` within your blade templates and these will be parsed by the ESI engine in LiteSpeed Web Server.
- the `/admin` route will never be cached by setting a `X-LiteSpeed-Cache-Control: no-cache` -header.

Now, you'll also be able to apply the same middleware to route groups in Laravel, let's take an example:

```
Route::group(['prefix' => 'admin', 'middleware' => ['lscache:private;esi=on;max-age=120']], function() {
    Route::get('/dashboard', function() {
        return view('dashboard');
    });

    Route::get('/stats', function() {
        return view('stats');
    })->middleware('lscache:no-cache');
});
```

In the above case, we've set the whole `admin` group to be private with esi enabled and a max-age of 120 seconds, however in the `/admin/stats` route, we override the `X-LiteSpeed-Cache-Control` header to `no-cache`.

### tags

[](#tags)

You're also able to set tags for LSCache using the `lstags` middleware. If we use the previous example of our `admin` route group:

```
Route::group(['prefix' => 'admin', 'middleware' => ['lscache:private;esi=on;max-age=900', 'lstags:admin']], function() {
    Route::get('/dashboard', function() {
        return view('dashboard');
    });

    Route::get('/users', function() {
        return view('users');
    });
});
```

Here we've added the `lstags:admin` middleware, this means that the cache will get tagged with an `admin` tag, so when we later want to purge the cache, we can target all admin pages using the tag `admin`.

You can also do more complex tags as such:

```
Route::get('/view', function() {
    return view('view');
})->middleware(['lscache:private', 'lstags:public:pubtag1;public:pubtag2;public:pubtag3;privtag1;privtag2']);
```

### purge

[](#purge)

If we have an admin interface that controls for example a blog, when you publish a new article, you might want to purge the frontpage of the blog so the article appears in the overview.

You'd do this in your controller by doing

```
