search
Homephp教程php手册一个比较蛋疼的php缓存类

php缓存类
$oFC = new FileCache();
$sKey = 'ab_123';
$data = $oFC -> get($sKey);
if (is_null($data))
$oFC -> set($sKey, array('name' => 'ttt', 'datetime' => date('Y-m-d H:i:s')), 100000);
print_r($data);

 

	<?php final class FileCache
{
    /**
     * * * 缓存目录 *
     * 
     * @var string 
     */
    private static $msCachePath = null;
    /**
     * * * 默认缓存失效时间(1小时) *
     * 
     * @var int 
     */
    const miEXPIRE = 3600;
    /**
     * * * 构造<br /> * self::$msCachePath 缓存目录为共享目录 *
     * 
     * @param string $sCachePath 
     */
    function __construct($sCachePath = './tmp/')
    {
        if (is_null(self :: $msCachePath))
            self :: $msCachePath = $sCachePath;
    } 
    /**
     * * * 读取缓存<br> * 返回: 缓存内容,字符串或数组;缓存为空或过期返回null *
     * 
     * @param string $sKey 缓存键值(无需做md5()) *
     * @return string  null *
     * @access public 
     */
    public function get($sKey)
    {
        if (empty($sKey))
            return false;
        $sFile = self :: getFileName($sKey);
        if (!file_exists($sFile))
            return null;
        else
        {
            $handle = fopen($sFile, 'rb');

            if (intval(fgets($handle)) > time())
            { 
                // 检查时间戳 {	//未失效期,取出数据
                $sData = fread($handle, filesize($sFile));
                fclose($handle);
                return unserialize($sData);
            } 
            else
            { 
                // 已经失效期
                fclose($handle);
                return null;
            } 
        } 
    } 
    /**
     * 写入缓存 * *
     * 
     * @param string $sKey 缓存键值 *
     * @param mixed $mVal 需要保存的对象 *
     * @param int $iExpire 失效时间 *
     * @return bool 
     * @access public 
     */
    public function set($sKey, $mVal, $iExpire = null)
    {
        if (empty($sKey))
            return false;
        $sFile = self :: getFileName($sKey);
        if (!file_exists(dirname($sFile)))
            if (!self :: is_mkdir(dirname($sFile)))
                return false;
            $aBuf = array();
            $aBuf[] = time() + ((empty($iExpire)) ? self :: miEXPIRE : intval($iExpire));
            $aBuf[] = serialize($mVal);
            /**
             * 写入文件操作
             */
            $handle = fopen($sFile, 'wb');
            fwrite($handle, implode("\n", $aBuf));
            fclose($handle);
            return true;
        } 
        /**
         * * * 删除指定的缓存键值 *
         * 
         * @param string $sKey 缓存键值
         * @return bool 
         */

        public function del($sKey)
        {
            if (empty($sKey))
                return false;
            else
            {
                @unlink (self :: getFileName($sKey));
                return true;
            } 
        } 
        /**
         * * * 获取缓存文件全路径<br> * 返回: 缓存文件全路径<br> * $sKey的值会被转换成md5(),并分解为3级目录进行访问 *
         * 
         * @param string $sKey 缓存键 *
         * @return string 
         * @access protected 
         */

        private static function getFileName($sKey)
        {
            if (empty($sKey)) return false;
            $key_md5 = md5($sKey);
            /**
             * $aFileName = array();
             * $aFileName[] = rtrim(self :: $msCachePath, '/');
             * $aFileName[] = $key_md5{0} . $key_md5{1};
             * $aFileName[] = $key_md5{2} . $key_md5{3};
             * $aFileName[] = $key_md5{4} . $key_md5{5};
             * $aFileName[] = $key_md5;
             */
            return self :: $msCachePath . '/' . $key_md5; 
            // return implode('/', $aFileName);
        } 
        /**
         * * * 创建目录<br> * *
         * 
         * @param string $sDir 
         * @return bool 
         */
        private static function is_mkdir($sDir = '')
        {
            if (empty($sDir))
                return false;
            /**
             * 如果无法创建缓存目录,让系统直接抛出错误提示
             */
            echo $sDir;
            if (!mkdir($sDir, 0666))
                return false;
            else
                return true;
        } 
    } 
    



Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)