PHP code example of erjon / laravel-cache-groups

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

    

erjon / laravel-cache-groups example snippets


'providers' => [
    // ...
    Erjon\CacheGroups\Providers\CacheGroupsServiceProvider::class,
],

use Illuminate\Support\Facades\Cache;

// Store a value in a group
Cache::groups(['products'])->put('item:1', $product, 3600);

// Retrieve a value
$product = Cache::groups(['products'])->get('item:1');

// Store with multiple groups
Cache::groups(['products', 'featured'])->put('item:1', $product, 3600);

// Flush all items in a group
Cache::groups(['products'])->flush();

// Store items
Cache::groups(['products'])->put('key', 'value', $seconds);
Cache::groups(['products'])->forever('key', 'value');
Cache::groups(['products'])->add('key', 'value', $seconds);
Cache::groups(['products'])->putMany(['key1' => 'value1', 'key2' => 'value2'], $seconds);

// Retrieve items
Cache::groups(['products'])->get('key');
Cache::groups(['products'])->get('key', 'default');
Cache::groups(['products'])->many(['key1', 'key2']);
Cache::groups(['products'])->has('key');

// Remember pattern
Cache::groups(['products'])->remember('key', $seconds, function () {
    return expensive_computation();
});

Cache::groups(['products'])->rememberForever('key', function () {
    return expensive_computation();
});

// Increment/Decrement
Cache::groups(['products'])->increment('counter', 1);
Cache::groups(['products'])->decrement('counter', 1);

// Remove items
Cache::groups(['products'])->forget('key');
Cache::groups(['products'])->flush(); // Flush entire group

// These are equivalent:
Cache::groups(['products', 'featured'])->put('key', 'value', 3600);
Cache::groups('products', 'featured')->put('key', 'value', 3600);

// On Redis/Memcached, this internally calls cache()->tags()
Cache::groups(['products'])->put('item:1', $data, 3600);

Cache::tags(['products'])->put('item:1', $data, 3600);
$data = Cache::tags(['products'])->get('item:1');
Cache::tags(['products'])->flush();

Cache::groups(['products'])->put('item:1', $data, 3600);
$data = Cache::groups(['products'])->get('item:1');
Cache::groups(['products'])->flush();

   // Good: Specific, manageable groups
   Cache::groups(['user:123'])->put('profile', $data, 3600);
   Cache::groups(['user:123'])->put('settings', $data, 3600);
   
   // Less optimal: One massive group
   Cache::groups(['all-data'])->put('random:key:' . $id, $data, 3600);