chengkun
2025-05-22 3b321a2882db082c68aaf8771e0f55daa58a63d3
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
/**   
 * @param string $cache_dir 缓文件夹
 * @param int $cache_create_time 文件缓存时间
 * @example $cache=new Esj_Cache('./_cache',100) 
 * @example $cache->Read_Cache() 读取缓存并输出
 * @example $cache->creatre_cache() 创建缓存文件(放在文件未尾) 
 * @example $cache->CacheList() 返回所有缓存文件列表
 * @example $cache->Del_Cache() 删除所有缓存文件
 */
ob_start();
 
class Phpyun_Cache
{
 
    private $cache_dir = "./cache";
 // cacher文件夹
    private $web_dir = '';
 // 站点目录
    private $cache_time = 3600;
 // cache文件的建立时间
    public function __construct($cache_dir, $web_dir, $cache_time = 3600)
    {
        ob_start();
        $this->web_dir = $web_dir;
        $this->cache_dir = $cache_dir;
        $this->cache_time = $cache_time;
    }
 
    public function Read_Cache()
    { // 读取缓存
        if (count($_POST) > 0 || $_GET[m] == "ajax" || $_GET[m] == "includejs") {
            return false;
        }
        try {
            if (self::Create_Dir($this->cache_dir)) {
                self::Get_Cache(); // 输出缓存文件信息
            } else {
                echo "缓存文件夹创建失败!";
                return false;
            }
        } catch (Exception $e) {
            echo $e;
            return false;
        }
    }
 
    private function Exist_Dir($foler)
    { // 测试缓存文件夹是否存在
        if (@file_exists($this->web_dir . "/" . $foler)) {
            return true;
        } else {
            return false;
        }
    }
 
    private function Create_Dir($foler)
    { // 建立一个新的文件夹
        if (! self::Exist_Dir($foler)) {
            try {
                @mkdir($this->web_dir . "/" . $foler, 0777);
                @chmod($this->web_dir . "/" . $foler, 0777);
                return true;
            } catch (Exception $e) {
                self::Get_Cache(); // 输出缓存
                return false;
            }
            return false;
        } else {
            return true;
        }
    }
 
    private function Get_Cache()
    { // 读取缓存文件
        $file_name = self::get_CacheName();
        if (@file_exists($file_name) && ((filemtime($file_name) + $this->cache_time) > time())) {
            $content = @file_get_contents($file_name);
            if (isset($content)) {
                if (! empty($content)) {
                    echo $content;
                    ob_end_flush();
                    exit();
                }
            } else {
                echo "缓存文件读取失败";
                exit();
            }
        } elseif (@file_exists($file_name)) {
            
            $this->Del_Cache();
        }
    }
 
    private function get_CacheName()
    { // 返回文件的名字
        $name = $_SERVER['HTTP_HOST'] . $_SERVER["REQUEST_URI"];
        if ($_COOKIE['cityid']) {
            $name .= $_COOKIE['cityid'] . $_COOKIE['host'];
        }
        $filename = $file_name = $this->web_dir . '/' . $this->cache_dir . '/' . md5($name) . ".html";
        return $filename;
    }
 
    public function CacheCreate()
    { // 建立缓存文件
        $filename = self::get_CacheName();
        if ($filename != "") {
            try {
                $op = ob_get_contents();
                if (! empty($op)) {
                    @file_put_contents($filename, $op);
                }
                return true;
            } catch (Exception $e) {
                echo "缓存文件写入失败:" . $e;
                exit();
            }
            return true;
        }
    }
 
    public function CacheList()
    { // 取得缓存中的所有文件
        $path = $this->cache_dir;
        if ($handle = opendir($path)) {
            while (false !== ($file = readdir($handle))) {
                if ($file != "." && $file != "..") {
                    $path1 = $path . "/" . $file;
                    if (@file_exists($path1)) {
                        $result[] = $file;
                    }
                }
            }
            @closedir($handle);
        }
        return $result;
    }
 
    public function Del_Cache()
    { // 删除缓存中的所有文件
        $path = $this->web_dir . $this->cache_dir;
        if ($handle = @opendir($path)) {
            while (false !== ($file = @readdir($handle))) {
                if ($file != "." && $file != "..") {
                    $path1 = $path . "/" . $file;
                    if (@file_exists($path1)) {
                        @unlink($path1);
                    }
                }
            }
            @closedir($handle);
        }
        return true;
    }
}
?>