Completed
Push — master ( e64f37...ec241f )
by Jared
02:07
created

CacheManager::decode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: jaredchu
5
 * Date: 04/08/2017
6
 * Time: 11:36
7
 */
8
9
namespace JC;
10
11
/**
12
 * Class CacheManager
13
 * @package JC
14
 *
15
 * Manage the list of caching file and object
16
 * Save in $cFilename
17
 */
18
class CacheManager
19
{
20
    /**
21
     * @var string
22
     */
23
    public static $cFileName = 'jc-simple-cache-list';
24
25
    /**
26
     * @param $cFileName
27
     */
28
    public static function setCFileName($cFileName)
29
    {
30
        static::$cFileName = $cFileName;
31
    }
32
33
    /**
34
     * @return string
35
     */
36
    public static function getCFileName()
37
    {
38
        return static::$cFileName;
39
    }
40
41
    /**
42
     * @return string
43
     *
44
     * Return absolute file path which store cache list
45
     */
46
    public static function getCFilePath()
47
    {
48
        return sys_get_temp_dir() . '/' . static::getCFileName();
49
    }
50
51
    /**
52
     * @param $key
53
     * @return bool|string
54
     *
55
     * Return absolute file path of caching object
56
     */
57
    public static function get($key)
58
    {
59
        $cacheList = static::getCacheList();
60
        if (isset($cacheList[$key])) {
61
            if (($cacheList[$key][0] == 0 || $cacheList[$key][0] >= time())) {
62
                return $cacheList[$key][1];
63
            } else {
64
                unlink($cacheList[$key][1]);
65
                static::remove($key);
66
            }
67
        }
68
69
        return false;
70
    }
71
72
    /**
73
     * @param $key
74
     * @param $filePath
75
     * @return bool
76
     *
77
     * Save $key and $filePath in cacheList
78
     */
79
    public static function set($key, $filePath, $ttl)
80
    {
81
        $cacheList = static::getCacheList();
82
83
        if ($ttl != 0) {
84
            $ttl += time();
85
        }
86
        $cacheList[$key] = [$ttl, $filePath];
87
88
        return static::setCacheList($cacheList);
89
    }
90
91
    /**
92
     * @param $key
93
     * @return bool
94
     *
95
     * Remove caching object file and remove from cache list
96
     */
97
    public static function remove($key)
98
    {
99
        $cacheList = static::getCacheList();
100
        unset($cacheList[$key]);
101
102
        return static::setCacheList($cacheList);
103
    }
104
105
    /**
106
     * @param $key
107
     * @return bool
108
     */
109
    public static function has($key)
110
    {
111
        $cacheList = static::getCacheList();
112
        return array_key_exists($key, $cacheList);
113
    }
114
115
    /**
116
     * @return array
117
     *
118
     * Return cache list in array
119
     */
120
    protected static function getCacheList()
121
    {
122
        if (file_exists(static::getCFilePath())) {
123
            return static::decode(file_get_contents(static::getCFilePath()));
124
        }
125
126
        return array();
127
    }
128
129
    /**
130
     * @param $cacheList
131
     * @return bool
132
     */
133
    protected static function setCacheList($cacheList)
134
    {
135
        return (bool)file_put_contents(static::getCFilePath(), static::encode($cacheList));
136
    }
137
138
    /**
139
     * @param $array
140
     * @return string
141
     */
142
    protected static function encode($array)
143
    {
144
        return json_encode($array);
145
    }
146
147
    /**
148
     * @param $string
149
     * @return array
150
     */
151
    protected static function decode($string)
152
    {
153
        return json_decode($string, true);
154
    }
155
}