Skip to content
This repository has been archived by the owner on Jul 27, 2022. It is now read-only.

Commit

Permalink
upgrade to 0.4.0-SNAPSHOT add cache file sys #43
Browse files Browse the repository at this point in the history
  • Loading branch information
besstiolle committed Mar 21, 2015
1 parent 95e12a1 commit baf02b4
Show file tree
Hide file tree
Showing 10 changed files with 196 additions and 47 deletions.
2 changes: 1 addition & 1 deletion src/Orm/Orm.module.php
Expand Up @@ -176,7 +176,7 @@ function GetFriendlyName() {
}

function GetVersion() {
return '0.3.3-SNAPSHOT';
return '0.4.0-SNAPSHOT';
}

function GetDependencies()
Expand Down
5 changes: 3 additions & 2 deletions src/Orm/action.admin_save.php
Expand Up @@ -12,15 +12,16 @@
$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
$cache = OrmCache::getInstance();
$cache->clearCache();

OrmTrace::info("Initiate the cache content");
} else if (isset($params['level']) ){
$this->SetPreference('loglevel', $params['level']);
}

$this->redirect($id,'defaultadmin');
Expand Down
2 changes: 1 addition & 1 deletion src/Orm/action.defaultadmin.php
Expand Up @@ -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);
Expand Down
15 changes: 12 additions & 3 deletions src/Orm/lib/class.OrmCache.php
Expand Up @@ -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;
* </code>
*
Expand All @@ -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
Expand All @@ -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');
Expand All @@ -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;
Expand Down
166 changes: 166 additions & 0 deletions src/Orm/lib/class.OrmCacheFile.php
@@ -0,0 +1,166 @@
<?php
/**
* Contains the class wich provide a persisted caching system of Orm to avoid multiples sql requests
*
* @since 0.4.0
* @author Bess
**/

/**
*
* Static classe used to provide a very simple persisted caching system. You can push the result of a request into it and asking later
* to collect the result.
*
* Example :
* <code>
* //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;
* </code>
*
* 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.'<br/>';
echo '# '.var_dump($params).'<br/>';
echo '# '.self::hash($sql,$params).'<br/>';*/
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.'<br/>';
echo '| '.var_dump($params).'<br/>';
echo '| '.self::hash($sql,$params).'<br/>';*/
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 "<h3>Orm can't write into the cache file : ".$filename."</h3>";
}
}

}

?>
2 changes: 1 addition & 1 deletion src/Orm/lib/class.OrmCacheScript.php
Expand Up @@ -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;
* </code>
*
Expand Down
6 changes: 3 additions & 3 deletions src/Orm/lib/class.OrmCore.php
Expand Up @@ -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);

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down
15 changes: 3 additions & 12 deletions src/OrmExtensions/OrmExtensions.module.php
@@ -1,14 +1,5 @@
<?php

/* Force the loading of Orm Framework BEFORE this module */
$config = cmsms()->GetConfig();
$Orm = $config['root_path'].'/modules/Orm/Orm.module.php';
if( !is_readable( $Orm ) ) {
echo '<h1><font color="red">ERROR: The Orm Framework could not be found [<a href="https://github.com/besstiolle/orm-ms/wiki">help</a>].</font></h1>';
return;
}
require_once($Orm);

class OrmExtensions extends Orm {

function GetName() {
Expand All @@ -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() {
Expand All @@ -49,7 +40,7 @@ function GetAdminDescription() {
}

function MinimumCMSVersion() {
return "1.11.0";
return "1.11.13";
}

function IsPluginModule() {
Expand Down
15 changes: 3 additions & 12 deletions src/OrmSkeleton/OrmSkeleton.module.php
@@ -1,14 +1,5 @@
<?php

/* Force the loading of Orm Framework BEFORE this module */
$config = cmsms()->GetConfig();
$Orm = $config['root_path'].'/modules/Orm/Orm.module.php';
if( !is_readable( $Orm ) ) {
echo '<h1><font color="red">ERROR: The Orm Framework could not be found [<a href="https://github.com/besstiolle/orm-ms/wiki">help</a>].</font></h1>';
return;
}
require_once($Orm);

class OrmSkeleton extends Orm
{
function GetName() {
Expand All @@ -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() {
Expand All @@ -48,7 +39,7 @@ function GetAdminDescription() {
}

function MinimumCMSVersion() {
return "1.11.0";
return "1.11.13";
}

function IsPluginModule() {
Expand Down
15 changes: 3 additions & 12 deletions src/OrmUT/OrmUT.module.php
@@ -1,14 +1,5 @@
<?php

/* Force the loading of Orm Framework BEFORE this module */
$config = cmsms()->GetConfig();
$Orm = $config['root_path'].'/modules/Orm/Orm.module.php';
if( !is_readable( $Orm ) ) {
echo '<h1><font color="red">ERROR: The Orm Framework could not be found [<a href="https://github.com/besstiolle/orm-ms/wiki">help</a>].</font></h1>';
return;
}
require_once($Orm);

class OrmUT extends Orm {

function GetName() {
Expand All @@ -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() {
Expand All @@ -48,7 +39,7 @@ function GetAdminDescription() {
}

function MinimumCMSVersion() {
return "1.11.0";
return "1.11.13";
}

function IsPluginModule() {
Expand Down

0 comments on commit baf02b4

Please sign in to comment.