PHPackages                             joostvanveen/litespeedcache - 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. joostvanveen/litespeedcache

ActiveLibrary[Caching](/categories/caching)

joostvanveen/litespeedcache
===========================

A framework agnostic Litespeed cache library for PHP. Can be used in any PHP application.

v1.4.0(7y ago)79.6k—0%11MITPHPPHP &gt;=7.1

Since Apr 14Pushed 7y ago2 watchersCompare

[ Source](https://github.com/joostvanveen/litespeedcache)[ Packagist](https://packagist.org/packages/joostvanveen/litespeedcache)[ RSS](/packages/joostvanveen-litespeedcache/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (17)Used By (1)

[![coverage](https://camo.githubusercontent.com/d7d303402b4a01caa020d99076945bb0f6c078652cf15b43eeba11551bd42da6/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f636f7665726167652d39392532352d79656c6c6f77677265656e2e7376673f63616368655365636f6e64733d33363030)](https://camo.githubusercontent.com/d7d303402b4a01caa020d99076945bb0f6c078652cf15b43eeba11551bd42da6/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f636f7665726167652d39392532352d79656c6c6f77677265656e2e7376673f63616368655365636f6e64733d33363030)

joostvanveen/litespeedcache
===========================

[](#joostvanveenlitespeedcache)

A framework agnostic Litespeed cache library for PHP. Can be used in any PHP application.

If you are looking for the Laravel implementation, see

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

[](#installation)

Require the package using composer:

```
composer require joostvanveen/litespeedcache

```

Enable the Litespeed in your .htaccess file.

```

    # Enable public cache
    CacheEnable public /

    # Enable private cache if you need to
    CacheEnable private /

    # Check the public cache
    CacheLookup public on

    # Ignore normal Cache Control headers
    CacheIgnoreCacheControl On

    # Maximum expiration time in seconds
    CacheMaxExpire 604800

```

Usage
-----

[](#usage)

The package does not cache:

- any requests other than GET and HEAD
- any ajax requests
- any CLI requests

The cache also

- can be enabled or disabled
- can contain one or multiple tags
- can contain vary headers
- can contain an array of blacklisted URIs that should bot be cached
- can contain an array of blacklisted query strings thta should not be cached

### Default configuration values

[](#default-configuration-values)

- type = public
- enabled = true (cache is enabled)
- lifetime = 120 (2 hours)
- bypass cookiename = cache\_bypass (see [Bypassing the cache](#bypassing-the-cache))
- bypass querystring = cache\_bypass (see [Bypassing the cache](#bypassing-the-cache))

### Caching the current URL

[](#caching-the-current-url)

```
use Joostvanveen\Litespeedcache\Cache;

[...]

// Cache the current URL as public with a cache lifetime of 120 minutes
$cache = new Cache;
$cache->cache(); // use default type and lifetime

// You can also use new Cache directly, like so:
(new Cache)->cache(); // use default type and lifetime
(new Cache)->cache('private', 360); // use explicit type and lifetime

// You can also use the setType() and setLifetime() methods
(new Cache)->setEnabled(true)->setType('private')->setLifetime(3600)->cache();

// If the lifetime is set to 0 the page will not be cached
(new Cache)->setEnabled(true)->setLifetime(0)->cache();
```

### Caching a specific URL

[](#caching-a-specific-url)

This can be used for warming the cache, for instance.

```
// Warm the cache for these URIs
$uris = [
    '/',
    '/about-us',
    '/contact',
];

$cache = (new \Joostvanveen\Litespeedcache\Cache);
foreach($uris as $uri) {
    $cache->cache('public', 120, $uri);
}
```

### Excluding URIs from cache

[](#excluding-uris-from-cache)

A URI is the request path without the query string. in `https://example.com/foo?bar=baz`, the URI is `/foo`.

A URL will not be cached if it matches any of the URIs set as excluded. Excluded URIs can contain wildcards.

In the following example, the URI '/checkout/step/1' would not be cached.

```
$excludedUris = [
    'checkout*',
    'admin*',
];
(new \Joostvanveen\Litespeedcache\Cache)->setExcludedUrls($excludedUris)->cache('public', 120);
```

### Caching ajax requests

[](#caching-ajax-requests)

By default, ajax requests are not cached. But you can set this the following way:

```
(new Cache)->setEnableAjaxCache(true)->cache();
```

### Caching HTTP request methods

[](#caching-http-request-methods)

By default, only GET and HEAD requests are cached. But you can set this the following way:

```
$requestTypes = ['GET', 'HEAD', 'POST', 'PUT'];
(new Cache)->setCacheableHttpVerbs($requestTypes)->cache();
```

### Excluding query string from cache

[](#excluding-query-string-from-cache)

A query string consists of the parameters added to a URL after the question mark. In `https://example.com/foo?bar=baz`, the query string is `bar=baz`

A URL with a query string will not be cached if the query string matches any of the URIs set as excluded. The excluded query strings can contain wildcards.

In the following example, the URL `https://example.com/search?query=foo&page=1&direction=desc` would not be cached.

```
$excludedQueryStrings = [
    '*direction=*',
];
(new \Joostvanveen\Litespeedcache\Cache)->setExcludedQueryStrings($excludedQueryStrings)->cache('public', 120);
```

### Adding tags to the cache

[](#adding-tags-to-the-cache)

You can add one or more tags to the current URL that is cached. You can use these tags to flush all caches containing those tags at once.

By default, `addTags()` takes an array of tags.

```
(new \Joostvanveen\Litespeedcache\Cache)->addTags(['articles', 'english'])
                                        ->addTags(['page1'])
                                        ->cache('public', 120);
```

### Adding ESI to the cache

[](#adding-esi-to-the-cache)

Sometimes, you don't want to cache **all** content on a page. For instance, if you have a form with a csrf token, you do not want to cache the csrf token. It has to be unique for all users. You can achieve this by using Edge Side Includes, or ESI blocks. These ESI block punch holes, as it were, in your cached page.

An ESI block is a HTML tag that has special markup and is **not** cached. Instead, on constructing the cached page Litespeed cache will replace the ESI tag with the contents retrieved from another (uncached or privately cached) URL on your domain. Typically, such a URL will return a string or a block of HTML, for instance a csrf token, or the name of a logged in user, or an intricate HTML string containing the contents of a shopping cart.

Using ESI is quite simple:

1. Enable ESI in joostvanveen/litespeedcache `(new \Joostvanveen\Litespeedcache\Cache)->setEsiEnabled(true)->cache()`
2. Create a URL on you domain that will return the content for the ESI block, for instance ''.
3. Use an ESI markup block in your page that contains the ESI URL: ``

**ESI example**
Let's say you have a form with a csrf token, and the URL to retreived the uncached token from is .

Without ESI, you would display the token in a form like this:

```

```

With ESI, you first create a URL that returns `` and then insert an ESI block in the page that will be replaced by the contents of that URL.

```

```

Of course, you can also have the URL return just the token and place the ESI block in the form like this:

```

```

A word to the wise: try to use as little ESI blocks as possible. Constructing a cached page with a lot of ESI blocks can take so much time and resources that it defeats all the advantages of caching. A page containing only a few ESI blocks will be a little slower, but will still perform well. When using ESI blocks, measure the difference in response time for a cached and uncached page.

### Adding a vary to the cache

[](#adding-a-vary-to-the-cache)

Sometimes, you want the cache to distinguish between different variants for the same URL.

Example: say you have a multi-site ap that runs across different subdomains. Say you serve these two URLS:

- [www.domain.com/news?page=2](http://www.domain.com/news?page=2)
- subdomain.domain.com/news?page=2

These two URLS have different content, so you need Litespeed to store a cache for each URL. By default, Litespeed Cache cannot do this. It only takes the `news?page=1` part to create te cache identifier, which would be equal for both URLs.

But when you add the subdomain as a `VARY`, Litespeed Cache **will** add that to the cache identifier, making it store a different version for each domain.

You want to be careful not to use too many vary values, or the identifiers will become so customized that no two cache identifiers in your app will ever be the same. This can for instance happen if you add the User Agent to the vary.

```
(new \Joostvanveen\Litespeedcache\Cache)->addVary('value=subdomain')->cache('public', 360);
```

You can also pass in a string, if you need to define only one tag.

```
(new \Joostvanveen\Litespeedcache\Cache)->addTags('articles')->cache('public', 120);
```

### Purging a selected URI from cache

[](#purging-a-selected-uri-from-cache)

To purge a specific URI, simply add the URI to the cache before calling purge()

```
(new \Joostvanveen\Litespeedcache\Cache)->addUri('/about-us')
                                        ->purge();
```

### Purging selected tags from cache

[](#purging-selected-tags-from-cache)

To purge tags, simply add the tag or tags to the cache before calling purge()

```
(new \Joostvanveen\Litespeedcache\Cache)->addTags(['articles', 'english'])
                                        ->purge();
```

There is also a special method to purge only tags: purgeTags(). By default, purgeTags() takes an array of tags.

```
(new \Joostvanveen\Litespeedcache\Cache)->purgeTags(['articles', 'english']);
```

You can also pass in a string, if you need to define only one tag.

```
(new \Joostvanveen\Litespeedcache\Cache)->purgeTags('english');
```

### Flushing the entire cache

[](#flushing-the-entire-cache)

You can purge all items from the cache at once like so:

```
(new \Joostvanveen\Litespeedcache\Cache)->purgeAll();
```

### Bypassing the cache

[](#bypassing-the-cache)

Sometimes, you need to inspect a URL without cache, e.g. for troubleshooting or previewing. For that, you can either add `cache_bypass=1` to the query string, or set a `cache_bypass` cookie with a value of `1`.

E.g. this URL bypasses cache: `https://example.com?cache_bypass=1`

### Disabling the cache

[](#disabling-the-cache)

By default, the cache is enabled. But you can disable it as well.

```
$cache = new \Joostvanveen\Litespeedcache\Cache;

$cache->disable();
$enabled = $cache->enabled(); // Returns false

$cache->enable();
$enabled = $cache->enabled(); // Returns true
```

Use Litespeed Cache in a Laravel project
----------------------------------------

[](#use-litespeed-cache-in-a-laravel-project)

In a Laravel project, the package is automatically registered. There is also a Litespeedcache facade at your disposal.

Instead of calling something like:

```
(new \Joostvanveen\Litespeedcache\Cache)->cache('public', 120);
```

You can simply call

```
use LitespeedCache;

[...]

LitespeedCache::cache('public', 120);
```

When you use caching in Laravel, you best check against the environment. Since the cache sets headers, this can break your tests (phpunit sends output before the headers are set, which would result in `headers already sent` errors.)

```
if(! \App::environment(‘testing’) {
    Cache::purge();
}
```

Litespeed documentation
-----------------------

[](#litespeed-documentation)

You can find the Litespeed Cache documentation here \[Litespeed documentation: [https://www.litespeedtech.com/support/wiki/doku.php/litespeed\_wiki:cache:developer\_guide:response\_headers\](Litespeed](https://www.litespeedtech.com/support/wiki/doku.php/litespeed_wiki:cache:developer_guide:response_headers](Litespeed) documentation: [https://www.litespeedtech.com/support/wiki/doku.php/litespeed\_wiki:cache:developer\_guide:response\_headers](https://www.litespeedtech.com/support/wiki/doku.php/litespeed_wiki:cache:developer_guide:response_headers))

Contributing
------------

[](#contributing)

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

Changelog
---------

[](#changelog)

[Changelog](/joostvanveen/litespeedcache/blob/master/CHANGELOG.md)

License
-------

[](#license)

[MIT](/joostvanveen/litespeedcache/blob/master/LICENSE.md)

###  Health Score

35

—

LowBetter than 80% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity30

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity63

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 ~2 days

Total

16

Last Release

2562d ago

Major Versions

v0.6.2 → v1.0.02019-04-21

### Community

Maintainers

![](https://www.gravatar.com/avatar/e620cd65b718594831154a9c36f5bdace8191fde673e7631ac9f40e054c09d6d?d=identicon)[joostvanveen](/maintainers/joostvanveen)

---

Top Contributors

[![joostvanveen](https://avatars.githubusercontent.com/u/540294?v=4)](https://github.com/joostvanveen "joostvanveen (44 commits)")

---

Tags

phpcachecachingvarnishlitespeedfpcfull page cache

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/joostvanveen-litespeedcache/health.svg)

```
[![Health](https://phpackages.com/badges/joostvanveen-litespeedcache/health.svg)](https://phpackages.com/packages/joostvanveen-litespeedcache)
```

###  Alternatives

[voku/simple-cache

Simple Cache library

322.5M7](/packages/voku-simple-cache)[mmamedov/page-cache

PageCache is a lightweight PHP library for full page cache. It uses various strategies to differentiate among separate versions of the same page.

7912.7k1](/packages/mmamedov-page-cache)[alekseykorzun/memcached-wrapper-php

Optimized PHP 5 wrapper for Memcached extension that supports dog-piling, igbinary and local storage

2984.6k1](/packages/alekseykorzun-memcached-wrapper-php)[rapidwebltd/rw-file-cache

RW File Cache is a PHP File-based Caching Library. Its syntax is designed to closely resemble the PHP memcache extension.

15191.3k7](/packages/rapidwebltd-rw-file-cache)

PHPackages © 2026

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