<?php
/**
* @author chenliujin <liujin.chen@qq.com>
* @since 2013-12-10
*/
class FileCache
{
public static $path = '/tmp/filecache';
public static $app = 'default';
public static $prefix = '';
/**
* @author chenliujin <liujin.chen@qq.com>
* @since 2013-12-10
*/
public static function set($key, $value, $timeout=3600)
{
$key = self::$prefix . $key;
$file = self::path() . $key;
$content = json_encode($value, TRUE);
$rs = file_put_contents($file, $content);
chmod($file, 0777);
touch($file, time() + $timeout);
return $rs ? TRUE : FALSE;
}
/**
* @author chenliujin <liujin.chen@qq.com>
* @since 2013-12-10
*/
public static function get($key)
{
$key = self::$prefix . $key;
$file = self::path() . $key;
if (!file_exists($file)) {
return FALSE;
}
//expire
if (filemtime($file) < time()) {
return FALSE;
}
$content = file_get_contents($file);
$cache = json_decode($content, TRUE);
return $cache;
}
/**
* @author chenliujin <liujin.chen@qq.com>
* @since 2013-12-10
*/
protected static function path()
{
return self::$path . '/' . self::$app . '/';
}
}
FileCache
最新推荐文章于 2023-05-03 11:41:09 发布