发布于 2014-10-24 03:55:01 | 498 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

ThinkPHP开源PHP框架

ThinkPHP是一个开源的PHP框架,是为了简化企业级应用开发和敏捷WEB应用开发而诞生的。ThinkPHP可以支持windows/Unix/Liunx等服务器环境,正式版需要PHP5.0以上版本支持,支持MySql、PgSQL、Sqlite以及PDO等多种数据库


本文是一个thinkphp下的缓存控制器,实现了删除所有缓存  Rumtime 以及 Html 静态缓存的功能,感兴趣的同学参考下。

缓存控制器
<?php
/**
 * This is not a free software, All Copyright @F.Z.B
 * Date: 14-8-12 下午4:08
 * File: CacheController.class.php
 * Author: default.fu@foxmail.com
 */

namespace Api\Controller;

class CacheController extends InitController
{
    public function cleanAll()
    {
        $this->totalSize = 0;
        $this->totalFile = 0;
        $GLOBALS['arrFiles'] = $GLOBALS['arrDirs'] = array();

        $this->dropDir(HTML_PATH);
        $this->dropDir(RUNTIME_PATH);

        //ThinkPHP 3.2 不会自动生成Html缓存目录
        if (!is_dir(HTML_PATH)) mkdir(HTML_PATH);

        $data = array(
            'totalFile' => $this->totalFile,
            'totalSize' => byte_format($this->totalSize),
            'arrFiles'  => $GLOBALS['arrFiles'],
            'arrDirs'   => $GLOBALS['arrDirs'],
            'result'    => 1,
            'reqtime'   => date('Y-m-d H:i:s'),
        );

        $returnType = I('type') == 'JSON' ? 'JSON' : 'JSONP';
        $this->ajaxReturn($data, $returnType);

    }

    public function dropDir($path = '')
    {
        $path = trimRepeatSlash($path);
        if (is_file($path)) {
            $this->totalSize += filesize($path);
            $this->totalFile++;
            $GLOBALS['arrFiles'][] = $path;
            unlink($path);

        } else if (is_dir($path)) {
            if (($dir = opendir($path)) !== false) {
                while (($file = readdir($dir)) !== false) {
                    if ($file != '.' && $file != '..') {
                        $this->dropDir($path . '/' . $file);
                    }
                }

                $GLOBALS['arrDirs'][] = $path;
                rmdir($path);
            }
        }
    }
}


thinkphp3.2.x设置缓存开启

<?php
return array(
    //'配置项'=>'配置值'
    'LAYOUT_ON'        => true,
    'HTML_CACHE_ON'    => strpos($_SERVER['HTTP_HOST'], '.') !== false, // 开启静态缓存 默认为 true 本地不开启
    'HTML_CACHE_TIME'  => 3600, // 全局静态缓存有效期(秒)
    'HTML_FILE_SUFFIX' => '.shtml', // 设置静态缓存文件后缀
    'HTML_CACHE_RULES' => array(
        '*' => array('{:module}/{:controller}/{:action}/{$_SERVER.REQUEST_URI|md5}', 3600, 'trimSW'),
    )
);


trimSW函数

/**
 * @author      default.fu@foxmail.com
 * @description 去除 空格 和非\w 字符串,用于cache 配置
 *
 * @param        $str
 * @param string $emptyValue
 *
 * @return mixed|string
 */
function trimSW($str, $emptyValue = '_empty_')
{
    $str = preg_replace('/([^\w\/]+)/', '-', $str);
    if (empty($str)) {
        $str = $emptyValue;
    }

    return $str;
}


最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务