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/ */
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']);
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);
}