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 |