diff --git a/config/config-template.php b/config/config-template.php index 46785d76f9360df0ddba4c2899ebb434d0a1fad0..27ae28fd4c75a1dfa9eda8589c8a47cc817ff85e 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 b9fde9273e7d51366ed904421cfb238db2b47aa7..ce3b51dbfd3ce3b6c468def3d9b07c560976186c 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; + } } ?>