PHP code example of mis3085 / laravel-cache-label

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

    

mis3085 / laravel-cache-label example snippets


return [
    /**
     * Prefix key of every label cache
     */
    'prefix' => 'label:',

    /**
     * Affix key of every label cache
     */
    'affix' => ':key',

    /**
     * The default cache ttl of a label, null means forever.
     */
    'ttl' => null,

    /**
     * The repository class to replace the regular cache repository.
     */
    'repository' => \Mis3085\LaravelCacheLabel\CacheLabelRepository::class,

    /**
     * The behavior when calling flush()
     *
     * 'flush_labels' => clear all items of the labels, but still remmember their keys for further use.
     * 'reset_labels' => only unlink the relation between items and the label, but does not touch the value of items.
     * default => refer to the flush() of current cache store which will wipe out everything.
     *
     */
    'flush_behavior' => null,

    /**
     * The behavior when calling forget()
     *
     * true => forget the item and also remove the key from the labels.
     * false => only forget the item from cache store, but does not remove its key from the labels.
     */
    'forget_and_remove' => false,
];

cache()->labels(['people', 'artists'])->put('John', $john, $seconds);
cache()->labels(['people', 'authors'])->put('Anne', $anne, $seconds);

cache()->labels(['people', 'artists'])->get('John');
cache()->labels(['artists', 'people'])->get('John');
cache()->labels('people')->get('John');
cache()->labels('artists')->get('John');
cache()->get('John');

cache()->labels(['authors', 'people'])->get('John');
// null, John does not belong to authors

cache()->labels('artists')->flushLabels();

cache()->get('John');
// null, been wiped by the "artists" label

cache()->put('John', 'john-updated-1', $seconds);
cache()->get('John');
// "john-updated-1"

cache()->labels('people')->flushLabels();

cache()->get('John');
// null, been wiped by the "people" label

cache()->get('Anne');
// null, also been wiped

cache()->put('John', 'john-updated-2', $seconds);

cache()->labels('artists')->resetLabels();
// No longer track any cache items

cache()->labels('artists')->flushLabels();
cache()->get('John');
// "john-updated-2"

cache()->labels(['people', 'artists'])->put('John', $john, $seconds);
cache()->labels('artists')->forget('John');
cache()->get('John');
// null

cache()->put('John', 'john-updated-1', $seconds);
cache()->labels('artists')->get('John');
// "john-updated-1", the "artists" label still can access "John"

cache()->labels(['people', 'artists'])->put('John', $john, $seconds);
cache()->labels('artists')->forget('John');
cache()->get('John');
// null

cache()->put('John', 'john-updated-1', $seconds);

cache()->labels('artists')->get('John');
// null, the "artists" label can no longer access "John"

cache()->labels('people')->get('John');
// "john-updated-1", the "people" label is unaffected
bash
php artisan vendor:publish --tag="laravel-cache-label-config"