forked from EFTEC/BladeOne
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBladeOneCacheRedis.php
More file actions
141 lines (124 loc) · 4.93 KB
/
BladeOneCacheRedis.php
File metadata and controls
141 lines (124 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
namespace eftec\bladeone;
/**
* trait BladeOneCacheRedis
* Copyright (c) 2016 Jorge Patricio Castro Castillo MIT License. Don't delete this comment, its part of the license.
* Extends the tags of the class BladeOne. Its optional
* It adds the next tags to the template
* <code>
* @ cache([cacheid],[duration=86400]). The id is optional. The duration of the cache is in seconds
* // content here
* @ endcache()
* </code>
* It also adds a new function (optional) to the business or logic layer
* <code>
* if ($blade->cacheExpired('hellocache',1,5)) { //'helloonecache' =template, =1 id cache, 5=duration (seconds)
* // cache expired, so we should do some stuff (such as read from the database)
* }
* </code>
* @package BladeOneCacheRedis
* @version 0.1 2017-12-15 NOT YET IMPLEMENTED, ITS A WIP!!!!!!!!
* @link https://github.com/EFTEC/BladeOne
* @author Jorge Patricio Castro Castillo <jcastro arroba eftec dot cl>
*/
const CACHEREDIS_SCOPEURL=1;
trait BladeOneCacheRedis
{
var $curCacheId=0;
var $curCacheDuration="";
var $curCachePosition=0;
var $cacheRunning=false;
private $cacheExpired=[]; // avoids to compare the file different times.
/** @var \Redis $redis */
var $redis;
var $redisIP='127.0.0.1';
var $redisPort=6379;
var $redisTimeOut=2.5;
var $redisConnected=false;
var $redisNamespace='bladeonecache:';
var $redisBase=0;
//<editor-fold desc="compile">
public function compileCache($expression) {
// get id of template
// if the file exists then
// compare date.
// if the date is too old then re-save.
// else
// save for the first time.
return $this->phpTag."echo \$this->cacheStart{$expression}; if(!\$this->cacheRunning) { ?>";
}
public function compileEndCache($expression) {
return $this->phpTag."} // if cacheRunning\necho \$this->cacheEnd{$expression}; ?>";
}
//</editor-fold>
public function connect($redisIP=null,$redisPort=null,$redisTimeOut=null) {
if ($this->redisConnected) return true;
if (!class_exists('Redis')) return false; // it requires redis.
if ($redisIP!==null) {
$this->redisIP=$redisIP;
$this->redisPort=$redisPort;
$this->redisTimeOut=$redisTimeOut;
}
$this->redis = new \Redis();
$this->redisConnected=$this->redis->connect($this->redisIP, $this->redisPort, $this->redisTimeOut); // 2.5 sec timeout.
return $this->redisConnected;
}
private function keyByScope($scope) {
$key='';
if ($scope && CACHEREDIS_SCOPEURL) {
$key.=$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
}
}
/**
* Returns true if the cache expired (or doesn't exist), otherwise false.
* @param string $templateName name of the template to use (such hello for template hello.blade.php)
* @param string $id (id of cache, optional, if not id then it adds automatically a number)
* @param int $scope scope of the cache.
* @param int $cacheDuration (duration of the cache in seconds)
* @return bool (return if the cache expired)
*/
public function cacheExpired($templateName,$id,$scope,$cacheDuration) {
if ($this->getMode() & 1) {
return true; // forced mode, hence it always expires. (fast mode is ignored).
}
$compiledFile = $this->getCompiledFile($templateName).'_cache'.$id;
if (isset($this->cacheExpired[$compiledFile])) {
// if the information is already in the array then returns it.
return $this->cacheExpired[$compiledFile];
}
$date=@filemtime($compiledFile);
if ($date) {
if ($date+$cacheDuration <time()) {
$this->cacheExpired[$compiledFile]=true;
return true; // time-out.
}
} else {
$this->cacheExpired[$compiledFile]=true;
return true; // no file
}
$this->cacheExpired[$compiledFile]=false;
return false; // cache active.
}
public function cacheStart($id="",$cacheDuration=86400) {
$this->curCacheId=($id=="")?($this->curCacheId+1):$id;
$this->curCacheDuration=$cacheDuration;
$this->curCachePosition=strlen(ob_get_contents());
$compiledFile = $this->getCompiledFile().'_cache'.$this->curCacheId;
if ($this->cacheExpired('',$id,$cacheDuration)) {
$this->cacheRunning=false;
} else {
$this->cacheRunning=true;
$content=$this->getFile($compiledFile);
echo $content;
}
// getFile($fileName)
}
public function cacheEnd() {
if (!$this->cacheRunning) {
$txt = substr(ob_get_contents(), $this->curCachePosition);
$compiledFile = $this->getCompiledFile() . '_cache' . $this->curCacheId;
file_put_contents($compiledFile, $txt);
}
$this->cacheRunning=false;
}
}