PHP code example of matchory / response-cache

1. Go to this page and download the library: Download matchory/response-cache 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/ */

    

matchory / response-cache example snippets


Route::get('/example')->middleware([
    'cacheResponse:3600,examples'
])

protected $middlewareGroups = [
   'web' => [
       // ...
       \Matchory\ResponseCache\Http\Middleware\CacheResponse::class,
       // ...
   ],
];

protected $routeMiddleware = [
    // ...
    'bypass_cache' => \Matchory\ResponseCache\Http\Middleware\BypassCache::class,
    // ...
];

Route::get('/kitten')->middleware('bypass_cache');

use Matchory\ResponseCache\Facades\ResponseCache;

class ExampleObserver {
    public function saved(): void {
        ResponseCache::clear();
    }

    public function deleted(): void {
        // By passing one or more tags, only those items tagged appropriately
        // will be cleared
        ResponseCache::clear(['example']);
    }
}

use Matchory\ResponseCache\Facades\ResponseCache;

ResponseCache::delete('/example/url');
ResponseCache::delete(['/example/1', '/example/2']);

  Route::get('/foo')->middleware(['cache:tag-1,tag-2']);
  

  'tags' => env('RESPONSE_CACHE_TAGS', [ 'tag-1', 'tag-2' ]),
  

Route::get('/api/v{version}/flights/{flight}', function(\App\Flight $flight) {
    // ...
})->middleware('cache:api.v{version},flights,flights.v{version},flights.{flight},flights.v{version}.{flight}');

use Illuminate\Http\Request;use Matchory\ResponseCache\Support\BaseStrategy;

class MyStrategy extends BaseStrategy
{
    public function key(Request $request): string
    {
        $identifier = $this->extractRequestIdentifier($request);
        $suffix = $this->buildSuffix($request);

        return $this->hash($identifier . $suffix);
    }
}

use Illuminate\Http\Request;
use Matchory\ResponseCache\Support\BaseStrategy;
use Symfony\Component\HttpFoundation\Response;

class MyStrategy extends BaseStrategy
{
    public function shouldCache(Request $request, Response $response): bool
    {
        if (! $this->isMethodCachable($request)) {
            return false;
        }

        return $this->isSuccessful($response);
    }
}

use Illuminate\Http\Request;
use Matchory\ResponseCache\Support\BaseStrategy;
use Symfony\Component\HttpFoundation\Response;

class MyStrategy extends BaseStrategy
{
    public function tags(Request $request, Response|null $response = null): array
    {
        return [
            $request->attributes->get('customer.id')
        ];
    }
}

// Default settings as set in the config file
Route::get('/foo')->middleware('response_cache')

// TTL of 60 seconds
Route::get('/bar')->middleware('response_cache:60');

// Tags "foo", "bar" and "baz" are added
Route::get('/baz')->middleware('response_cache:foo,bar,baz'); 

// TTL of 60 seconds and tags "foo", "bar" and "baz" are added
Route::get('/quz')->middleware('response_cache:60,foo,bar,baz');

use Matchory\ResponseCache\Repository;
use Symfony\Component\HttpFoundation\Response;

class CustomRepository extends Repository
{
    protected function serialize(Response $response) : mixed
    {
        return serialize($response); 
    }

    protected function hydrate(mixed $responseData): Response
    {
        return unserialize($responseData, [Response::class])
    }
}

use Matchory\ResponseCache\Repository;

protected function register():void
    {
        $this->app->bind(Repository::class, CustomRepository::class);
    }
bash
php artisan vendor:publish --provider "Matchory\ResponseCache\ResponseCacheProvider"
bash
php artisan response-cache:flush
bash
php artisan response-cache:flush tag-1 tag-2 ... tag-n