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 "