Skip to content

Commit a737eec

Browse files
committed
phpFastCache integration +other cache sys
1 parent 4304d8a commit a737eec

14 files changed

+295
-513
lines changed

micro/cache/system/AbstractDataCache.php

+13-3
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,23 @@ public function expired($key, $duration) {
4747
* Caches the given data with the given key.
4848
* @param string $key cache key
4949
* @param string $code the source-code to be cached
50+
* @param string tag the item tag
51+
* @param boolean $php
5052
* @throws AnnotationException if file could not be written
5153
*/
52-
public function store($key, $code, $php=true) {
54+
public function store($key, $code,$tag=null, $php=true) {
5355
$content="";
5456
if ($php)
5557
$content=self::PHP_TAG;
5658
$content.=$code . "\n";
57-
$this->storeContent($key, $content);
59+
$this->storeContent($key, $content,$tag);
5860
}
5961

6062
public function getRoot() {
6163
return $this->_root;
6264
}
6365

64-
abstract protected function storeContent($key,$content);
66+
abstract protected function storeContent($key,$content,$tag);
6567

6668
/**
6769
* Fetches data stored for the given key.
@@ -89,4 +91,12 @@ abstract public function remove($key);
8991

9092
abstract public function clear();
9193

94+
abstract public function getCacheFiles($type);
95+
96+
abstract public function clearCache($type);
97+
98+
public function getCacheInfo(){
99+
return "Cache system is an instance of <b>".\get_class($this)."</b>.";
100+
}
101+
92102
}

micro/cache/system/ApcuCache.php

+45-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
<?php
22
namespace micro\cache\system;
33

4+
use micro\utils\StrUtils;
5+
use micro\controllers\admin\popo\CacheFile;
6+
47
/**
5-
* This class is responsible for storing Arrays in PHP files.
8+
* This class is responsible for storing values in apcu cache.
69
*/
710
class ApcuCache extends AbstractDataCache{
811
/**
@@ -21,21 +24,22 @@ public function exists($key) {
2124
return \apcu_exists($this->getRealKey($key));
2225
}
2326

24-
public function store($key, $code, $php=true) {
25-
$this->storeContent($key, $code);
27+
public function store($key, $code, $tag,$php=true) {
28+
$this->storeContent($key, $code,$tag);
2629
}
2730

2831
/**
2932
* Caches the given data with the given key.
3033
* @param string $key cache key
3134
* @param string $content the source-code to be cached
35+
* @param string $tag not used
3236
*/
33-
protected function storeContent($key,$content) {
37+
protected function storeContent($key,$content,$tag) {
3438
\apcu_store($this->getRealKey($key), $content);
3539
}
3640

3741
protected function getRealKey($key){
38-
return \md5($key);
42+
return $key;
3943
}
4044

4145
/**
@@ -65,7 +69,7 @@ public function file_get_contents($key) {
6569
*/
6670
public function getTimestamp($key) {
6771
$key=$this->getRealKey($key);
68-
$cache = \apc_cache_info();
72+
$cache = \apcu_cache_info();
6973
if (empty($cache['cache_list'])) {
7074
return false;
7175
}
@@ -86,4 +90,39 @@ public function remove($key) {
8690
public function clear() {
8791
\apcu_clear_cache();
8892
}
93+
94+
protected function getCacheEntries($type){
95+
$entries=$this->getAllEntries();
96+
return \array_filter($entries,function($v) use ($type){return StrUtils::startswith($v['info'], $type);});
97+
}
98+
99+
protected function getAllEntries(){
100+
$entries=[];
101+
$cache = \apcu_cache_info();
102+
if (!empty($cache['cache_list'])) {
103+
$entries=$cache['cache_list'];
104+
}
105+
return $entries;
106+
}
107+
108+
public function getCacheFiles($type){
109+
$result=[];
110+
$entries=$this->getCacheEntries($type);
111+
foreach ($entries as $entry) {
112+
$key=$entry['info'];
113+
if(StrUtils::startswith($key, $type)){
114+
$result[]=new CacheFile(\ucfirst($type),$key,$entry['creation_time'],$entry['mem_size'],$key);
115+
}
116+
}
117+
if(\sizeof($result)===0)
118+
$result[]=new CacheFile(\ucfirst($type),"","","");
119+
return $result;
120+
}
121+
122+
public function clearCache($type){
123+
$entries=$this->getCacheEntries($type);
124+
foreach ($entries as $entry){
125+
$this->remove($entry['info']);
126+
}
127+
}
89128
}

micro/cache/system/ArrayCache.php

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<?php
22
namespace micro\cache\system;
33

4+
use micro\controllers\admin\popo\CacheFile;
5+
use micro\cache\CacheManager;
6+
47
/**
58
* This class is responsible for storing Arrays in PHP files.
69
*/
@@ -39,7 +42,7 @@ public function exists($key) {
3942
* @param string $content the source-code to be cached
4043
* @throws AnnotationException if file could not be written
4144
*/
42-
protected function storeContent($key,$content) {
45+
protected function storeContent($key,$content,$tag) {
4346
$path=$this->_getPath($key);
4447
if (@\file_put_contents($path, $content, LOCK_EX) === false) {
4548
throw new \Exception("Unable to write cache file: {$path}");
@@ -100,4 +103,18 @@ public function clear() {
100103
\unlink($file);
101104
}
102105
}
106+
107+
public function getCacheFiles($type){
108+
return CacheFile::initFromFiles(ROOT . DS .CacheManager::getCacheDirectory().$type, \ucfirst($type),function($file) use($type){$file=\basename($file);return $type."/".substr($file, 0, strpos($file, $this->postfix.'.php'));});
109+
}
110+
111+
public function clearCache($type){
112+
CacheFile::delete(ROOT . DS .CacheManager::getCacheDirectory().\strtolower($type));
113+
}
114+
115+
public function getCacheInfo(){
116+
$result=parent::getCacheInfo();
117+
$result.="<br>Root cache directory is <b>".$this->_root."</b>.";
118+
return $result;
119+
}
103120
}
+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
namespace micro\cache\system;
3+
4+
use micro\controllers\admin\popo\CacheFile;
5+
use phpFastCache\CacheManager;
6+
use phpFastCache\Core\Pool\ExtendedCacheItemPoolInterface;
7+
8+
/**
9+
* This class is responsible for storing values with PhpFastCache.
10+
*/
11+
class PhpFastCacheDriver extends AbstractDataCache{
12+
/**
13+
* @var ExtendedCacheItemPoolInterface
14+
*/
15+
private $cacheInstance;
16+
/**
17+
* Initializes the cache-provider
18+
*/
19+
public function __construct($root,$postfix="",$cacheType="Mongodb") {
20+
parent::__construct($root,$postfix);
21+
$this->cacheInstance = CacheManager::getInstance($cacheType,["itemDetailedDate"=>true,'host' => '127.0.0.1',
22+
'port' => '27017',
23+
'username' => '',
24+
'password' => '',
25+
'timeout' => '1']);
26+
}
27+
28+
/**
29+
* Check if annotation-data for the key has been stored.
30+
* @param string $key cache key
31+
* @return boolean true if data with the given key has been stored; otherwise false
32+
*/
33+
public function exists($key) {
34+
return $this->cacheInstance->hasItem($this->getRealKey($key));
35+
}
36+
37+
public function store($key, $code,$tag=null, $php=true) {
38+
$this->storeContent($key, $code,$tag);
39+
}
40+
41+
/**
42+
* Caches the given data with the given key.
43+
* @param string $key cache key
44+
* @param string $content the source-code to be cached
45+
* @param string $tag
46+
*/
47+
protected function storeContent($key,$content,$tag) {
48+
$key=$this->getRealKey($key);
49+
$item=$this->cacheInstance->getItem($key);
50+
$item->set($content);
51+
$item->addTag($tag);
52+
$this->cacheInstance->save($item);
53+
}
54+
55+
protected function getRealKey($key){
56+
$key=\str_replace("/", "-", $key);
57+
return \str_replace("\\", "-", $key);
58+
}
59+
60+
/**
61+
* Fetches data stored for the given key.
62+
* @param string $key cache key
63+
* @return mixed the cached data
64+
*/
65+
public function fetch($key) {
66+
$result=$this->cacheInstance->getItem($this->getRealKey($key))->get();
67+
return eval($result);
68+
}
69+
70+
/**
71+
* return data stored for the given key.
72+
* @param string $key cache key
73+
* @return mixed the cached data
74+
*/
75+
public function file_get_contents($key) {
76+
return $this->cacheInstance->getItem($this->getRealKey($key))->get();
77+
}
78+
79+
/**
80+
* Returns the timestamp of the last cache update for the given key.
81+
*
82+
* @param string $key cache key
83+
* @return int unix timestamp
84+
*/
85+
public function getTimestamp($key) {
86+
$key=$this->getRealKey($key);
87+
return $this->cacheInstance->getItem($key)->getCreationDate()->getTimestamp();
88+
}
89+
90+
public function remove($key) {
91+
$key=$this->getRealKey($key);
92+
$this->cacheInstance->deleteItem($this->getRealKey($key));
93+
}
94+
95+
public function clear() {
96+
$this->cacheInstance->clear();
97+
}
98+
99+
protected function getCacheEntries($type){
100+
return $this->cacheInstance->getItemsByTag($type);
101+
}
102+
103+
public function getCacheFiles($type){
104+
$result=[];
105+
$entries=$this->getCacheEntries($type);
106+
107+
foreach ($entries as $entry) {
108+
$key=$entry->getKey();
109+
$result[]=new CacheFile(\ucfirst($type),$key,$entry->getCreationDate()->getTimestamp(),"",$key);
110+
}
111+
if(\sizeof($result)===0)
112+
$result[]=new CacheFile(\ucfirst($type),"","","");
113+
return $result;
114+
}
115+
116+
public function clearCache($type){
117+
$this->cacheInstance->deleteItemsByTag($type);
118+
}
119+
}

micro/cache/traits/ModelsCacheTrait.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public static function createOrmModelCache($classname) {
1717
if (!self::$cache->exists($key)) {
1818
$p=new ModelParser();
1919
$p->parse($classname);
20-
self::$cache->store($key, $p->__toString());
20+
self::$cache->store($key, $p->__toString(),'models');
2121
}
2222
return self::$cache->fetch($key);
2323
}

micro/cache/traits/RestCacheTrait.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ private static function initRestCache(&$config,$silent=false) {
2626
$restCache=\array_merge($restCache,$parser->asArray());
2727
}
2828
}
29-
self::$cache->store("controllers/rest", "return " . JArray::asPhpArray($restCache, "array") . ";");
29+
self::$cache->store("controllers/rest", "return " . JArray::asPhpArray($restCache, "array") . ";",'controllers');
3030
if(!$silent){
3131
echo "Rest cache reset\n";
3232
}

micro/cache/traits/RouterCacheTrait.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,16 @@ private static function initRouterCache(&$config,$silent=false) {
4545

4646
}
4747
}
48-
self::$cache->store("controllers/routes.default", "return " . JArray::asPhpArray($routes["default"], "array") . ";");
49-
self::$cache->store("controllers/routes.rest", "return " . JArray::asPhpArray($routes["rest"], "array") . ";");
48+
self::$cache->store("controllers/routes.default", "return " . JArray::asPhpArray($routes["default"], "array") . ";",'controllers');
49+
self::$cache->store("controllers/routes.rest", "return " . JArray::asPhpArray($routes["rest"], "array") . ";",'controllers');
5050
if(!$silent){
5151
echo "Router cache reset\n";
5252
}
5353
}
5454

5555
private static function storeRouteResponse($key, $response) {
5656
self::setKeyExpired($key, false);
57-
self::$cache->store("controllers/" . $key, $response, false);
57+
self::$cache->store("controllers/" . $key, $response, 'controllers',false);
5858
return $response;
5959
}
6060

@@ -144,7 +144,7 @@ public static function getControllerRoutes($controllerClass,$isRest=false){
144144
public static function addRoute($path, $controller, $action="index", $methods=null, $name="") {
145145
$controllerCache=self::getControllerCache();
146146
Router::addRouteToRoutes($controllerCache, $path, $controller, $action, $methods, $name);
147-
self::$cache->store("controllers/routes", "return " . JArray::asPhpArray($controllerCache, "array") . ";");
147+
self::$cache->store("controllers/routes", "return " . JArray::asPhpArray($controllerCache, "array") . ";",'controllers');
148148
}
149149

150150
public static function getControllersFiles(&$config,$silent=false){

micro/controllers/admin/UbiquityMyAdminBaseController.php

+4-21
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,11 @@ protected function addNavigationTesting(){
182182
}
183183

184184
public function cache(){
185-
$cacheDirectory=CacheManager::getCacheDirectory();
186-
$config=Startup::getConfig();
187185
$this->getHeader("cache");
188-
$this->showSimpleMessage("Cache directory is <b>".FsUtils::cleanPathname(ROOT.DS.$cacheDirectory)."</b>", "info","info circle",null,"msgCache");
189-
$cacheFiles=CacheFile::init(ROOT . DS .$cacheDirectory."controllers", "Controllers");
190-
$cacheFiles=\array_merge($cacheFiles,CacheFile::init(ROOT . DS .$cacheDirectory."models", "Models"));
186+
$this->showSimpleMessage(CacheManager::$cache->getCacheInfo(), "info","info circle",null,"msgCache");
187+
188+
$cacheFiles=CacheManager::$cache->getCacheFiles('controllers');
189+
$cacheFiles=\array_merge($cacheFiles,CacheManager::$cache->getCacheFiles('models'));
191190
$form=$this->jquery->semantic()->htmlForm("frmCache");
192191
$radios=HtmlFormFields::checkeds("cacheTypes[]",["controllers"=>"Controllers","models"=>"Models","views"=>"Views","queries"=>"Queries","annotations"=>"Annotations"],"Display cache types: ",["controllers","models"]);
193192
$radios->postFormOnClick($this->_getAdminFiles()->getAdminBaseRoute()."/setCacheTypes","frmCache","#dtCacheFiles tbody",["jqueryDone"=>"replaceWith"]);
@@ -396,22 +395,6 @@ public function createController($force=null){
396395
$this->controllers();
397396
}
398397

399-
public function _showFileContent(){
400-
if(RequestUtils::isPost()){
401-
$type=$_POST["type"];$filename=$_POST["filename"];
402-
if(\file_exists($filename)){
403-
$modal=$this->jquery->semantic()->htmlModal("file",$type." : ".\basename($filename));
404-
$frm=new HtmlForm("frmShowFileContent");
405-
$frm->addTextarea("file-content", null,\file_get_contents($filename),"",10);
406-
$modal->setContent($frm);
407-
$modal->addAction("Close");
408-
$this->jquery->exec("$('#file').modal('show');",true);
409-
echo $modal;
410-
echo $this->jquery->compile($this->view);
411-
}
412-
}
413-
}
414-
415398
public function _runPostWithParams($method="post",$type="parameter",$origine="routes"){
416399
if(RequestUtils::isPost()){
417400
$model=null;

0 commit comments

Comments
 (0)