yabx/cache

Simple Cache Implementation

0.0.2 2023-10-17 14:16 UTC

This package is auto-updated.

Last update: 2024-09-17 16:19:52 UTC


README

  • CacheInterface (cache interface)
  • FileCache (file cache implementation)
  • RedisCache (Redis cache implementation)

Installation

composer req yabx/cache

Simple usage

<?php

use Yabx\Cache\FileCache;
use Yabx\Cache\RedisCache;

// FileCache
$cache = new FileCache(__DIR__ . '/cache');

// or RedisCache  
$redis = new Redis;
$redis->connect('localhost');
$cache = new RedisCache($redis, 'prefix:');

// Setting value (3600 seconds live period)
$cache->set('key', $value, 3600);

// Getting value by key
$value = $cache->get('key');

// Deleting value by key
$cache->delete('key');

Example

<?php

// ....

$key = 'calculated';

// Checking for cached item with $key
if(!$value = $cache->get($key)) {
    // Hardworking for calculate value
    $value = calc(....);
    $cache->set($key, $value, 3600);
}

// Displing value
echo $value;