PHP code example of schoolpalm / cache-store

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

    

schoolpalm / cache-store example snippets


use SchoolPalm\CacheStore\Facades\CacheStore;

// Basic usage
CacheStore::put('students_count', 500);
$count = CacheStore::get('students_count'); // 500

// Context-aware caching
CacheStore::forContext('tenant_abc', 'school_123')
    ->put('students', 500);

CacheStore::forContext('tenant_xyz', 'school_456')
    ->put('students', 900);

// Values are isolated per context!
CacheStore::forContext('tenant_abc', 'school_123')
    ->get('students'); // 500

CacheStore::forContext('tenant_xyz', 'school_456')
    ->get('students'); // 900

'providers' => [
    SchoolPalm\CacheStore\Providers\CacheStoreServiceProvider::class,
],

'aliases' => [
    'CacheStore' => SchoolPalm\CacheStore\Facades\CacheStore::class,
],

return [

    /*
    | Default cache driver used by CacheStore.
    */
    'driver' => env('CACHE_STORE_DRIVER', 'file'),

    /*
    | Separator used when building context-aware cache keys.
    | Example: tenant:school:key  or  tenant.school.key
    */
    'key_separator' => env('CACHE_STORE_KEY_SEPARATOR', ':'),

    /*
    | Prefix for all generated cache keys.
    | Example: schoolpalm:tenant:1:school:5:students
    */
    'prefix' => env('CACHE_STORE_PREFIX', 'schoolpalm'),

    /*
    | Context settings
    */
    'context' => [
        'tenant' => true,  // Include tenant identifier
        'school' => true,  // Include school identifier
    ],

    /*
    | Driver-specific configurations
    */
    'drivers' => [
        'file' => [
            'path' => storage_path('framework/cache'),
        ],
        'redis' => [
            'connection' => env('CACHE_STORE_REDIS_CONNECTION', 'default'),
        ],
        'database' => [
            'table' => 'cache_store',
        ],
    ],
];

use SchoolPalm\CacheStore\Facades\CacheStore;

// Store a value
CacheStore::put('key', 'value', $ttl = 3600);

// Store a value permanently
CacheStore::forever('key', 'value');

// Retrieve a value
$value = CacheStore::get('key', $default = null);

// Check if key exists
if (CacheStore::has('key')) {
    // ...
}

// Store if not exists
CacheStore::add('key', 'value', $ttl = 3600);

// Retrieve and forget
$value = CacheStore::pull('key');

// Remove a value
CacheStore::forget('key');

// Increment / Decrement
CacheStore::increment('counter', 1);
CacheStore::decrement('counter', 1);

// Multiple values
CacheStore::putMany(['key1' => 'value1', 'key2' => 'value2']);
$values = CacheStore::many(['key1', 'key2']);

// Flush all cache
CacheStore::flush();

// Arrays
CacheStore::put('school', [
    'name' => 'Greenhill Academy',
    'students' => 1200,
    'teachers' => 80,
]);
$school = CacheStore::get('school');

// Objects
$user = (object) ['name' => 'John', 'role' => 'Admin'];
CacheStore::put('user', $user);
$retrieved = CacheStore::get('user'); // stdClass

// Cache the result of a callback
$value = CacheStore::remember('expensive_operation', 3600, function () {
    return someExpensiveOperation();
});

// Cache forever
$value = CacheStore::rememberForever('config', function () {
    return loadConfiguration();
});

// At runtime via factory
$factory = app(\SchoolPalm\CacheStore\CacheDriverFactory::class);
$driver = $factory->driver('redis');
$driver->put('key', 'value');

// List available drivers
$drivers = $factory->available(); // ['array', 'file', 'database', 'redis', 'laravel']

// Check if a driver is available
if ($factory->has('redis')) {
    // ...
}

use SchoolPalm\CacheStore\Contracts\CacheDriver;

class MongoCacheDriver implements CacheDriver
{
    // Implement all CacheDriver methods...
}

// Register it in the factory
$factory->extend('mongo', function () {
    return new MongoCacheDriver();
});

// Store in a specific context
CacheStore::forContext('tenant_abc', 'school_123')
    ->put('students_count', 500);

// Retrieve from the same context
CacheStore::forContext('tenant_abc', 'school_123')
    ->get('students_count'); // 500

// Different context — isolated value
CacheStore::forContext('tenant_xyz', 'school_456')
    ->get('students_count'); // null

'context' => [
    'tenant' => true,  // Include tenant in key
    'school' => true,  // Include school in key
],

use SchoolPalm\CacheStore\Drivers\ArrayCacheDriver;
use SchoolPalm\CacheStore\Tests\Concerns\CacheDriverBehaviour;

uses(CacheDriverBehaviour::class);

beforeEach(function () {
    $this->driver = new ArrayCacheDriver(
        app(\Illuminate\Contracts\Cache\Repository::class),
        app(\SchoolPalm\CacheStore\Support\CacheSerializer::class)
    );
});

use SchoolPalm\CacheStore\Drivers\YourDriver;
use SchoolPalm\CacheStore\Tests\Concerns\CacheDriverBehaviour;
use SchoolPalm\CacheStore\Tests\Support\CreatesCacheDrivers;

uses(
    CreatesCacheDrivers::class,
    CacheDriverBehaviour::class
);

beforeEach(function () {
    $this->driver = $this->makeDriver(
        YourDriver::class
    );
});

namespace SchoolPalm\CacheStore\Contracts;

interface CacheDriver
{
    public function get(string $key, mixed $default = null): mixed;
    public function put(string $key, mixed $value, DateTimeInterface|DateInterval|int|null $ttl = null): bool;
    public function forever(string $key, mixed $value): bool;
    public function add(string $key, mixed $value, DateTimeInterface|DateInterval|int|null $ttl = null): bool;
    public function has(string $key): bool;
    public function forget(string $key): bool;
    public function flush(): bool;
    public function increment(string $key, int $value = 1): int;
    public function decrement(string $key, int $value = 1): int;
    public function pull(string $key, mixed $default = null): mixed;
    public function many(array $keys): array;
    public function putMany(array $values, DateTimeInterface|DateInterval|int|null $ttl = null): bool;
}
bash
php artisan vendor:publish --provider="SchoolPalm\CacheStore\Providers\CacheStoreServiceProvider" --tag="config"