前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >我的PHP缓存类Cache 2.0版发布

我的PHP缓存类Cache 2.0版发布

作者头像
魏杰
发布2022-12-23 17:51:20
2360
发布2022-12-23 17:51:20
举报

2.0版较1.0版完善了缓存文件存取时的资源访问锁定问题,希望大家多提宝贵意见!

下载请移步至http://www.sunbloger.com/download/

下面是源代码:

代码语言:javascript
复制
<?php
/**
 * Cache
 * @author Jason.Wei <jasonwei06@hotmail.com>
 * @license http://www.sunbloger.com/
 * @version 2.0 utf-8
 */

class cache
{
    /**
     * 缓存目录
     *
     * @var string
     */
    protected $cache_dir = './cache/';

    /**
     * 缓存生命周期(单位:秒)
     *
     * @var int
     */
    protected $cache_lifetime = 1800;

    /**
     * 设置缓存目录
     *
     * @param string $dir 目录
     */
    public function setCacheDir($dir)
    {
        $this->cache_dir = $dir;
    }

    /**
     * 设置缓存生命周期
     *
     * @param int $second 秒
     */
    public function setCacheLeftTime($second)
    {
        $this->cache_lifetime = $second;
    }

    /**
     * 写入缓存数据
     *
     * @param string $cache_name 缓存名
     * @param mixed $cache_data 缓存数据
     * @return bool
     */
    public function writeCache($cache_name, $cache_data)
    {
        $cache_key = $this->getCacheKey($cache_name);
        $cache_value = json_encode($cache_data);

        $save_dir = $this->cache_dir;
        if ( !file_exists($save_dir) ) {
            mkdir($save_dir);
            chmod($save_dir, 0777);
        }
        $cache_file = $save_dir.$cache_key;
        $fso = fopen($cache_file, "w"); //打开文件指针
        if (flock($fso, LOCK_EX)) { //独占锁定
            fwrite($fso, $cache_value); //写入

            flock($fso, LOCK_UN); //释放锁定
            fclose($fso);
            return true;
        } else {
            fclose($fso);
            return false;
        }
    }

    /**
     * 读取缓存数据
     *
     * @param string $cache_name 缓存名
     * @return mixed
     */
    public function readCache($cache_name)
    {
        $cache_key = $this->getCacheKey($cache_name);
        if (!$this->checkLifeTime($cache_key)) {
            return false;
        }
        $cache_file = $this->getCacheFile($cache_key);
        if (!file_exists($cache_file)) {
            return false;
        }
        $fso = fopen($cache_file, "r"); //打开文件指针

        if (flock($fso, LOCK_SH | LOCK_NB)) {
            $cache_value = @fread($fso, filesize($cache_file));
            flock($fso, LOCK_UN); //释放锁定
            fclose($fso);
            if (!empty($cache_value)) {
                $cache_data = json_decode($cache_value, true);
                return $cache_data;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    /**
     * 清除指定的缓存
     *
     * @param string $cache_name
     * @return bool
     */
    public function clearCache($cache_name)
    {
        $cache_key = $this->getCacheKey($cache_name);
        $cache_file = $this->getCacheFile($cache_key);
        return unlink($cache_file);
    }

    /**
     * 获取缓存名对应的Key
     *
     * @param string $cache_name 缓存名
     * @return string
     */
    private function getCacheKey($cache_name)
    {
        return md5($cache_name);
    }

    /**
     * 获取缓存文件
     *
     * @param string $cache_key 缓存KEY
     * @return string
     */
    private function getCacheFile($cache_key)
    {
        $save_dir = $this->cache_dir;
        $cache_file = $save_dir.$cache_key;
        return $cache_file;
    }

    /**
     * 获取缓存文件最后修改时间的UNIX时间戳
     *
     * @param string $cache_key 缓存KEY
     * @return int
     */
    private function getCacheTime($cache_key)
    {
        $cache_file = $this->getCacheFile($cache_key);
        return @filemtime($cache_file);
    }

    /**
     * 检查缓存生命周期
     *
     * @param string $cache_key 缓存KEY
     * @return bool
     */
    private function checkLifeTime($cache_key)
    {
        $cache_time = $this->getCacheTime($cache_key);
        if ($cache_time == false) {
            return false;
        }
        if ((time() - $cache_time) > $this->cache_lifetime) {
            return false;
        } else {
            return true;
        }
    }
}
?>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2011-07-19,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档