PHP code example of ziming / laravel-cloudflare-workers-kv

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

    

ziming / laravel-cloudflare-workers-kv example snippets


return [
    'account_id' => env('CLOUDFLARE_KV_ACCOUNT_ID'),

    'namespace_id' => env('CLOUDFLARE_KV_NAMESPACE_ID'),

    'api_token' => env('CLOUDFLARE_KV_API_TOKEN'),

    'base_url' => env('CLOUDFLARE_KV_BASE_URL', 'https://api.cloudflare.com/client/v4'),

    // HTTP timeouts (seconds) so a hung connection never blocks the request.
    'timeout' => env('CLOUDFLARE_KV_TIMEOUT', 5),
    'connect_timeout' => env('CLOUDFLARE_KV_CONNECT_TIMEOUT', 2),

    // When true, read failures (KV outage) degrade to a cache miss instead of
    // throwing. See "Graceful reads" below.
    'graceful' => env('CLOUDFLARE_KV_GRACEFUL', false),

    'serializer' => env('CLOUDFLARE_KV_SERIALIZER', 'php'),

    // Restricts which classes may be instantiated when unserializing PHP-serialized
    // values. Use an array of class-strings to allowlist, false to forbid all objects,
    // or null (default) to allow all. See "Security" below.
    'allowed_classes' => null,

    'prefix' => env('CLOUDFLARE_KV_PREFIX', ''),
];

'stores' => [
    'cloudflare' => [
        'driver' => 'cloudflare-kv',
        'serializer' => 'php',
        'prefix' => env('CACHE_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_cache_'),
    ],
],

Cache::store('cloudflare')->put('user:1', ['name' => 'Ada'], 3600);

$user = Cache::store('cloudflare')->get('user:1');

'stores' => [
    'cloudflare-json' => [
        'driver' => 'cloudflare-kv',
        'serializer' => 'json',
        'prefix' => 'shared:',
    ],
],

Cache::store('cloudflare-json')->forever('feature-flags', [
    'checkout' => true,
    'limit' => 5,
]);

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

    'cloudflare-sessions' => [
        'driver'       => 'cloudflare-kv',
        'namespace_id' => env('CLOUDFLARE_KV_SESSIONS_NAMESPACE_ID'),
        'prefix'       => 'sess:',
    ],
],

'cloudflare' => [
    'driver'   => 'cloudflare-kv',
    'graceful' => true,
],

use Ziming\LaravelCloudflareWorkersKv\LaravelCloudflareWorkersKv;

$kv = app(LaravelCloudflareWorkersKv::class);

$kv->put('settings', ['theme' => 'dark'], 3600); // optional TTL in seconds
$kv->forever('settings', ['theme' => 'dark']);    // no expiry

$settings = $kv->get('settings');

// Read a value alongside its absolute expiry (unix timestamp, null if none):
$entry = $kv->getWithMetadata('settings'); // ['value' => ..., 'expiration' => ..., 'metadata' => [...]]
$expiresAt = $kv->expiresAt('settings');   // ?int

$kv->putMany(['a' => 1, 'b' => 2], 3600);   // PUT .../bulk  (up to 10,000 keys/request)

$values = $kv->many(['a', 'b', 'c']);        // POST .../bulk/get (up to 100 keys/request)
// => ['a' => 1, 'b' => 2, 'c' => null]      // missing keys are null

$kv->deleteMany(['a', 'b']);                  // POST .../bulk/delete (up to 10,000 keys/request)

// config/cloudflare-workers-kv.php
'allowed_classes' => false,                          // forbid all objects (scalars/arrays only)
// or
'allowed_classes' => [App\Dto\FeatureFlags::class],  // allowlist specific classes
bash
php artisan vendor:publish --tag="laravel-cloudflare-workers-kv-config"
json
{"checkout":true,"limit":5}