[icinga-checkins] icinga.org: icinga-web/cdoebler/default: * prepared cache interface

git at icinga.org git at icinga.org
Wed Oct 7 10:46:13 CEST 2009


Module: icinga-web
Branch: cdoebler/default
Commit: b748152d611113ed285f8b97b45cff6c0002e41c
URL:    https://git.icinga.org/?p=icinga-web.git;a=commit;h=b748152d611113ed285f8b97b45cff6c0002e41c

Author: Marius Hein <marius.hein at netways.de>
Date:   Sat Oct  3 09:59:45 2009 +0200

* prepared cache interface
* simple file cache

---

 app/config/icinga.xml                      |    9 ++-
 lib/appkit/cache/AppKitCache.class.php     |  104 +++++++++++++++++++++++++---
 lib/appkit/cache/AppKitFileCache.class.php |   15 ++++
 3 files changed, 114 insertions(+), 14 deletions(-)

diff --git a/app/config/icinga.xml b/app/config/icinga.xml
index b8c58aa..f95a631 100644
--- a/app/config/icinga.xml
+++ b/app/config/icinga.xml
@@ -169,15 +169,16 @@
 
 	<setting name="appkit.factories">
 		<!-- Cache provider -->
-		<!-- <parameter name="Cache">
-			<parameter name="class">AppKitCache</parameter>
+		<!--
+		<parameter name="Cache">
+			<parameter name="class">AppKitFileCache</parameter>
 			
 			<parameter name="cacheDefaultRegion">appkit.cache.default</parameter>
 			<parameter name="cacheTransactionType">AppKitCache::COMMIT_POOL</parameter>
 			<parameter name="cacheSerializeType">AppKitCache::SERIALIZE_PHP</parameter>
-			<parameter name="cacheProviderName">AppKitCacheProviderFile</parameter>
 			
-		</parameter> -->
+		</parameter>
+		-->
 		
 		<!-- GLOBAL MESSAGE QUEUE -->
 		<parameter name="MessageQueue">
diff --git a/lib/appkit/cache/AppKitCache.class.php b/lib/appkit/cache/AppKitCache.class.php
index ab36e75..a80f705 100644
--- a/lib/appkit/cache/AppKitCache.class.php
+++ b/lib/appkit/cache/AppKitCache.class.php
@@ -1,6 +1,6 @@
 <?php 
 
-class AppKitCache extends AppKitBaseClass implements AppKitFactoryInterface {
+abstract class AppKitCache extends AppKitBaseClass implements AppKitFactoryInterface {
 	
 	const COMMIT_POOL		= 1;
 	const COMMIT_DISCRETE	= 2;
@@ -8,13 +8,13 @@ class AppKitCache extends AppKitBaseClass implements AppKitFactoryInterface {
 	const SERIALIZE_PHP		= 1;
 	const SERIALIZE_NONE	= 2;
 	
-	private $regions				= array();
-	private $provider				= null;
+	protected $regions				= array();
+	protected $data					= array();
+	protected $config				= array();
 	
-	private $cacheTransactionType	= false;
-	private $cacheSerializeType		= false;
-	private $cacheDefaultRegion		= null;
-	private $cacheProviderName		= null;
+	protected $cacheTransactionType	= false;
+	protected $cacheSerializeType	= false;
+	protected $cacheDefaultRegion	= null;
 	
 	public function __construct() {
 		
@@ -22,24 +22,108 @@ class AppKitCache extends AppKitBaseClass implements AppKitFactoryInterface {
 	
 	public function initializeFactory(array $parameters=array()) {
 		
+		if (array_key_exists('cacheDefaultRegion', $parameters)) {
+			$this->setDefaultRegion($parameters['cacheDefaultRegion']);
+		}
+		
+		if (array_key_exists('cacheTransactionType', $parameters)) {
+			$this->cacheTransactionType = AppKit::getConstant($parameters['cacheTransactionType']);
+		}
+		
+		if (array_key_exists('cacheSerializeType', $parameters)) {
+			$this->cacheSerializeType = AppKit::getConstant($parameters['cacheSerializeType']);
+		}
+		
+		$this->restoreData();
 		
 	}
 	
 	public function shutdownFactory() {
-		
+		if ($this->cacheTransactionType == self::COMMIT_POOL) {
+			$this->storeAll();
+		}
+	}
+	
+	public function setDefaultRegion($region_name) {
+		if (!$this->regionExists($region_name)) {
+			$this->addRegion($region);
+			$this->cacheDefaultRegion = $region_name;
+		}
 	}
 	
 	public function addRegion($region_name) {
 		if (!array_key_exists($region_name, $this->regions)) {
 			$this->regions[$region_name] = true;
+			
+			if (!is_array($this->data[$region_name])) {
+				$this->data[$region_name] = array();
+			}
+			
+			if (!is_array($this->config[$region_name])) {
+				$this->config[$region_name] = array();
+			}
+			
+			$this->config[$region_name]['added'] = time();
+			
+			$this->restoreData($region_name);
+			
 			return true;
 		}
 		
 		return false;
 	}
 	
-	public function setCacheProvider(AppKitCacheProvider &$provider) {
-		$this->provider =& $provider;
+	public function regionExists($region) {
+		return array_key_exists($region, $this->regions);
+	}
+	
+	public function setValue($name, $value, $region=null) {
+		if ($region===null) $region=$this->cacheDefaultRegion;
+		if (!$this->regionExists($region)) {
+			throw new AppKitCacheException('Region %s does not exist!', $region);
+		}
+		
+		$this->data[$region][$name] = $value;
+		
+		if ($this->cacheTransactionType == self::COMMIT_DISCRETE) {
+			$this->storeData();
+		}
+		
+		return true;
+	}
+	
+	public function getValue($name, $default=null,$region=null) {
+		if ($region===null) $region=$this->cacheDefaultRegion;
+		if (!$this->regionExists($region)) {
+			throw new AppKitCacheException('Region %s does not exist!', $region);
+		}
+		
+		if (array_key_exists($name, $this->data[$region])) {
+			return $this->data[$region][$name];
+		}
+		else {
+			return $default;
+		}
+	}
+	
+	public function loadAll() {
+		foreach ($this->regions as $name=>$garbage) {
+			$this->restoreData($name);
+		}
+	}
+	
+	public function storeAll() {
+		foreach ($this->regions as $name=>$garbage) {
+			$this->storeData($name);
+		}
+	}
+	
+	protected function storeData($region_name) {
+		$this->config[$region_name]['stored_at'] = time();
+	}
+	
+	protected function restoreData($region_name) {
+		$this->config[$region_name]['restored_at'] = time();
 	}
 	
 }
diff --git a/lib/appkit/cache/AppKitFileCache.class.php b/lib/appkit/cache/AppKitFileCache.class.php
new file mode 100644
index 0000000..d9ccedd
--- /dev/null
+++ b/lib/appkit/cache/AppKitFileCache.class.php
@@ -0,0 +1,15 @@
+<?php
+
+class AppKitFileCache extends AppKitCache {
+	
+	protected function storeData($region_name = null) {
+		
+	}	
+	
+	protected function restoreData($region_name = null) {
+		
+	}
+	
+}
+
+?>
\ No newline at end of file





More information about the icinga-checkins mailing list