PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 1.30.0
Independent Analytics – WordPress Analytics Plugin v1.30.0
2.15.5 2.15.4 2.15.3 2.15.2 2.15.1 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 2 years ago BladeOneCache.php 2 years ago BladeOneCacheRedis.php 2 years ago BladeOneCustom.php 2 years ago bladeonecli 2 years ago
BladeOneCacheRedis.php
155 lines
1 <?php
2
3 namespace IAWP_SCOPED\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 */
35 const CACHEREDIS_SCOPEURL = 1;
36 trait BladeOneCacheRedis
37 {
38 protected $curCacheId = 0;
39 protected $curCacheDuration = "";
40 protected $curCachePosition = 0;
41 protected $cacheRunning = \false;
42 /** @var \Redis $redis */
43 protected $redis;
44 protected $redisIP = '127.0.0.1';
45 protected $redisPort = 6379;
46 protected $redisTimeOut = 2.5;
47 protected $redisConnected = \false;
48 protected $redisNamespace = 'bladeonecache:';
49 protected $redisBase = 0;
50 private $cacheExpired = [];
51 // avoids to compare the file different times.
52 //<editor-fold desc="compile">
53 public function compileCache($expression)
54 {
55 // get id of template
56 // if the file exists then
57 // compare date.
58 // if the date is too old then re-save.
59 // else
60 // save for the first time.
61 return $this->phpTag . "echo \$this->cacheStart{$expression}; if(!\$this->cacheRunning) { ?>";
62 }
63 public function compileEndCache($expression)
64 {
65 return $this->phpTag . "} // if cacheRunning\necho \$this->cacheEnd{$expression}; ?>";
66 }
67 //</editor-fold>
68 public function connect($redisIP = null, $redisPort = null, $redisTimeOut = null)
69 {
70 if ($this->redisConnected) {
71 return \true;
72 }
73 if (!class_exists('Redis')) {
74 return \false;
75 // it requires redis.
76 }
77 if ($redisIP !== null) {
78 $this->redisIP = $redisIP;
79 $this->redisPort = $redisPort;
80 $this->redisTimeOut = $redisTimeOut;
81 }
82 $this->redis = new Redis();
83 // 2.5 sec timeout.
84 $this->redisConnected = $this->redis->connect($this->redisIP, $this->redisPort, $this->redisTimeOut);
85 return $this->redisConnected;
86 }
87 /**
88 * Returns true if the cache expired (or doesn't exist), otherwise false.
89 *
90 * @param string $templateName name of the template to use (such hello for template hello.blade.php)
91 * @param string $id (id of cache, optional, if not id then it adds automatically a number)
92 * @param int $scope scope of the cache.
93 * @param int $cacheDuration (duration of the cache in seconds)
94 * @return bool (return if the cache expired)
95 */
96 public function cacheExpired($templateName, $id, $scope, $cacheDuration)
97 {
98 if ($this->getMode() & 1) {
99 return \true;
100 // forced mode, hence it always expires. (fast mode is ignored).
101 }
102 $compiledFile = $this->getCompiledFile($templateName) . '_cache' . $id;
103 if (isset($this->cacheExpired[$compiledFile])) {
104 // if the information is already in the array then returns it.
105 return $this->cacheExpired[$compiledFile];
106 }
107 $date = @filemtime($compiledFile);
108 if ($date) {
109 if ($date + $cacheDuration < time()) {
110 $this->cacheExpired[$compiledFile] = \true;
111 return \true;
112 // time-out.
113 }
114 } else {
115 $this->cacheExpired[$compiledFile] = \true;
116 return \true;
117 // no file
118 }
119 $this->cacheExpired[$compiledFile] = \false;
120 return \false;
121 // cache active.
122 }
123 public function cacheStart($id = "", $cacheDuration = 86400)
124 {
125 $this->curCacheId = $id == "" ? $this->curCacheId + 1 : $id;
126 $this->curCacheDuration = $cacheDuration;
127 $this->curCachePosition = strlen(ob_get_contents());
128 $compiledFile = $this->getCompiledFile() . '_cache' . $this->curCacheId;
129 if ($this->cacheExpired('', $id, $cacheDuration)) {
130 $this->cacheRunning = \false;
131 } else {
132 $this->cacheRunning = \true;
133 $content = $this->getFile($compiledFile);
134 echo $content;
135 }
136 // getFile($fileName)
137 }
138 public function cacheEnd()
139 {
140 if (!$this->cacheRunning) {
141 $txt = substr(ob_get_contents(), $this->curCachePosition);
142 $compiledFile = $this->getCompiledFile() . '_cache' . $this->curCacheId;
143 file_put_contents($compiledFile, $txt);
144 }
145 $this->cacheRunning = \false;
146 }
147 private function keyByScope($scope)
148 {
149 $key = '';
150 if ($scope && CACHEREDIS_SCOPEURL) {
151 $key .= $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
152 }
153 }
154 }
155