PHP code example of joostvanveen / laravel-litespeedcache

1. Go to this page and download the library: Download joostvanveen/laravel-litespeedcache library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

joostvanveen / laravel-litespeedcache example snippets


php artisan vendor:publish --provider="Joostvanveen\LaravelLitespeedcache\LitespeedCacheServiceProvider" --tag=config

// Cache is enabled
config(['litespeedcache.defaults.enabled' => true]);

// ESI is enabled
config(['litespeedcache.defaults.esiEnabled' => true]);

// Whether or not to cache ajax calls
config(['litespeedcache.defaults.enable_ajax_cache' => false]);

// Array of request methods that should be cached
config(['litespeedcache.defaults.cache_http_verbs' => ['GET', 'HEAD']]);

// Whether or not to use the deafult middleware that comes with this package
config(['litespeedcache.defaults.use_middleware' => true]);

// Default cache type
config(['litespeedcache.defaults.type' => 'public']);

// Default TTL for cache in minutes
config(['litespeedcache.defaults.lifetime' => 240]);

// Array of URIs that should not be cached, can contain wildcards like '/admin*'
config(['litespeedcache.defaults.excludedUris' => [$csrfTokenUri . '*', $csrfFieldUri . '*']]);

// Array of query strings that should not be cached, can contain wildcards like '*utm_source=*'
config(['litespeedcache.defaults.excludedQueryStrings' => []]);

// Array of routes for this package 
config(['litespeedcache.routes' => ['token' => $csrfTokenUri, 'field' => $csrfFieldUri] ]);

use LitespeedCache;

[...]

// Cache use all default settings from config.
LitespeedCache::cache();

// Purge the cache
LitespeedCache::purge();

// Full options example
// You can also set $excludedUris and $excludedQueryStrings in config.
$excludedUris = [
    'checkout*',
    'admin*',
];
$excludedQueryStrings = [
    '*direction=*',
];
LitespeedCache::setType('private')->setLifetime(120)
                                  ->addTags(['articles', 'en_GB'])
                                  ->addVary('value=mysubdomain')
                                  ->setExcludedUrls($excludedUris)
                                  ->setExcludedQueryStrings($excludedQueryStrings)
                                  ->cache();
                                  
// Purge cache using tags.
LitespeedCache::addTags('articles')->purge();

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

php artisan vendor:publish --provider="Joostvanveen\LaravelLitespeedcache\LitespeedCacheServiceProvider" --tag=config



return [
    'defaults' => [
        'enabled' => true, 
        'use_middleware' => false, // Set Litespeed Cache Middleware to inactive
        'type' => 'public', 
        'lifetime' => 240, 
    ],
];

php artisan make:middleware LitespeedCache



namespace App\Http\Middleware;

use Closure;
use Joostvanveen\LaravelLitespeedcache\Facades\LitespeedCache as LitespeedCacheFacade;

class LitespeedCache
{

    /**
     * Handle an incoming request.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Closure $next
     *
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $excludedUris = [
            'checkout*',
            'admin*',
        ];
        
        LitespeedCacheFacade::setEnabled(config('litespeedcache.defaults.enabled'))
                            ->setType(config('litespeedcache.defaults.type'))
                            ->setLifetime(config('litespeedcache.defaults.lifetime'))
                            ->setExcludedUrls($excludedUris)
                            ->cache();

        return $next($request);
    }
}

{!! getLitespeedCsrfField() !!}

// With esi enabled
<esi:input type="hidden" name="_token" value="TGZUHk9BSg6QTH1knqPLPXB20EHKLmqEZSt4f3Uk">

{{ getLitespeedCsrfToken() }}

// With esi enabled
<esi:GZUHk9BSg6QTH1knqPLPXB20EHKLmqEZSt4f3Uk

<esi::