diff --git a/src/Orm/Orm.module.php b/src/Orm/Orm.module.php index 94232b9..23425d8 100644 --- a/src/Orm/Orm.module.php +++ b/src/Orm/Orm.module.php @@ -176,7 +176,7 @@ function GetFriendlyName() { } function GetVersion() { - return '0.3.3-SNAPSHOT'; + return '0.4.0-SNAPSHOT'; } function GetDependencies() diff --git a/src/Orm/action.admin_save.php b/src/Orm/action.admin_save.php index 5c4b7d1..7522206 100644 --- a/src/Orm/action.admin_save.php +++ b/src/Orm/action.admin_save.php @@ -12,8 +12,7 @@ $cache = OrmCache::getInstance(); $cache->clearCache(); OrmTrace::info("Reinitiate the cache content"); -} else if (isset($params['level']) || isset($params['cache']) ){ - $this->SetPreference('loglevel', $params['level']); +} else if ( isset($params['cache']) ){ $this->SetPreference('cacheType', $params['cache']); //We reload the cache anyway @@ -21,6 +20,8 @@ $cache->clearCache(); OrmTrace::info("Initiate the cache content"); +} else if (isset($params['level']) ){ + $this->SetPreference('loglevel', $params['level']); } $this->redirect($id,'defaultadmin'); diff --git a/src/Orm/action.defaultadmin.php b/src/Orm/action.defaultadmin.php index 12f48d9..a31cdfb 100644 --- a/src/Orm/action.defaultadmin.php +++ b/src/Orm/action.defaultadmin.php @@ -4,7 +4,7 @@ //CONSTANTES $itemsLog = array("DEBUG"=>OrmTrace::$DEBUG, "INFO & SQL"=>OrmTrace::$SQL, "INFO"=>OrmTrace::$INFO,"WARN"=>OrmTrace::$WARN,"ERROR"=>OrmTrace::$ERROR); -$itemsCache = array("NONE"=>OrmCache::$NONE, "SCRIPT"=>OrmCache::$SCRIPT); +$itemsCache = array("NONE"=>OrmCache::$NONE, "SCRIPT"=>OrmCache::$SCRIPT, "FILE"=>OrmCache::$FILE); $currentLevelLog = $this->GetPreference('loglevel', OrmTrace::$INFO); $currentTypeCache = $this->GetPreference('cacheType', OrmCache::$NONE); diff --git a/src/Orm/lib/class.OrmCache.php b/src/Orm/lib/class.OrmCache.php index d77e5ba..19c1cf7 100644 --- a/src/Orm/lib/class.OrmCache.php +++ b/src/Orm/lib/class.OrmCache.php @@ -39,7 +39,7 @@ * * //Don't forget to push the result into the caching system for the next call * $cacheInstance->setCache($querySelect, null, $entitys); - + * * return $entitys; * * @@ -58,6 +58,11 @@ abstract class OrmCache { * Will use a cache during the time of the execution of the script **/ public static $SCRIPT = 1; + + /** + * Will use a cache during the time of the execution of the script + **/ + public static $FILE = 2; /** * Private constructor @@ -68,10 +73,11 @@ protected function __construct() {} * Will return a implementation of the cache. * * @param mixed $typeCache not required : the type of cache. By default it will take the type of cache defined as default into CmsMadeSimple - * + * @param mixed[] $parameters : optional parameters for cache classes + * * @return an instance of Cache. **/ - public static function getInstance($typeCache = null){ + public static function getInstance($typeCache = null, $parameters = null){ if($typeCache == null){ $orm = cmsms()->GetModuleOperations()->get_module_instance('Orm'); @@ -85,6 +91,9 @@ public static function getInstance($typeCache = null){ case OrmCache::$SCRIPT: return OrmCacheScript::getMyOwnInstance(); break; + case OrmCache::$FILE: + return OrmCacheFile::getMyOwnInstance($parameters); + break; default: OrmTrace::error("Type of Cache #{$typeCache} is not a valid Type of Cache"); exit -1; diff --git a/src/Orm/lib/class.OrmCacheFile.php b/src/Orm/lib/class.OrmCacheFile.php new file mode 100644 index 0000000..f684577 --- /dev/null +++ b/src/Orm/lib/class.OrmCacheFile.php @@ -0,0 +1,166 @@ + + * //Call to cmsms to get the database connector + * $db = cmsms()->GetDb(); + * + * //Defines a new Customer entity + * $entity = MyAutoload::getInstance('myModule', 'customer'); + * + * //Select all Customers + * $querySelect = 'Select * FROM '.$entity->getDbname(); + * + * //If the caching system already know the answer : we return the result immediately + * if(OrmCache::getInstance()->isCache($querySelect)) { + * return OrmCache::getInstance()->getCache($querySelect); + * } + * + * //So we need to execute the query + * $result = $db->Execute($querySelect); + * if ($result === false){die("Database error!");} + * + * $entitys = array(); + * while ($row = $result->FetchRow()) { + * $entitys[] = OrmCore::rowToEntity($entity, $row); + * } + * + * //Don't forget to push the result into the caching system for the next call + * OrmCache::getInstance()->setCache($querySelect, null, $entitys); + * + * return $entitys; + * + * + * The cache is available for an amount of time + * And will be clean the time after. + * + * + * @since 0.4.0 + * @author Bess + * @package Orm + **/ +class OrmCacheFile extends OrmCache { + + /** + * @var OrmCacheFile $instance The current instance + **/ + private static $instance; + + /** + * @var mixed[] $cache an array containing all the data cached + **/ + private static $cache; + + + /** + * @var string $filename the file in tmp directory wich will contains cache data + **/ + private static $filename; + + /** + * Private constructor + * + * @param mixed[] $parameters the parameter to the constructor + */ + protected function __construct($parameters) { + self::$cache = array(); + $config = cmsms()->GetConfig(); + self::$filename = $config['root_path'].'/tmp/cache/cache_orm'; + if(file_exists(self::$filename) && is_readable(self::$filename)){ + $content = file_get_contents(self::$filename); + self::$cache = unserialize($content); + } else { + self::saveCache(); + } + } + + /** + * Will return an instance of the cache class + * + * @param mixed[] $parameters the parameter to the constructor + * + * @return OrmCacheFile the cache class + */ + public static function getMyOwnInstance($parameters){ + if(self::$instance == null){ + self::$instance = new OrmCacheFile($parameters); + } + return self::$instance; + } + + /** + * Set the cache for a sql request, its parameters and of course the result + * + * @param string $sql the sql query + * @param mixed[] $params array the parameters into a array. May be null + * @param mixed $value the result + */ + public function setCache($sql, $params = null, $value) { + /*echo '# '.$sql.'
'; + echo '# '.var_dump($params).'
'; + echo '# '.self::hash($sql,$params).'
';*/ + self::$cache[self::hash($sql,$params)] = $value; + self::saveCache(); + + } + + /** + * Querying the cache for a sql request and its parameters + * + * @param string $sql the sql query + * @param mixed[] $params array the parameters into a array. May be null + * + * @return mixed the result + */ + public function getCache($sql, $params = null) { + if(self::isCache($sql, $params)) { + return self::$cache[self::hash($sql,$params)]; + } + + return null; + } + + /** + * Return true if a cache exist for a sql request and its parameters + * + * @param string $sql the sql query + * @param mixed[] $params array the parameters into a array. May be null + * + * @return boolean true if the cache exists + */ + public function isCache($sql, $params = null) { + /*echo '| '.$sql.'
'; + echo '| '.var_dump($params).'
'; + echo '| '.self::hash($sql,$params).'
';*/ + return array_KEY_exists(self::hash($sql,$params),self::$cache); + } + + /** + * Empty the cache. Very important if between 2 querying, the system may insert/delete/update some data in the database + * In the Orm system, we always drop the cache in the insert/delete/update function. + */ + public function clearCache() { + self::$cache = array(); + self::saveCache(); + } + + private function saveCache(){ + if(FALSE === file_put_contents(self::$filename ,serialize(self::$cache) )){ + echo "

Orm can't write into the cache file : ".$filename."

"; + } + } + +} + +?> diff --git a/src/Orm/lib/class.OrmCacheScript.php b/src/Orm/lib/class.OrmCacheScript.php index 4f80b26..9486d31 100644 --- a/src/Orm/lib/class.OrmCacheScript.php +++ b/src/Orm/lib/class.OrmCacheScript.php @@ -38,7 +38,7 @@ * * //Don't forget to push the result into the caching system for the next call * OrmCache::getInstance()->setCache($querySelect, null, $entitys); - + * * return $entitys; * * diff --git a/src/Orm/lib/class.OrmCore.php b/src/Orm/lib/class.OrmCore.php index 683580d..eebcb02 100644 --- a/src/Orm/lib/class.OrmCore.php +++ b/src/Orm/lib/class.OrmCore.php @@ -1201,7 +1201,7 @@ public static final function findByExample(OrmEntity $entityParam, OrmExample $e } //We push the result into the cache before return it - OrmCache::getInstance()->setCache($queryExample, null, $entities); + OrmCache::getInstance()->setCache($queryExample, $params, $entities); } return array_values($entities); @@ -1273,7 +1273,7 @@ public static final function selectCountByExample(OrmEntity $entityParam, OrmExa } //We push the result into the cache before return it - OrmCache::getInstance()->setCache($queryExample, null, $counter); + OrmCache::getInstance()->setCache($queryExample, $params, $counter); } return $counter; @@ -1522,7 +1522,7 @@ private static final function _selectSomethingByExample(OrmEntity $entityParam, } //We push the result into the cache before return it - OrmCache::getInstance()->setCache($queryExample, null, $counter); + OrmCache::getInstance()->setCache($queryExample, $params, $counter); } return $counter; diff --git a/src/OrmExtensions/OrmExtensions.module.php b/src/OrmExtensions/OrmExtensions.module.php index c33eb8e..32095a4 100644 --- a/src/OrmExtensions/OrmExtensions.module.php +++ b/src/OrmExtensions/OrmExtensions.module.php @@ -1,14 +1,5 @@ GetConfig(); -$Orm = $config['root_path'].'/modules/Orm/Orm.module.php'; -if( !is_readable( $Orm ) ) { - echo '

ERROR: The Orm Framework could not be found [help].

'; - return; -} -require_once($Orm); - class OrmExtensions extends Orm { function GetName() { @@ -20,12 +11,12 @@ function GetFriendlyName() { } function GetVersion() { - return '0.3.3-SNAPSHOT'; + return '0.4.0-SNAPSHOT'; } function GetDependencies() { - return array('Orm'=>'0.3.3-SNAPSHOT'); + return array('Orm'=>'0.4.0-SNAPSHOT'); } function GetHelp() { @@ -49,7 +40,7 @@ function GetAdminDescription() { } function MinimumCMSVersion() { - return "1.11.0"; + return "1.11.13"; } function IsPluginModule() { diff --git a/src/OrmSkeleton/OrmSkeleton.module.php b/src/OrmSkeleton/OrmSkeleton.module.php index b7a375e..effa78d 100644 --- a/src/OrmSkeleton/OrmSkeleton.module.php +++ b/src/OrmSkeleton/OrmSkeleton.module.php @@ -1,14 +1,5 @@ GetConfig(); -$Orm = $config['root_path'].'/modules/Orm/Orm.module.php'; -if( !is_readable( $Orm ) ) { - echo '

ERROR: The Orm Framework could not be found [help].

'; - return; -} -require_once($Orm); - class OrmSkeleton extends Orm { function GetName() { @@ -20,11 +11,11 @@ function GetFriendlyName() { } function GetVersion() { - return '0.3.3-SNAPSHOT'; + return '0.4.0-SNAPSHOT'; } function GetDependencies() { - return array('Orm'=>'0.3.3-SNAPSHOT'); + return array('Orm'=>'0.4.0-SNAPSHOT'); } function GetHelp() { @@ -48,7 +39,7 @@ function GetAdminDescription() { } function MinimumCMSVersion() { - return "1.11.0"; + return "1.11.13"; } function IsPluginModule() { diff --git a/src/OrmUT/OrmUT.module.php b/src/OrmUT/OrmUT.module.php index ccbbdf3..dc841da 100644 --- a/src/OrmUT/OrmUT.module.php +++ b/src/OrmUT/OrmUT.module.php @@ -1,14 +1,5 @@ GetConfig(); -$Orm = $config['root_path'].'/modules/Orm/Orm.module.php'; -if( !is_readable( $Orm ) ) { - echo '

ERROR: The Orm Framework could not be found [help].

'; - return; -} -require_once($Orm); - class OrmUT extends Orm { function GetName() { @@ -20,11 +11,11 @@ function GetFriendlyName() { } function GetVersion() { - return '0.3.3-SNAPSHOT'; + return '0.4.0-SNAPSHOT'; } function GetDependencies() { - return array('Orm'=>'0.3.3-SNAPSHOT'); + return array('Orm'=>'0.4.0-SNAPSHOT'); } function GetHelp() { @@ -48,7 +39,7 @@ function GetAdminDescription() { } function MinimumCMSVersion() { - return "1.11.0"; + return "1.11.13"; } function IsPluginModule() {