1 | <?php |
||
11 | class Cache { |
||
12 | |||
13 | /** |
||
14 | * Connection to a set of memcache servers |
||
15 | * |
||
16 | * @var Memcache |
||
17 | */ |
||
18 | public static $server = null; |
||
19 | |||
20 | /** |
||
21 | * Truing to connect flag |
||
22 | * |
||
23 | * @var boolean |
||
24 | */ |
||
25 | public static $connectTrying = false; |
||
26 | |||
27 | /** |
||
28 | * Connected flag |
||
29 | * |
||
30 | * @var boolean |
||
31 | */ |
||
32 | public static $connected = false; |
||
33 | |||
34 | /** |
||
35 | * Try connect to memcache server |
||
36 | */ |
||
37 | public static function connect() { |
||
44 | |||
45 | /** |
||
46 | * Get chached value |
||
47 | * |
||
48 | * If value not present, call callback |
||
49 | * |
||
50 | * @param string $name |
||
51 | * @param array $params |
||
52 | * @param callable $callback |
||
53 | * @return boolean |
||
54 | */ |
||
55 | public static function get($name, $params = [], $callback = null) { |
||
77 | |||
78 | /** |
||
79 | * Set value to cache |
||
80 | * |
||
81 | * @param string $name |
||
82 | * @param array $params |
||
83 | * @param mixed $val |
||
84 | * @param int $lifeTime |
||
85 | * @return boolean |
||
86 | */ |
||
87 | public static function set($name, $params = [], $val = '', $lifeTime = 3600) { |
||
96 | |||
97 | /** |
||
98 | * Move file to cache folder and return path |
||
99 | * |
||
100 | * Also resize image when given resize params |
||
101 | * |
||
102 | * @param string $file |
||
103 | * @param array $options |
||
104 | * @return string |
||
105 | */ |
||
106 | public static function file($file, $options = []) { |
||
107 | $sizes = !empty($options['resize']) ? $options['resize'] : []; |
||
108 | $crop = !empty($options['crop']) ? $options['crop'] : ''; |
||
109 | $pos = !empty($options['pos']) ? $options['pos'] : 'center'; |
||
110 | $fileinfo = pathinfo($file); |
||
111 | $fileCheckSum = md5($fileinfo['dirname'] . filemtime($file)); |
||
112 | $path = static::getDir('static') . $fileCheckSum . '_' . $fileinfo['filename']; |
||
113 | if ($sizes) { |
||
114 | $path .= '.' . $sizes['x'] . 'x' . $sizes['y'] . $crop . $pos; |
||
115 | } |
||
116 | $path .= '.' . $fileinfo['extension']; |
||
117 | if (!file_exists($path)) { |
||
118 | copy($file, $path); |
||
119 | if ($sizes) { |
||
120 | Tools::resizeImage($path, $sizes['x'], $sizes['y'], $crop, $pos); |
||
121 | } |
||
122 | } |
||
123 | |||
124 | return $path; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Get cache dir for app |
||
129 | * |
||
130 | * @param App $app |
||
131 | * @return string |
||
132 | */ |
||
133 | public static function getDir($dirname, $app = null) { |
||
141 | |||
142 | public static function folder() { |
||
145 | } |