php如何做缓存插件

取自织梦缓存方法,可根据自己的自行修改

首先我们需要一个写入文件的方法

if ( ! function_exists('PutFile'))
{
    function PutFile($file, $content, $flag = 0)
    {
        $pathinfo = pathinfo ( $file );
        if (! empty ( $pathinfo ['dirname'] ))
        {
            if (file_exists ( $pathinfo ['dirname'] ) === FALSE)
            {
                if (@mkdir ( $pathinfo ['dirname'], 0777, TRUE ) === FALSE)
                {
                    return FALSE;
                }
            }
        }
        if ($flag === FILE_APPEND)
        {
            return @file_put_contents ( $file, $content, FILE_APPEND );
        }
        else
        {
            return @file_put_contents ( $file, $content, LOCK_EX );
        }
    }
}

然后我们再写缓存组件

<?php   if(!defined('XHINC')) exit("Request Error!");
/**
 * 缓存小助手,支持文件和memcache
 *
 * @version        $Id: cache.helper.php 1 10:46 2011-3-2 tianya $
 * @package        DedeCMS.Helpers
 * @copyright      Copyright (c) 2007 - 2010, DesDev, Inc.
 * @license        http://help.dedecms.com/usersguide/license.html
 * @link           http://www.dedecms.com
 */
/**
 *  读缓存
 *
 * @access    public
 * @param     string  $prefix  前缀
 * @param     string  $key  键
 * @param     string  $is_memcache  是否为memcache缓存
 * @return    string
 */
if ( ! function_exists('GetCache'))
{
    function GetCache($prefix, $key, $is_memcache = TRUE)
    {
        global $cache_helper_config;
        $key = md5 ( $key );
        /* 如果启用MC缓存 */
        if ($is_memcache === TRUE && ! empty ( $cache_helper_config['memcache'] ) && $cache_helper_config['memcache'] ['is_mc_enable'] === 'Y')
        {
            $mc_path = empty ( $cache_helper_config['memcache'] ['mc'] [substr ( $key, 0, 1 )] ) ? $cache_helper_config['memcache'] ['mc'] ['default'] : $cache_helper_config['memcache'] ['mc'] [substr ( $key, 0, 1 )];
            $mc_path = parse_url ( $mc_path );
            $key = ltrim ( $mc_path ['path'], '/' ) . '_' . $prefix . '_' . $key;
            if (empty ( $GLOBALS ['mc_' . $mc_path ['host']] ))
            {
                $GLOBALS ['mc_' . $mc_path ['host']] = new Memcache ( );
                $GLOBALS ['mc_' . $mc_path ['host']]->connect ( $mc_path ['host'], $mc_path ['port'] );
            }
            return $GLOBALS ['mc_' . $mc_path ['host']]->get ( $key );
        }
        $key = substr ( $key, 0, 2 ) . '/' . substr ( $key, 2, 2 ) . '/' . substr ( $key, 4, 2 ) . '/' . $key;
        $result = @file_get_contents ( DEDEDATA . "/cache/$prefix/$key.php" );
        
        if ($result === false)
        {
            return false;
        }
        $result = str_replace("<?php exit('dedecms');?>\n\r", "", $result);
        $result = @unserialize ( $result );
        if($result ['timeout'] != 0 && $result ['timeout'] < time ())
        {
              return false;
        }
        return $result ['data'];
    }
}


/**
 *  写缓存
 *
 * @access    public
 * @param     string  $prefix  前缀
 * @param     string  $key  键
 * @param     string  $value  值
 * @param     string  $timeout  缓存时间
 * @return    int
 */
if ( ! function_exists('SetCache'))
{
    function SetCache($prefix, $key, $value, $timeout = 3600, $is_memcache = TRUE)
    {
        global $cache_helper_config;
        $key = md5 ($key);
        /* 如果启用MC缓存 */
        if (! empty ( $cache_helper_config['memcache'] ) && $cache_helper_config['memcache'] ['is_mc_enable'] === 'Y' && $is_memcache === TRUE)
        {
            $mc_path = empty ( $cache_helper_config['memcache'] ['mc'] [substr ( $key, 0, 1 )] ) ? $cache_helper_config['memcache'] ['mc'] ['default'] : $cache_helper_config['memcache'] ['mc'] [substr ( $key, 0, 1 )];
            $mc_path = parse_url ( $mc_path );
            $key = ltrim ( $mc_path ['path'], '/' ) . '_' . $prefix . '_' . $key;
            if (empty ( $GLOBALS ['mc_' . $mc_path ['host']] ))
            {
                $GLOBALS ['mc_' . $mc_path ['host']] = new Memcache ( );
                $GLOBALS ['mc_' . $mc_path ['host']]->connect ( $mc_path ['host'], $mc_path ['port'] );
                //设置数据压缩门槛
                //$GLOBALS ['mc_' . $mc_path ['host']]->setCompressThreshold(2048, 0.2);
            }
            $result = $GLOBALS ['mc_' . $mc_path ['host']]->set ( $key, $value, MEMCACHE_COMPRESSED, $timeout );
            return $result;
        }
        $key = substr ( $key, 0, 2 ) . '/' . substr ( $key, 2, 2 ) . '/' . substr ( $key, 4, 2 ) . '/' . $key;
        $tmp ['data'] = $value;
        $tmp ['timeout'] = $timeout != 0 ? time () + ( int ) $timeout : 0;
        $cache_data = "<?php exit('dedecms');?>\n\r".@serialize ( $tmp );
        return @PutFile (DEDEDATA."/cache/$prefix/$key.php",$cache_data);
    }
}


/**
 *  删除缓存
 *
 * @access    public
 * @param     string  $prefix  前缀
 * @param     string  $key  键
 * @param     string  $is_memcache  是否为memcache缓存
 * @return    string
 */
if ( ! function_exists('DelCache'))
{
    /* 删缓存 */
    function DelCache($prefix, $key, $is_memcache = TRUE)
    {
        global $cache_helper_config;
        $key = md5 ( $key );
        /* 如果启用MC缓存 */
        if (! empty ( $cache_helper_config['memcache'] ) && $cache_helper_config['memcache'] ['is_mc_enable'] === TRUE && $is_memcache === TRUE)
        {
            $mc_path = empty ( $cache_helper_config['memcache'] ['mc'] [substr ( $key, 0, 1 )] ) ? $cache_helper_config['memcache'] ['mc'] ['default'] : $cache_helper_config['memcache'] ['mc'] [substr ( $key, 0, 1 )];
            $mc_path = parse_url ( $mc_path );
            $key = ltrim ( $mc_path ['path'], '/' ) . '_' . $prefix . '_' . $key;
            if (empty ( $GLOBALS ['mc_' . $mc_path ['host']] ))
            {
                $GLOBALS ['mc_' . $mc_path ['host']] = new Memcache ( );
                $GLOBALS ['mc_' . $mc_path ['host']]->connect ( $mc_path ['host'], $mc_path ['port'] );
            }
            return $GLOBALS ['mc_' . $mc_path ['host']]->delete ( $key );
        }
        $key = substr ( $key, 0, 2 ) . '/' . substr ( $key, 2, 2 ) . '/' . substr ( $key, 4, 2 ) . '/' . $key;
        return @unlink ( DEDEDATA . "/cache/$prefix/$key.php" );
    }
}

使用方法

写入方法

我们首先在ajax数据结束的地方,把数据写入缓存

SetCache('ajax', $name, $result, 1800);

ajax是文件夹

$name是文件名字

$result 是数据

1800 是缓存的时间

注意的是会把$name分割每2个字符创建一个文件夹,共创建3个,增加安全,$name会被md5编码

读取方法

在页面开始的地方,判断是否有缓存,注意写入的时间,如果时间没过期,则自动读取缓存,如果时间过期了 会返回false

注意name是写入时候的name

$ns=GetCache('ajax',$name);
if($ns){
  var_dump($ns);// 根据自己的方法返回数据即可
  exit();  
}
else{ //实际使用这句else删掉
    echo '没读取到缓存';
    exit();
}

删除方法

有时候我们需要最新数据,则可以删除掉缓存

注意name是写入时候的name

$ns=DelCache('ajax',$name);


相关内容

发表评论

验证码:
点击我更换图片

最新评论