PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 2.15.0
Independent Analytics – WordPress Analytics Plugin v2.15.0
2.15.0 2.14.10 trunk 1.1 1.10 1.10.1 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.17.1 1.17.2 1.17.3 1.17.4 1.18 1.18.1 1.19.0 1.19.1 1.2 1.20.0 1.21.0 1.22.0 1.22.1 1.23.0 1.23.1 1.24.0 1.24.1 1.25.0 1.25.1 1.26.0 1.27.0 1.28.0 1.28.1 1.28.2 1.28.3 1.29.0 1.3 1.30.0 1.30.1 1.4 1.5 1.6 1.7 1.8 1.9 2.0.0 2.0.1 2.1.4 2.1.5 2.1.6 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.10 2.11.2 2.11.3 2.11.4 2.11.5 2.11.6 2.11.7 2.11.8 2.11.9 2.12.0 2.12.1 2.12.2 2.13.1 2.13.2 2.13.5 2.13.6 2.14.0 2.14.1 2.14.2 2.14.4 2.14.6 2.14.7 2.14.8 2.14.9 2.2.0 2.2.1 2.3.1 2.3.2 2.4.2 2.4.3 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.7.1 2.7.2 2.7.3 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7
independent-analytics / vendor / eftec / bladeone / lib / BladeOneCacheRedis.php
independent-analytics / vendor / eftec / bladeone / lib Last commit date
BladeOne.php 4 days ago BladeOneCache.php 4 days ago BladeOneCacheRedis.php 2 years ago BladeOneCustom.php 4 days ago bladeonecli 2 years ago
BladeOneCacheRedis.php
157 lines
1 <?php
2
3 namespace IAWPSCOPED\eftec\bladeone;
4
5 use Redis;
6 use function class_exists;
7 use function file_put_contents;
8 use function filemtime;
9 use function ob_get_contents;
10 use function strlen;
11 use function substr;
12 use function time;
13 /**
14 * trait BladeOneCacheRedis
15 * Copyright (c) 2016 Jorge Patricio Castro Castillo MIT License. Don't delete this comment, its part of the license.
16 * Extends the tags of the class BladeOne. Its optional
17 * It adds the next tags to the template
18 * <code>
19 * @ cache([cacheid],[duration=86400]). The id is optional. The duration of the cache is in seconds
20 * // content here
21 * @ endcache()
22 * </code>
23 * It also adds a new function (optional) to the business or logic layer
24 * <code>
25 * if ($blade->cacheExpired('hellocache',1,5)) { //'helloonecache' =template, =1 id cache, 5=duration (seconds)
26 * // cache expired, so we should do some stuff (such as read from the database)
27 * }
28 * </code>
29 *
30 * @package BladeOneCacheRedis
31 * @version 0.1 2017-12-15 NOT YET IMPLEMENTED, ITS A WIP!!!!!!!!
32 * @link https://github.com/EFTEC/BladeOne
33 * @author Jorge Patricio Castro Castillo <jcastro arroba eftec dot cl>
34 * @internal
35 */
36 const CACHEREDIS_SCOPEURL = 1;
37 /** @internal */
38 trait BladeOneCacheRedis
39 {
40 protected $curCacheId = 0;
41 protected $curCacheDuration = "";
42 protected $curCachePosition = 0;
43 protected $cacheRunning = \false;
44 /** @var \Redis $redis */
45 protected $redis;
46 protected $redisIP = '127.0.0.1';
47 protected $redisPort = 6379;
48 protected $redisTimeOut = 2.5;
49 protected $redisConnected = \false;
50 protected $redisNamespace = 'bladeonecache:';
51 protected $redisBase = 0;
52 private $cacheExpired = [];
53 // avoids to compare the file different times.
54 //<editor-fold desc="compile">
55 public function compileCache($expression)
56 {
57 // get id of template
58 // if the file exists then
59 // compare date.
60 // if the date is too old then re-save.
61 // else
62 // save for the first time.
63 return $this->phpTag . "echo \$this->cacheStart{$expression}; if(!\$this->cacheRunning) { ?>";
64 }
65 public function compileEndCache($expression)
66 {
67 return $this->phpTag . "} // if cacheRunning\necho \$this->cacheEnd{$expression}; ?>";
68 }
69 //</editor-fold>
70 public function connect($redisIP = null, $redisPort = null, $redisTimeOut = null)
71 {
72 if ($this->redisConnected) {
73 return \true;
74 }
75 if (!class_exists('Redis')) {
76 return \false;
77 // it requires redis.
78 }
79 if ($redisIP !== null) {
80 $this->redisIP = $redisIP;
81 $this->redisPort = $redisPort;
82 $this->redisTimeOut = $redisTimeOut;
83 }
84 $this->redis = new Redis();
85 // 2.5 sec timeout.
86 $this->redisConnected = $this->redis->connect($this->redisIP, $this->redisPort, $this->redisTimeOut);
87 return $this->redisConnected;
88 }
89 /**
90 * Returns true if the cache expired (or doesn't exist), otherwise false.
91 *
92 * @param string $templateName name of the template to use (such hello for template hello.blade.php)
93 * @param string $id (id of cache, optional, if not id then it adds automatically a number)
94 * @param int $scope scope of the cache.
95 * @param int $cacheDuration (duration of the cache in seconds)
96 * @return bool (return if the cache expired)
97 */
98 public function cacheExpired($templateName, $id, $scope, $cacheDuration)
99 {
100 if ($this->getMode() & 1) {
101 return \true;
102 // forced mode, hence it always expires. (fast mode is ignored).
103 }
104 $compiledFile = $this->getCompiledFile($templateName) . '_cache' . $id;
105 if (isset($this->cacheExpired[$compiledFile])) {
106 // if the information is already in the array then returns it.
107 return $this->cacheExpired[$compiledFile];
108 }
109 $date = @filemtime($compiledFile);
110 if ($date) {
111 if ($date + $cacheDuration < time()) {
112 $this->cacheExpired[$compiledFile] = \true;
113 return \true;
114 // time-out.
115 }
116 } else {
117 $this->cacheExpired[$compiledFile] = \true;
118 return \true;
119 // no file
120 }
121 $this->cacheExpired[$compiledFile] = \false;
122 return \false;
123 // cache active.
124 }
125 public function cacheStart($id = "", $cacheDuration = 86400)
126 {
127 $this->curCacheId = $id == "" ? $this->curCacheId + 1 : $id;
128 $this->curCacheDuration = $cacheDuration;
129 $this->curCachePosition = strlen(ob_get_contents());
130 $compiledFile = $this->getCompiledFile() . '_cache' . $this->curCacheId;
131 if ($this->cacheExpired('', $id, $cacheDuration)) {
132 $this->cacheRunning = \false;
133 } else {
134 $this->cacheRunning = \true;
135 $content = $this->getFile($compiledFile);
136 echo $content;
137 }
138 // getFile($fileName)
139 }
140 public function cacheEnd()
141 {
142 if (!$this->cacheRunning) {
143 $txt = substr(ob_get_contents(), $this->curCachePosition);
144 $compiledFile = $this->getCompiledFile() . '_cache' . $this->curCacheId;
145 file_put_contents($compiledFile, $txt);
146 }
147 $this->cacheRunning = \false;
148 }
149 private function keyByScope($scope)
150 {
151 $key = '';
152 if ($scope && CACHEREDIS_SCOPEURL) {
153 $key .= $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
154 }
155 }
156 }
157