PHP code example of kopaing / laravel-cloudflare-kv

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

    

kopaing / laravel-cloudflare-kv example snippets


Cache::store('cloudflare')->remember('products:featured', 3600, fn () => Product::featured()->get());

'stores' => [
    'cloudflare' => [
        'driver' => 'cloudflare',
    ],
],

'cloudflare' => [
    'driver' => 'cloudflare',
    'account_id' => env('CLOUDFLARE_ACCOUNT_ID'),
    'namespace_id' => env('CLOUDFLARE_KV_NAMESPACE_ID'),
    'api_token' => env('CLOUDFLARE_API_TOKEN'),
    'prefix' => env('CLOUDFLARE_KV_PREFIX', 'laravel:'),
    'timeout' => 10,
    'connect_timeout' => 5,
    'retry' => [
        'times' => 3,
        'sleep' => 200,
    ],
    'lock_store' => 'redis', // optional, see "Locks"
],

use Illuminate\Support\Facades\Cache;

$kv = Cache::store('cloudflare');

$kv->put('user:1', $user, 3600);
$kv->get('user:1');
$kv->has('user:1');
$kv->remember('products:featured', 3600, fn () => Product::featured()->get()->toArray());
$kv->forever('app:settings', $settings);
$kv->forget('user:1');

$kv->many(['user:1', 'user:2']);                       // 1 bulk request per 100 keys
$kv->putMany(['user:1' => $a, 'user:2' => $b], 3600); // 1 bulk request per 10,000 keys

$kv->flush();                                          // only keys under this store's prefix

Cache::store('cloudflare')->put('tracking:ORDER-1001', [
    'status' => 'shipped',
    'updated_at' => now()->toISOString(),
], 86400);

'cloudflare' => ['driver' => 'cloudflare', 'lock_store' => 'redis'],

Cache::store('cloudflare')->lock('reports:daily', 60)->get(fn () => ...); // lock lives in Redis

Event::listen(CloudflareKVRequestFailed::class, fn ($e) => Log::warning('KV request failed', (array) $e));
bash
php artisan vendor:publish --tag=cloudflare-kv-config
bash
   composer package:discover   # lists kopaing/laravel-cloudflare-kv
   php artisan tinker --execute="dump(get_class(Cache::store('cloudflare')->getStore()));"