PHP code example of iak / keys

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

    

iak / keys example snippets


// config/keys.php
return [
    'cache' => [
        'product-data' => 'product:{productId},variant:{variantId}',
    ],
    'limit' => [
        'password-reset' => 'password-reset:{userId}' 
    ]
];

use Iak\Key\Key;

// Using named parameters
Key::cache('product-data', productId: 123, variantId: 456);

// Using array parameters
Key::cache('product-data', [
    'productId' => 123,
    'variantId' => 456
]);

// Using parameter position
Key::cache('user.action', 123, 456);

// The result for all above: "product:123,variant:456"


// Cache keys
Key::cache('user.profile', id: 123);

// Queue keys
Key::queue('email.notification', type: 'welcome', userId: 456);

// Event keys
Key::event('user.action', action: 'login', userId: 789);

// Session keys
Key::session('user.data', userId: 123, sessionId: 'abc123');

// Job keys
Key::job('process.data', jobType: 'import', priority: 'high');

// Lock keys
Key::lock('', version: 'v1', resource: 'users');

// Define a custom key type in config
// config/keys.php
return [
    'custom' => [
        'api.request' => 'api:{endpoint}:{method}:{timestamp}',
    ],
];

// Use it dynamically
$key = Key::custom('api.request', 
    endpoint: 'users',
    method: 'GET',
    timestamp: time()
);
// Result: "api:users:GET:1234567890"

// Missing ('user.profile'); 
// Throws: Missing user.profile', id: 123, extra: 'value'); // Throws: Extra parameters: extra

// Non-existent key
Key::cache('nonexistent.key', id: 123); // Throws: Key not found in config

'user.profile' => 'user:profile:{id}',
'product.details' => 'product:{category}:{id}:{variant}',
'api.request' => 'api:{version}:{endpoint}:{method}',
bash
php artisan vendor:publish --tag="keys-config"