yabx/cache

简单的缓存实现

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 (缓存接口)
  • FileCache (文件缓存实现)
  • RedisCache (Redis 缓存实现)

安装

composer req yabx/cache

简单使用

<?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');

示例

<?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;