From 285bc96184eb27cfa5dd2d62113461876b504c52 Mon Sep 17 00:00:00 2001
From: Olav Morken <olav.morken@uninett.no>
Date: Wed, 28 Nov 2007 14:12:26 +0000
Subject: [PATCH] Added option to set the expiry time of data in the memcache
 servers.

git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@83 44740490-163a-0410-bde0-09ae8108e29a
---
 config/config-template.php       | 17 +++++++++
 lib/SimpleSAML/MemcacheStore.php | 64 +++++++++++++++++++++++++++++++-
 2 files changed, 80 insertions(+), 1 deletion(-)

diff --git a/config/config-template.php b/config/config-template.php
index 46785d76f..27ae28fd4 100644
--- a/config/config-template.php
+++ b/config/config-template.php
@@ -153,6 +153,23 @@ $config = array (
 	),
 
 
+	/*
+	 * This value is the duration data should be stored in memcache. Data
+	 * will be dropped from the memcache servers when this time expires.
+	 * The time will be reset every time the data is written to the
+	 * memcache servers.
+	 *
+	 * This value should always be larger than the 'session.duration'
+	 * option. Not doing this may result in the session being deleted from
+	 * the memcache servers while it is still in use.
+	 *
+	 * Set this value to 0 if you don't want data to expire.
+	 *
+	 * Note: The oldest data will always be deleted if the memcache server
+	 * runs out of storage space.
+	 */
+	'memcache_store.expires' =>  36 * (60*60), // 36 hours.
+
 );
 
 
diff --git a/lib/SimpleSAML/MemcacheStore.php b/lib/SimpleSAML/MemcacheStore.php
index b9fde9273..ce3b51dbf 100644
--- a/lib/SimpleSAML/MemcacheStore.php
+++ b/lib/SimpleSAML/MemcacheStore.php
@@ -281,7 +281,8 @@ class SimpleSAML_MemcacheStore {
 
 		/* Store this object to all groups of memcache servers. */
 		foreach(self::getMemcacheServers() as $server) {
-			$server->set($this->id, $this->savedData);
+			$server->set($this->id, $this->savedData, 0,
+			             self::getExpireTime());
 		}
 	}
 
@@ -565,5 +566,66 @@ class SimpleSAML_MemcacheStore {
 
 		return TRUE;
 	}
+
+
+	/* This is a helper-function which returns the expire value of data
+	 * we should store to the memcache servers.
+	 *
+	 * The value is set depending on the configuration. If no value is
+	 * set in the configuration, then we will use a default value of 0.
+	 * 0 means that the item will never expire.
+	 *
+	 * Returns:
+	 *  The value which should be passed in the set(...) calls to the
+	 *  memcache objects.
+	 */
+	private static function getExpireTime()
+	{
+		/* Get the configuration instance. */
+		$config = SimpleSAML_Configuration::getInstance();
+		assert($config instanceof SimpleSAML_Configuration);
+
+		/* Get the expire-value from the configuration. */
+		$expire = $config->getValue('memcache_store.expires');
+
+		/* If 'memcache_store.expires' isn't defined in the
+		 * configuration, then we will use 0 as the expire parameter.
+		 */
+		if($expire === NULL) {
+			return 0;
+		}
+
+		/* The 'memcache_store.expires' option must be an integer. */
+		if(!is_integer($expire)) {
+			$e = 'The value of \'memcache_store.expires\' in the' .
+			     ' configuration must be a valid integer.';
+			error_log($e);
+			die($e);
+		}
+
+		/* It must be a positive integer. */
+		if($expire < 0) {
+			$e = 'The value of \'memcache_store.expires\' in the' .
+			     ' configuration can\'t be a negative integer.';
+			error_log($e);
+			die($e);
+		}
+
+		/* If the configuration option is 0, then we should
+		 * return 0. This allows the user to specify that the data
+		 * shouldn't expire.
+		 */
+		if($expire == 0) {
+			return 0;
+		}
+
+		/* The expire option is given as the number of seconds into the
+		 * future an item should expire. We convert this to an actual
+		 * timestamp.
+		 */
+		$expireTime = time() + $expire;
+
+		return $expireTime;
+	}
 }
 ?>
-- 
GitLab