Skip to content

Commit c428b0a

Browse files
committed
Cache sanitation
1 parent 7c92dc1 commit c428b0a

File tree

9 files changed

+305
-264
lines changed

9 files changed

+305
-264
lines changed

micro/cache/CacheManager.php

Lines changed: 6 additions & 252 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,24 @@
55
use mindplay\annotations\Annotations;
66
use mindplay\annotations\AnnotationCache;
77
use mindplay\annotations\AnnotationManager;
8-
use micro\orm\parser\ModelParser;
9-
use micro\utils\JArray;
108
use micro\controllers\Router;
11-
use micro\controllers\Startup;
129
use micro\utils\FsUtils;
13-
use micro\cache\parser\ControllerParser;
14-
use micro\cache\parser\RestControllerParser;
15-
use micro\exceptions\RestException;
16-
use micro\exceptions\RouterException;
10+
use micro\cache\traits\RouterCacheTrait;
11+
use micro\cache\traits\ModelsCacheTrait;
12+
use micro\cache\traits\RestCacheTrait;
1713

1814
class CacheManager {
15+
use RouterCacheTrait,ModelsCacheTrait,RestCacheTrait;
16+
1917
public static $cache;
2018
private static $cacheDirectory;
21-
private static $expiredRoutes=[ ];
2219

2320
public static function start(&$config) {
2421
self::$cacheDirectory=self::initialGetCacheDirectory($config);
2522
$cacheDirectory=ROOT . DS . self::$cacheDirectory;
2623
Annotations::$config['cache']=new AnnotationCache($cacheDirectory . '/annotations');
2724
self::register(Annotations::getManager());
2825
self::getCacheInstance($config, $cacheDirectory, ".cache");
29-
//self::$cache=new ArrayCache($cacheDirectory, ".cache");
3026
}
3127

3228
public static function startProd(&$config) {
@@ -36,7 +32,7 @@ public static function startProd(&$config) {
3632
}
3733

3834
protected static function getCacheInstance(&$config,$cacheDirectory,$postfix){
39-
$cacheSystem='micro\cache\ArrayCache';
35+
$cacheSystem='micro\cache\system\ArrayCache';
4036
if(!isset(self::$cache)){
4137
if(isset($config["cache"]["system"])){
4238
$cacheSystem=$config["cache"]["system"];
@@ -46,73 +42,6 @@ protected static function getCacheInstance(&$config,$cacheDirectory,$postfix){
4642
return self::$cache;
4743
}
4844

49-
public static function getControllerCache($isRest=false) {
50-
$key=($isRest)?"rest":"default";
51-
if (self::$cache->exists("controllers/routes.".$key))
52-
return self::$cache->fetch("controllers/routes.".$key);
53-
return [];
54-
//throw new RouterException( $key." cache does not exist : the file `".FsUtils::cleanPathname(ROOT.DS.self::getCacheDirectory()."controllers/routes.").$key.".cache.php` is missing.\nTry to Re-init cache.");
55-
}
56-
57-
public static function getRestCache() {
58-
if (self::$cache->exists("controllers/rest"))
59-
return self::$cache->fetch("controllers/rest");
60-
throw new RestException("Rest cache does not exist : the file `".FsUtils::cleanPathname(ROOT.DS.self::getCacheDirectory()."controllers/")."rest.cache.php` is missing.\nTry to Re-init Rest cache.");
61-
}
62-
63-
public static function getRouteCache($routePath, $duration) {
64-
$key=self::getRouteKey($routePath);
65-
66-
if (self::$cache->exists("controllers/" . $key) && !self::expired($key, $duration)) {
67-
$response=self::$cache->file_get_contents("controllers/" . $key);
68-
return $response;
69-
} else {
70-
$response=Startup::runAsString($routePath);
71-
return self::storeRouteResponse($key, $response);
72-
}
73-
}
74-
75-
public static function expired($key, $duration) {
76-
return self::$cache->expired("controllers/" . $key, $duration) === true || \array_key_exists($key, self::$expiredRoutes);
77-
}
78-
79-
public static function isExpired($path,$duration){
80-
$route=Router::getRoute($path,false);
81-
if($route!==false && \is_array($route)){
82-
return self::expired(self::getRouteKey($route), $duration);
83-
}
84-
return true;
85-
}
86-
87-
public static function setExpired($routePath, $expired=true) {
88-
$key=self::getRouteKey($routePath);
89-
self::setKeyExpired($key, $expired);
90-
}
91-
92-
private static function setKeyExpired($key, $expired=true) {
93-
if ($expired) {
94-
self::$expiredRoutes[$key]=true;
95-
} else {
96-
unset(self::$expiredRoutes[$key]);
97-
}
98-
}
99-
100-
public static function setRouteCache($routePath) {
101-
$key=self::getRouteKey($routePath);
102-
$response=Startup::runAsString($routePath);
103-
return self::storeRouteResponse($key, $response);
104-
}
105-
106-
private static function storeRouteResponse($key, $response) {
107-
self::setKeyExpired($key, false);
108-
self::$cache->store("controllers/" . $key, $response, false);
109-
return $response;
110-
}
111-
112-
private static function getRouteKey($routePath) {
113-
return "path" . \md5(\implode("", $routePath));
114-
}
115-
11645
private static function initialGetCacheDirectory(&$config) {
11746
$cacheDirectory=@$config["cache"]["directory"];
11847
if (!isset($cacheDirectory)) {
@@ -126,44 +55,6 @@ public static function getCacheDirectory() {
12655
return self::$cacheDirectory;
12756
}
12857

129-
public static function createOrmModelCache($classname) {
130-
$key=self::getModelCacheKey($classname);
131-
if(isset(self::$cache)){
132-
if (!self::$cache->exists($key)) {
133-
$p=new ModelParser();
134-
$p->parse($classname);
135-
self::$cache->store($key, $p->__toString());
136-
}
137-
return self::$cache->fetch($key);
138-
}
139-
}
140-
141-
public static function getOrmModelCache($classname) {
142-
return self::$cache->fetch(self::getModelCacheKey($classname));
143-
}
144-
145-
public static function getModelCacheKey($classname){
146-
return \str_replace("\\", DIRECTORY_SEPARATOR, $classname);
147-
}
148-
149-
public static function modelCacheExists($classname){
150-
$key=self::getModelCacheKey($classname);
151-
if(isset(self::$cache))
152-
return self::$cache->exists($key);
153-
return false;
154-
}
155-
156-
private static function addControllerCache($classname) {
157-
$parser=new ControllerParser();
158-
try {
159-
$parser->parse($classname);
160-
return $parser->asArray();
161-
} catch ( \Exception $e ) {
162-
// Nothing to do
163-
}
164-
return [];
165-
}
166-
16758
public static function checkCache(&$config,$silent=false) {
16859
$dirs=self::getCacheDirectories($config,$silent);
16960
foreach ($dirs as $dir){
@@ -216,38 +107,6 @@ public static function initCache(&$config, $type="all",$silent=false) {
216107
self::initRestCache($config,$silent);
217108
}
218109

219-
public static function initModelsCache(&$config,$forChecking=false,$silent=false) {
220-
$files=self::getModelsFiles($config,$silent);
221-
foreach ( $files as $file ) {
222-
if (is_file($file)) {
223-
$model=ClassUtils::getClassFullNameFromFile($file);
224-
if(!$forChecking){
225-
self::createOrmModelCache($model);
226-
}
227-
}
228-
}
229-
if(!$silent){
230-
echo "Models cache reset\n";
231-
}
232-
}
233-
234-
public static function getModelsFiles(&$config,$silent=false){
235-
return self::_getFiles($config, "models",$silent);
236-
}
237-
238-
public static function getModels(&$config,$silent=false){
239-
$result=[];
240-
$files=self::getModelsFiles($config,$silent);
241-
foreach ($files as $file){
242-
$result[]=ClassUtils::getClassFullNameFromFile($file);
243-
}
244-
return $result;
245-
}
246-
247-
public static function getControllersFiles(&$config,$silent=false){
248-
return self::_getFiles($config, "controllers",$silent);
249-
}
250-
251110
private static function _getFiles(&$config,$type,$silent=false){
252111
$typeNS=$config["mvcNS"][$type];
253112
$typeDir=ROOT . DS . str_replace("\\", DS, $typeNS);
@@ -256,49 +115,6 @@ private static function _getFiles(&$config,$type,$silent=false){
256115
return FsUtils::glob_recursive($typeDir . DS . '*');
257116
}
258117

259-
private static function initRouterCache(&$config,$silent=false) {
260-
$routes=["rest"=>[],"default"=>[]];
261-
$files=self::getControllersFiles($config);
262-
foreach ( $files as $file ) {
263-
if (is_file($file)) {
264-
$controller=ClassUtils::getClassFullNameFromFile($file);
265-
$parser=new ControllerParser();
266-
try {
267-
$parser->parse($controller);
268-
$ret= $parser->asArray();
269-
$key=($parser->isRest())?"rest":"default";
270-
$routes[$key]=\array_merge($routes[$key], $ret);
271-
} catch ( \Exception $e ) {
272-
// Nothing to do
273-
}
274-
275-
}
276-
}
277-
self::$cache->store("controllers/routes.default", "return " . JArray::asPhpArray($routes["default"], "array") . ";");
278-
self::$cache->store("controllers/routes.rest", "return " . JArray::asPhpArray($routes["rest"], "array") . ";");
279-
if(!$silent){
280-
echo "Router cache reset\n";
281-
}
282-
}
283-
284-
private static function initRestCache(&$config,$silent=false) {
285-
$restCache=[];
286-
$files=self::getControllersFiles($config);
287-
foreach ( $files as $file ) {
288-
if (is_file($file)) {
289-
$controller=ClassUtils::getClassFullNameFromFile($file);
290-
$parser=new RestControllerParser();
291-
$parser->parse($controller,$config);
292-
if($parser->isRest())
293-
$restCache=\array_merge($restCache,$parser->asArray());
294-
}
295-
}
296-
self::$cache->store("controllers/rest", "return " . JArray::asPhpArray($restCache, "array") . ";");
297-
if(!$silent){
298-
echo "Rest cache reset\n";
299-
}
300-
}
301-
302118
private static function register(AnnotationManager $annotationManager) {
303119
$annotationManager->registry=array_merge($annotationManager->registry, [
304120
'id' => 'micro\annotations\IdAnnotation',
@@ -317,66 +133,4 @@ private static function register(AnnotationManager $annotationManager) {
317133
'authorization' => 'micro\annotations\rest\AuthorizationAnnotation'
318134
]);
319135
}
320-
321-
public static function addAdminRoutes() {
322-
self::addControllerCache("micro\controllers\Admin");
323-
}
324-
325-
public static function getRoutes() {
326-
$result=self::getControllerCache();
327-
return $result;
328-
}
329-
330-
public static function getRestRoutes() {
331-
$result=[];
332-
$restCache=self::getRestCache();
333-
foreach ($restCache as $controllerClass=>$restAttributes){
334-
if(isset($restCache[$controllerClass])){
335-
$result[$controllerClass]=["restAttributes"=>$restAttributes,"routes"=>self::getControllerRoutes($controllerClass,true)];
336-
}
337-
}
338-
return $result;
339-
}
340-
341-
public static function getControllerRoutes($controllerClass,$isRest=false){
342-
$result=[];
343-
$ctrlCache=self::getControllerCache($isRest);
344-
foreach ($ctrlCache as $path=>$routeAttributes){
345-
if(isset($routeAttributes["controller"])){
346-
if($routeAttributes["controller"]===$controllerClass){
347-
$result[$path]=$routeAttributes;
348-
}
349-
}else{
350-
$firstValue=reset($routeAttributes);
351-
if(isset($firstValue)&&isset($firstValue["controller"])){
352-
if($firstValue["controller"]===$controllerClass){
353-
$result[$path]=$routeAttributes;
354-
}
355-
}
356-
}
357-
}
358-
return $result;
359-
}
360-
361-
public static function getRestResource($controllerClass){
362-
$cacheControllerClass=self::getRestCacheController($controllerClass);
363-
if(isset($cacheControllerClass))
364-
return $cacheControllerClass["resource"];
365-
return null;
366-
}
367-
368-
public static function getRestCacheController($controllerClass){
369-
$cache=self::getRestCache();
370-
if(isset($cache[$controllerClass])){
371-
return $cache[$controllerClass];
372-
}
373-
return null;
374-
}
375-
376-
377-
public static function addRoute($path, $controller, $action="index", $methods=null, $name="") {
378-
$controllerCache=self::getControllerCache();
379-
Router::addRouteToRoutes($controllerCache, $path, $controller, $action, $methods, $name);
380-
self::$cache->store("controllers/routes", "return " . JArray::asPhpArray($controllerCache, "array") . ";");
381-
}
382136
}

micro/cache/database/DbCache.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace micro\cache\database;
44

5-
use micro\cache\ArrayCache;
5+
use micro\cache\system\ArrayCache;
66
use micro\cache\CacheManager;
77

88
abstract class DbCache {

micro/cache/AbstractDataCache.php renamed to micro/cache/system/AbstractDataCache.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Inspired by (c) Rasmus Schultz <[email protected]>
44
* <https://github.com/mindplay-dk/php-annotations>
55
*/
6-
namespace micro\cache;
6+
namespace micro\cache\system;
77

88
/**
99
* This class is responsible for storing Arrays in PHP files.
@@ -27,7 +27,7 @@ public function __construct($root, $postfix=""){
2727
/**
2828
* Check if annotation-data for the key has been stored.
2929
* @param string $key cache key
30-
* @return bool true if data with the given key has been stored; otherwise false
30+
* @return boolean true if data with the given key has been stored; otherwise false
3131
*/
3232
abstract public function exists($key);
3333

micro/cache/ApcuCache.php renamed to micro/cache/system/ApcuCache.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
namespace micro\cache;
2+
namespace micro\cache\system;
33

44
/**
55
* This class is responsible for storing Arrays in PHP files.
@@ -15,7 +15,7 @@ public function __construct($root,$postfix="") {
1515
/**
1616
* Check if annotation-data for the key has been stored.
1717
* @param string $key cache key
18-
* @return bool true if data with the given key has been stored; otherwise false
18+
* @return boolean true if data with the given key has been stored; otherwise false
1919
*/
2020
public function exists($key) {
2121
return \apcu_exists($this->getRealKey($key));
@@ -28,8 +28,7 @@ public function store($key, $code, $php=true) {
2828
/**
2929
* Caches the given data with the given key.
3030
* @param string $key cache key
31-
* @param string $code the source-code to be cached
32-
* @throws AnnotationException if file could not be written
31+
* @param string $content the source-code to be cached
3332
*/
3433
protected function storeContent($key,$content) {
3534
\apcu_store($this->getRealKey($key), $content);
@@ -77,7 +76,7 @@ public function getTimestamp($key) {
7776
$creationTime = $entry['creation_time'];
7877
return $creationTime;
7978
}
80-
return false;
79+
return \time();
8180
}
8281

8382
public function remove($key) {

0 commit comments

Comments
 (0)