特點(diǎn)是小巧簡單,適用于一些較小的應(yīng)用,沒有Redis環(huán)境,但又需要用到緩存的情況,如下代碼加入到functions.php即可。
if (!function_exists('cache')) {
/**
* 緩存管理
* @param string $name 緩存名稱
* @param mixed $value 緩存值
* @param int $expire 緩存有效時(shí)長(秒)
* @return mixed
*/
function cache(string $name = null, $value = '', $expire = null)
{
if (is_null($name)) {
return false;
}
$md5 = hash('md5', $name);
$filename = runtime_path() . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . substr($md5, 0, 2) . DIRECTORY_SEPARATOR . substr($md5, 2) . '.php';
if ('' === $value) {
// 獲取緩存
if (!file_exists($filename)) {
return false;
}
$content = @file_get_contents($filename);
if (false !== $content) {
$expire = (int) substr($content, 8, 12);
if (0 != $expire && time() - $expire > filemtime($filename)) {
//緩存過期刪除緩存文件
unlink($filename);
return false;
}
$content = substr($content, 32);
if (function_exists('gzcompress')) {
//啟用數(shù)據(jù)壓縮
$content = gzuncompress($content);
}
return is_string($content) ? unserialize($content) : null;
}
} elseif (is_null($value)) {
// 刪除緩存
unlink($filename);
return false;
}
// 緩存數(shù)據(jù)
$dir = dirname($filename);
if (!is_dir($dir)) {
mkdir($dir, 0755, true);
}
$data = serialize($value);
if (function_exists('gzcompress')) {
//數(shù)據(jù)壓縮
$data = gzcompress($data, 3);
}
$data = "<?php\n//" . sprintf('%012d', $expire) . "\n exit();?>\n" . $data;
file_put_contents($filename, $data, LOCK_EX);
return true;
}
}
使用方法:
// 設(shè)置緩存數(shù)據(jù)
cache('name', $value, 3600);
// 獲取緩存數(shù)據(jù)
print_r(cache('name'));
// 刪除緩存數(shù)據(jù)
cache('name', NULL);
作了下補(bǔ)充,如果系統(tǒng)有開啟redis擴(kuò)展,自動(dòng)使用redis緩存,未開啟則使用文件緩存
function cache(string $name = null, $value = '', $expire = null)
{
if (is_null($name)) {
return false;
}
$md5 = hash('md5', $name);
if (extension_loaded('redis')) {
//已安裝Redis擴(kuò)展
if ('' === $value) {
// 獲取緩存
if (!\support\Cache::has($name)) {
return false;
}
$content = \support\Cache::get($name);
if (false !== $content) {
return is_string($content) ? unserialize($content) : null;
}
}elseif (is_null($value)) {
// 刪除緩存
\support\Cache::delete($name);
}
$data = serialize($value);
\support\Cache::set($name,$data,$expire);
return true;
}
$filename = runtime_path() . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . substr($md5, 0, 2) . DIRECTORY_SEPARATOR . substr($md5, 2) . '.php';
if ('' === $value) {
// 獲取緩存
if (!file_exists($filename)) {
return false;
}
$content = @file_get_contents($filename);
if (false !== $content) {
$expire = (int) substr($content, 8, 12);
if (0 != $expire && time() - $expire > filemtime($filename)) {
//緩存過期刪除緩存文件
unlink($filename);
return false;
}
$content = substr($content, 32);
if (function_exists('gzcompress')) {
//啟用數(shù)據(jù)壓縮
$content = gzuncompress($content);
}
return is_string($content) ? unserialize($content) : null;
}
} elseif (is_null($value)) {
// 刪除緩存
if (!file_exists($filename)) {
return false;
}
unlink($filename);
return false;
}
// 緩存數(shù)據(jù)
$dir = dirname($filename);
if (!is_dir($dir)) {
mkdir($dir, 0755, true);
}
$data = serialize($value);
if (function_exists('gzcompress')) {
//數(shù)據(jù)壓縮
$data = gzcompress($data, 3);
}
$data = "<?php\n//" . sprintf('%012d', $expire) . "\n exit();?>\n" . $data;
file_put_contents($filename, $data, LOCK_EX);
return true;
}
好東西