1. Go to this page and download the library: Download andydefer/jsonl-cache 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/ */
andydefer / jsonl-cache example snippets
// config/jsonl-cache.php
return [
// Chemin de base pour les fichiers de cache
'base_path' => env('JSONL_CACHE_PATH', storage_path('jsonl-cache')),
// TTL par défaut en secondes (null = pas d'expiration)
'default_ttl' => (int) env('JSONL_CACHE_TTL', 3600),
// Nombre de niveaux de hash (1-4)
'hash_levels' => (int) env('JSONL_CACHE_HASH_LEVELS', 2),
// Activation/désactivation du cache
'enabled' => (bool) env('JSONL_CACHE_ENABLED', true),
// Préfixe ajouté aux clés
'prefix' => env('JSONL_CACHE_PREFIX', 'cache'),
];
use AndyDefer\JsonlCache\Services\JsonlCacheService;
use AndyDefer\JsonlCache\Config\JsonlCacheConfig;
use AndyDefer\LaravelJsonl\JsonlService;
use AndyDefer\LaravelJsonl\Contexts\JsonlContext;
use AndyDefer\JsonlCache\Strategies\CachePathStrategy;
use AndyDefer\DomainStructures\Services\HydrationService;
use AndyDefer\PhpServices\Services\FileSystemService;
$config = new JsonlCacheConfig(app('config'));
$strategy = new CachePathStrategy('/tmp/cache', 2);
$fs = new FileSystemService();
$hydration = new HydrationService();
$jsonl = new JsonlService($strategy, $fs, new JsonlContext());
$cache = new JsonlCacheService($jsonl, $strategy, $config, $hydration, $fs);
use AndyDefer\JsonlCache\Contracts\JsonlCacheInterface;
class MyController extends Controller
{
public function __construct(
private readonly JsonlCacheInterface $cache,
) {}
public function index()
{
// Utilisation directe
}
}
// Stocker avec TTL par défaut (config)
$cache->set('user_123', ['name' => 'John Doe']);
// Stocker pour 1 heure
$cache->set('user_123', $userData, 3600);
// Stocker sans expiration
$cache->set('config_app', $config, null);
// Lecture simple
$user = $cache->get('user_123');
// Avec valeur par défaut
$user = $cache->get('user_123', ['name' => 'Guest']);
// Vérifier l'existence
if ($cache->has('user_123')) {
echo "Cache hit!";
}
// Supprimer une entrée
$cache->delete('user_123');
// Vider tout le cache
$cache->clear();