colly_wyx
2018-04-27 74adf3a72663f151dc2c1b87ecb4ea4b0e080a50
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
<?php
/**
 * PhalApi_Cache_File 文件缓存
 *
 * @package     PhalApi\Cache
 * @license     http://www.phalapi.net/license GPL 协议
 * @link        http://www.phalapi.net/
 * @author      dogstar <chanzonghuang@gmail.com> 2015-02-26
 */
 
class PhalApi_Cache_File implements PhalApi_Cache {
 
    protected $folder;
 
    protected $prefix;
 
    public function __construct($config) {
        $this->folder = rtrim($config['path'], '/');
 
        $cacheFolder = $this->createCacheFileFolder();
 
        if (!is_dir($cacheFolder)) {
            mkdir($cacheFolder, 0777, TRUE);
        }
 
        $this->prefix = isset($config['prefix']) ? $config['prefix'] : 'phapapi';
    }
 
    public function set($key, $value, $expire = 600) {
        if ($key === NULL || $key === '') {
            return;
        }
 
        $filePath = $this->createCacheFilePath($key);
 
        $expireStr = sprintf('%010d', $expire + time());
        if (strlen($expireStr) > 10) {
            throw new PhalApi_Exception_InternalServerError(
                T('file expire is too large')
            );
        }
 
        if (!file_exists($filePath)) {
            touch($filePath);
            chmod($filePath, 0777);
        }
        file_put_contents($filePath, $expireStr . serialize($value));
    }
 
    public function get($key) {
        $filePath = $this->createCacheFilePath($key);
 
        if (file_exists($filePath)) {
            $expireTime = file_get_contents($filePath, FALSE, NULL, 0, 10);
 
            if ($expireTime > time()) {
                return @unserialize(file_get_contents($filePath, FALSE, NULL, 10));
            }
        }
 
        return NULL;
    }
 
    public function delete($key) {
        if ($key === NULL || $key === '') {
            return;
        }
 
        $filePath = $this->createCacheFilePath($key);
 
        @unlink($filePath);
    }
 
    /**
     * 考虑到Linux同一目录下的文件个数限制,这里拆分成1000个文件缓存目录
     */
    protected function createCacheFilePath($key) {
        $folderSufix = sprintf('%03d', hexdec(substr(sha1($key), -5)) % 1000);
        $cacheFolder = $this->createCacheFileFolder() . DIRECTORY_SEPARATOR . $folderSufix;
        if (!is_dir($cacheFolder)) {
            mkdir($cacheFolder, 0777, TRUE);
        }
 
        return $cacheFolder . DIRECTORY_SEPARATOR . md5($key) . '.dat';
    }
 
    protected function createCacheFileFolder() {
        return $this->folder . DIRECTORY_SEPARATOR . 'cache';
    }
}