PHP code example of gokhankurtulus / jsoncache

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

    

gokhankurtulus / jsoncache example snippets


use JsonCache\JsonCache;

$config = [
    'storage_path' => '/path/to/storage',
    'index_file' => 'index.json',
    'lifetime' => 60, // seconds
    'force_create_storage_path' => true,
    'force_create_index_file' => true,
    'json_flags' => JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES,
    'compress' => true
];

$jsonCache = new JsonCache($config);

$jsonCache->set('my_key', ['name' => 'John Doe', 'age' => 30]);

$data = $jsonCache->get('my_key');
if ($data !== null) {
    // Use the cached data
    echo $data['name']; // John Doe
    echo $data['age']; // 30
} else {
    // Data not found or expired
}

if ($jsonCache->has('my_key')) {
    // Key exists in the cache
} else {
    // Key does not exist
}

$jsonCache->delete('my_key');

$jsonCache->clear();

$jsonCache->getCacheSize('my_key'); // Returns the size of the key in bytes
$jsonCache->getLifetime(); // Returns cache instance lifetime
$jsonCache->setLifetime(120); // Sets cache instance lifetime to 120 seconds
$jsonCache->isCompressed(); // Returns cache compress status
$jsonCache->getConfig(); // Returns config array
$jsonCache->getStoragePath(); // Returns config's storage path
$jsonCache->getIndexFile(); // Returns config's index file path
$jsonCache->getJsonFlags(); // Returns config's json flags