Data.class.php 1.64 KB
<?php
/**
 * 数据级缓存(存储内容小于1MB)
 *
 * @name QLib_Utils_Cache_Data
 * @package Lib/Utils/Cache
 * @author whb whb@yoho.cn
 * @since 1.0
 */
class Lib_Utils_Cache_Data 
{
    private static $_key = 'yh_cache_data_';
    private static $_cache = null;
    private static $_options = array('domain' => 'yoho', 'class' => 'text');

    /**
     * @return object 缓存操作对象
     */
    private static function cache() 
    {
        if (! isset(self::$_cache)) 
        {
            self::$_cache = Util_Cache::factory(self::$_options);
        }
        return self::$_cache;
    }
    
    /**
     * 键名加密处理
     * 
     * @param string $key (键名)
     * @return string
     */
    private static function encryptKey($key) 
    {
        return md5(self::$_key . md5($key));
    }
    
    /**
     * 设置
     * 
     * @param string $key (键名)
     * @param source $data (数据)
     * @param integer $expire (有效期 默认为1天)
     * @return void
     */
    public static function set($key, $data, $expire = 86400) 
    {
        self::cache()->set(self::encryptKey($key), $data, $expire);
    }
    
    /**
     * 获取
     * 
     * @param string $key (键名)
     * @return source
     */
    public static function get($key) 
    {
        return self::cache()->get(self::encryptKey($key));
    }
    
    /**
     * 清除
     * 
     * @param string $key (键名)
     * @param integer $delay (延时时间,单位秒)
     * @return array | boolean
     * @since 0.4
     */
    public static function del($key, $delay = 0)
    {
        return self::cache()->del(self::encryptKey($key), $delay);
    }
    
}