zoukankan      html  css  js  c++  java
  • PHP文件缓存函数

    发一个自己写的PHP文件缓存函数


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    /**
    +----------------------------------------------------------
    * PHP文件缓存函数
    * @author LiuYuanjun <http://www.liuyuanjun.com>
    +----------------------------------------------------------
    * @param string $key 缓存KEY 设为null则清空所有缓存
    * @param mixed $data 缓存内容 设为null则删除KEY为$key的缓存
    * @param int $time 缓存时间 秒数,如果大于当前时间戳则按照时间戳来处理,为0则永不过期
    +----------------------------------------------------------
    * @return mixed
    +----------------------------------------------------------
    */
    function file_cache($key, $data='', $time=0) {
        $cacheDir = ROOTPATH . 'Cache' . DS; //这里设置缓存目录
        $timestamp = time();
        if ($key === null) { //clear all
            $dirFiles = scandir($cacheDir);
            foreach ($dirFiles as $dirFile) {
                if ($dirFile != '.' && $dirFile != '..')
                    @unlink($cacheDir . $dirFile);
            }
        }
        $cachePath = $cacheDir . '~' . $key . '.php';
        if ($data === '') { //get cache
            if (!is_file($cachePath))
                return false;
            $cacheData = @require $cachePath;
            if ($cacheData[0] > 0 && $cacheData[0] < $timestamp) {
                @unlink($cachePath);
                return false;
            } else {
                return @unserialize($cacheData[1]);
            }
        }
        if ($data === null)
            @unlink($cachePath); //delete cache
        $time = intval($time);
        $deadline = $time == 0 ? 0 : ($time>$timestamp?$time:($timestamp+$time));
        $storeData = '<?php return array(' . $deadline . ',\'' . serialize($data) . '\'); ?>';
        @file_put_contents($cachePath, $storeData);
    }
  • 相关阅读:
    Spark开发环境搭建(IDEA、Scala、SVN、SBT)
    Spark源码系列:RDD repartition、coalesce 对比
    Scala:类和对象
    申请MS的FastCounter
    code generation part1some OOrelated topic! not completed
    [book]ADO.NET实用指南
    如果FC能把blog的WEB VIEW与AGG VIEW统计起来就方便多了
    Dell 1704
    O'Reilly .NET Framework Essentials, 2nd Edition
    单用户blog系统(一)
  • 原文地址:https://www.cnblogs.com/shihao/p/2320669.html
Copyright © 2011-2022 走看看