[icinga-checkins] icinga.org: icinga-web/jmosshammer/filtering: Added user local caching of xml cronks
git at icinga.org
git at icinga.org
Wed Jan 2 15:43:54 CET 2013
Module: icinga-web
Branch: jmosshammer/filtering
Commit: 30dc8d4c8de2c08d6b8cb62f64a17af94908cd2e
URL: https://git.icinga.org/?p=icinga-web.git;a=commit;h=30dc8d4c8de2c08d6b8cb62f64a17af94908cd2e
Author: Jannis Mosshammer <jannis.mosshammer at netways.de>
Date: Mon Dec 17 12:55:26 2012 +0100
Added user local caching of xml cronks
---
.../storage/AppKitMemcacheSessionStorage.class.php | 116 ++++++++++++++++++++
.../models/Provider/CronksDataModel.class.php | 14 ++-
2 files changed, 124 insertions(+), 6 deletions(-)
diff --git a/app/modules/AppKit/lib/storage/AppKitMemcacheSessionStorage.class.php b/app/modules/AppKit/lib/storage/AppKitMemcacheSessionStorage.class.php
new file mode 100755
index 0000000..01f1bd9
--- /dev/null
+++ b/app/modules/AppKit/lib/storage/AppKitMemcacheSessionStorage.class.php
@@ -0,0 +1,116 @@
+<?php
+// {{{ICINGA_LICENSE_CODE}}}
+// -----------------------------------------------------------------------------
+// This file is part of icinga-web.
+//
+// Copyright (c) 2009-2012 Icinga Developer Team.
+// All rights reserved.
+//
+// icinga-web is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// icinga-web is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with icinga-web. If not, see <http://www.gnu.org/licenses/>.
+// -----------------------------------------------------------------------------
+// {{{ICINGA_LICENSE_CODE}}}
+
+class AppKitMemcacheSessionStorage extends AgaviSessionStorage {
+
+ /**
+ * @var NsmSession
+ */
+ private $NsmSession = null;
+ private $host = "localhost";
+ private $port = 11211;
+
+ /**
+ * @var Memcache
+ */
+ private $memcache = null;
+
+ public function initialize(AgaviContext $context, array $parameters = array()) {
+
+ // initialize the parent
+ parent::initialize($context, $parameters);
+ $this->host = isset($parameters["host"]) ? $parameters["host"] : 'localhost';
+ $this->port = isset($parameters["port"]) ? $parameters["port"] : 11211;
+ session_set_save_handler(
+ array(&$this, 'sessionOpen'),
+ array(&$this, 'sessionClose'),
+ array(&$this, 'sessionRead'),
+ array(&$this, 'sessionWrite'),
+ array(&$this, 'sessionDestroy'),
+ array(&$this, 'sessionGC')
+ );
+
+ }
+
+ public function sessionClose() {
+ // Hm, the same as sessionOpen?!
+
+ }
+
+ /**
+ * Trigger the sesstion destroy and remove
+ * data from database
+ * @param string $id
+ */
+ public function sessionDestroy($id) {
+ memcache_delete($this->memcache,$this->prefix.$this->getParameter('session_name').":".$id);
+ return true;
+ }
+
+ /**
+ Memcache has a expire value
+ * @param integer $lifetime
+ */
+ public function sessionGC($lifetime) {
+ /**/
+ return true;
+ }
+
+ /**
+ * Trigger to open the session
+ * @param string $path
+ * @param string $name
+ */
+ public function sessionOpen($path, $name) {
+ $this->prefix = $path.$name;
+ $this->memcache = memcache_pconnect($this->host,$this->port);
+ }
+
+ /**
+ * Reads data from doctrine tables and return its content
+ * @param string $id
+ * @throws AppKitDoctrineSessionStorageException
+ */
+ public function sessionRead($id) {
+ $session = memcache_get($this->memcache,$this->prefix.$this->getParameter('session_name').":".$id);
+ if(!$session) {
+ memcache_add($this->memcache,$this->prefix.$this->getParameter('session_name').":".$id,"");
+ return '';
+ }
+
+ return $session;
+
+ }
+
+ /**
+ * Writes session data to database tables
+ * @param string $id
+ * @param mixed $data
+ */
+ public function sessionWrite($id, &$data) {
+ memcache_set($this->memcache,$this->prefix.$this->getParameter('session_name').":".$id,$data);
+ }
+
+}
+
+class AppKitDoctrineSessionStorageException extends AppKitException {}
diff --git a/app/modules/Cronks/models/Provider/CronksDataModel.class.php b/app/modules/Cronks/models/Provider/CronksDataModel.class.php
index 4795fc2..847bbf3 100644
--- a/app/modules/Cronks/models/Provider/CronksDataModel.class.php
+++ b/app/modules/Cronks/models/Provider/CronksDataModel.class.php
@@ -100,9 +100,9 @@ class Cronks_Provider_CronksDataModel extends CronksBaseModel implements AgaviIS
}
$this->initializeXmlData();
-
+
$this->cronks = $this->getCronks(true);
-
+
}
/**
@@ -225,8 +225,10 @@ class Cronks_Provider_CronksDataModel extends CronksBaseModel implements AgaviIS
* @return array
*/
private function getXmlCronks($all=false) {
+ $cached = $this->user->getStorage()->read("icinga.cronks.cache.xml");
+ if($cached)
+ return $cached;
$out = array();
-
foreach(self::$xml_cronk_data as $uid=>$cronk) {
/*
@@ -276,9 +278,10 @@ class Cronks_Provider_CronksDataModel extends CronksBaseModel implements AgaviIS
'position' => isset($cronk['position']) ? $cronk['position'] : 0,
'owner_name' => self::DEFAULT_CRONK_OWNER,
'owner_id' => self::DEFAULT_CRONK_OWNERID
- );
+ );
+
}
-
+ $this->user->getStorage()->write("icinga.cronks.cache.xml",$out);
return $out;
}
@@ -383,7 +386,6 @@ class Cronks_Provider_CronksDataModel extends CronksBaseModel implements AgaviIS
$cronks = (array)$this->getDbCronks() + $cronks;
$this->reorderCronks($cronks);
-
return $cronks;
}
More information about the icinga-checkins
mailing list