Issues (1519)

system/Inji/Cache.php (1 issue)

Labels
Severity
1
<?php
2
namespace Inji;
3
/**
4
 * Cache
5
 *
6
 * @author Alexey Krupskiy <[email protected]>
7
 * @link http://inji.ru/
8
 * @copyright 2015 Alexey Krupskiy
9
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
10
 */
11
class Cache {
12
13
    /**
14
     * Connection to a set of memcache servers
15
     *
16
     * @var Memcache
0 ignored issues
show
The type Inji\Memcache was not found. Did you mean Memcache? If so, make sure to prefix the type with \.
Loading history...
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() {
38
        if (!self::$connectTrying && class_exists('Memcache', false)) {
39
            self::$server = new Memcache();
40
            self::$connected = @self::$server->connect('localhost', 11211);
41
        }
42
        self::$connectTrying = true;
43
    }
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, $lifeTime = 3600, $prefix = false) {
56
        if (!self::$connected) {
57
            self::connect();
58
        }
59
        if (!self::$connected) {
60
            if (is_callable($callback, true)) {
61
                return $callback($params);
62
            }
63
            return false;
64
        }
65
        if ($prefix === false) {
66
            $prefix = App::$primary->name;
67
        }
68
69
        $val = @self::$server->get($prefix . '_' . $name . serialize($params));
70
        if ($val !== false) {
71
            return $val;
72
        } else {
73
            if (is_callable($callback, true)) {
74
                while (!\Inji::$inst->blockParallel()) {
75
                    sleep(1);
76
                    $val = @self::$server->get($prefix . '_' . $name . serialize($params));
77
                    if ($val !== false) {
78
                        return $val;
79
                    }
80
                }
81
                $val = $callback($params);
82
                \Inji::$inst->unBlockParallel();
83
                self::set($name, $params, $val, $lifeTime, $prefix);
84
                return $val;
85
            }
86
        }
87
        return false;
88
    }
89
90
    /**
91
     * Set value to cache
92
     *
93
     * @param string $name
94
     * @param array $params
95
     * @param mixed $val
96
     * @param int $lifeTime
97
     * @return boolean
98
     */
99
    public static function set($name, $params = [], $val = '', $lifeTime = 3600, $prefix = false) {
100
        if (!self::$connected) {
101
            self::connect();
102
        }
103
        if (!self::$connected) {
104
            return false;
105
        }
106
        if ($prefix === false) {
107
            $prefix = App::$primary->name;
108
        }
109
        return @self::$server->set($prefix . '_' . $name . serialize($params), $val, false, $lifeTime);
110
    }
111
112
    /**
113
     * Move file to cache folder and return path
114
     *
115
     * Also resize image when given resize params
116
     *
117
     * @param string $file
118
     * @param array $options
119
     * @return string
120
     */
121
    public static function file($file, $options = []) {
122
        $sizes = !empty($options['resize']) ? $options['resize'] : [];
123
        $crop = !empty($options['crop']) ? $options['crop'] : '';
124
        $pos = !empty($options['pos']) ? $options['pos'] : 'center';
125
        $fileinfo = pathinfo($file);
126
        $fileCheckSum = md5($fileinfo['dirname'] . filemtime($file));
127
        $path = static::getDir('static') . $fileCheckSum . '_' . $fileinfo['filename'];
128
        if ($sizes) {
129
            $path .= '.' . $sizes['x'] . 'x' . $sizes['y'] . $crop . $pos;
130
        }
131
        $path .= '.' . $fileinfo['extension'];
132
        if (!file_exists($path)) {
133
            copy($file, $path);
134
            if ($sizes) {
135
                Tools::resizeImage($path, $sizes['x'], $sizes['y'], $crop, $pos);
136
            }
137
        }
138
139
        return $path;
140
    }
141
142
    /**
143
     * Get cache dir for app
144
     *
145
     * @param App $app
146
     * @return string
147
     */
148
    public static function getDir($dirname, $app = null) {
149
        if (!$app) {
150
            $app = App::$primary;
151
        }
152
        $path = static::folder() . "{$dirname}/{$app->dir}/";
153
        Tools::createDir($path);
154
        return $path;
155
    }
156
157
    public static function folder() {
158
        return 'cache/';
159
    }
160
}