PHP code example of vormkracht10 / laravel-permanent-cache

1. Go to this page and download the library: Download vormkracht10/laravel-permanent-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/ */

    

vormkracht10 / laravel-permanent-cache example snippets




return [
    // Default cache driver to use for permanent cache
    'driver' => env('PERMANENT_CACHE_DRIVER', 'file'),

    // Option for cached components to add markers around output
    'components' => [
        // Add markers around the rendered value of Cached Components,
        // this helps to identify the cached value in the rendered HTML.

        // Which is useful for debugging and testing, but also for updating
        // the cached value inside another cache when using nested caches
        'markers' => [
            'enabled' => env('PERMANENT_CACHE_MARKERS_ENABLED', true),
            'hash' => env('PERMANENT_CACHE_MARKERS_HASH', env('APP_ENV') === 'production'),
        ],
    ],
];

use \Vormkracht10\PermanentCache\Facades\PermanentCache;

# When you don't need parameters per class, you can use direct parameters or an array:

# Without an array
PermanentCache::caches(
    LongRunningTask::class,
    LongerRunningTask::class,
);

# Passing an array
$caches = [
    LongRunningTask::class,
    LongerRunningTask::class,
];

PermanentCache::caches($caches);

# Specifying parameters per class
PermanentCache::caches([
    LongRunningTask::class => ['type' => 'long'],
    LongerRunningTask::class => ['type' => 'longer'],
]);

# As a multi-dimensional array when you need to use the same class multiple times, but with different parameters
PermanentCache::caches(
    [LongRunningTask::class => ['type' => 'long']],
    [LongRunningTask::class => ['type' => 'longer']],
);

use Vormkracht10\PermanentCache\Cached;

class LongRunningTask extends Cached
{
    protected $store = 'redis:a-unique-cache-key';

    public function run(): string
    {
        return "I'm executing a long running task!";
    }
}

use Vormkracht10\PermanentCache\Cached;

class LongRunningTaskListeningForEvents extends Cached
{
    protected $store = 'redis:unique-cache-key';

    public function run(TestEvent $event): string
    {
        return "I'm executing because of {$event->name}!";
    }
}

use Vormkracht10\PermanentCache\Cached;

class LongRunningTaskListeningForEvents extends Cached
{
    protected $store = 'redis:unique-cache-key';

    protected $events = [
        TestEvent::class,
        OtherEvent::class,
    ];
    
    /** @param TestEvent|OtherEvent $event */
    public function run($event): string
    {
        return "I'm executing because of {$event->name}!";
    }
}

use Vormkracht10\PermanentCache\Cached;
use Vormkracht10\PermanentCache\Scheduled;

class LongRunningTaskExecutedPeriodicallyOrWhenAnEventHappens extends Cached implements Scheduled
{
    protected $store = 'redis:unique-cache-key';

    protected $events = [
        TestEvent::class,
    ];

    // Use cron expression
    protected $expression = '* * * * *'; // run every minute

    public function run(): string
    {
        return "I'm executing because of {$event->name} or a scheduled run!";
    }

    // Or use the `schedule` method using a callback
    public static function schedule($callback)
    {
        return $callback->everyHour();
    }
}

use Illuminate\Contracts\Queue\ShouldQueue;
use Vormkracht10\PermanentCache\Cached;
use Vormkracht10\PermanentCache\Scheduled;

class LongRunningTaskExecutedPeriodicallyOrWhenAnEventHappensDispatchedOnTheQueue extends Cached implements ShouldQueue
{
    protected $store = 'redis:unique-cache-key';

    public $queue = 'execute-on-this-queue';

    public function run(TestEvent $event): string
    {
        return "I'm dispatching for execution on the queue because of {$event->name}!";
    }
}

use Illuminate\Contracts\Queue\ShouldQueue;
use Vormkracht10\PermanentCache\CachedComponent;
use Vormkracht10\PermanentCache\Scheduled;

class HeavyComponent extends CachedComponent implements Scheduled, ShouldQueue
{
    protected $store = 'redis:unique-cache-key';

    protected $events = [
        TestEvent::class,
    ];

    public function render()
    {
        return view('components.heavy-component');
    }

    public static function schedule($callback)
    {
        return $callback->everyHour();
    }
}

LongTaskInPermanentCache::update(['parameter' => 'value']);

use Vormkracht10\PermanentCache\Facades\PermanentCache;

PermanentCache::update();

# Before updating the cache
use Vormkracht10\PermanentCache\Events\PermanentCacheUpdating;

# After the cache has been updated
use Vormkracht10\PermanentCache\Events\PermanentCacheUpdated;

# Update all caches that match namespace using the optional filter
php artisan permanent-cache:update --filter=

# Show status overview of all registered caches including cached status, cache size, last updated at and scheduled update frequency
php artisan permanent-cache:status --parameters --filter=
bash
php artisan vendor:publish --provider="Vormkracht10\PermanentCache\PermanentCacheServiceProvider"