diff --git a/modules/metarefresh/config-templates/config-metarefresh.php b/modules/metarefresh/config-templates/config-metarefresh.php
index 0fc7503382342f715d876828aea3a60693c6d457..1efee0efd369d6e63084acd2a155ea5f8c191ccc 100644
--- a/modules/metarefresh/config-templates/config-metarefresh.php
+++ b/modules/metarefresh/config-templates/config-metarefresh.php
@@ -20,6 +20,12 @@ $config = array(
 			),
 			'expireAfter' 		=> 60*60*24*4, // Maximum 4 days cache time.
 			'outputDir' 	=> 'metadata/metadata-kalmar-consuming/',
+
+			/*
+			 * Which output format the metadata should be saved as.
+			 * Can be 'flatfile' or 'serialize'. 'flatfile' is the default.
+			 */
+			'outputFormat' => 'flatfile',
 		),
 	),
 );
diff --git a/modules/metarefresh/hooks/hook_cron.php b/modules/metarefresh/hooks/hook_cron.php
index 375cdf744cace027e38ddfe5818c70c7f97be79e..bf91de66079e3983b3b19577ff79518e8a137a00 100644
--- a/modules/metarefresh/hooks/hook_cron.php
+++ b/modules/metarefresh/hooks/hook_cron.php
@@ -41,7 +41,16 @@ function metarefresh_hook_cron(&$croninfo) {
 			$outputDir = $set->getString('outputDir');
 			$outputDir = $config->resolvePath($outputDir);
 
-			$metaloader->writeMetadataFiles($outputDir);
+			$outputFormat = $set->getValueValidate('outputFormat', array('flatfile', 'serialize'), 'flatfile');
+			switch ($outputFormat) {
+			case 'flatfile':
+				$metaloader->writeMetadataFiles($outputDir);
+				break;
+			case 'serialize':
+				$metaloader->writeMetadataSerialize($outputDir);
+				break;
+			}
+
 		}
 
 	} catch (Exception $e) {
diff --git a/modules/metarefresh/lib/MetaLoader.php b/modules/metarefresh/lib/MetaLoader.php
index 26067a7d8223230994b5d8aa08959e09a973b94c..b06ff2f673ddd08c9ae2017aa115cc836aca8985 100644
--- a/modules/metarefresh/lib/MetaLoader.php
+++ b/modules/metarefresh/lib/MetaLoader.php
@@ -184,6 +184,49 @@ class sspmod_metarefresh_MetaLoader {
 		}
 	}
 
+
+	/**
+	 * Save metadata for loading with the 'serialize' metadata loader.
+	 *
+	 * @param string $outputDir  The directory we should save the metadata to.
+	 */
+	public function writeMetadataSerialize($outputDir) {
+		assert('is_string($outputDir)');
+
+		$metaHandler = new SimpleSAML_Metadata_MetaDataStorageHandlerSerialize(array('directory' => $outputDir));
+
+		/* First we add all the metadata entries to the metadata handler. */
+		foreach ($this->metadata as $set => $elements) {
+			foreach ($elements as $m) {
+				$entityId = $m['metadata']['entityid'];
+
+				SimpleSAML_Logger::debug('metarefresh: Add metadata entry ' .
+					var_export($entityId, TRUE) . ' in set ' . var_export($set, TRUE) . '.');
+				$metaHandler->saveMetadata($entityId, $set, $m['metadata']);
+			}
+		}
+
+		/* Then we delete old entries which should no longer exist. */
+		$ct = time();
+		foreach ($metaHandler->getMetadataSets() as $set) {
+			foreach ($metaHandler->getMetadataSet($set) as $entityId => $metadata) {
+				if (!array_key_exists('expire', $metadata)) {
+					SimpleSAML_Logger::warning('metarefresh: Metadata entry without expire ' .
+						'timestamp: ' . var_export($entityId, TRUE) . ' in set ' .
+						var_export($set, TRUE) . '.');
+				}
+				if ($metadata['expire'] > $ct) {
+					continue;
+				}
+
+				SimpleSAML_Logger::debug('metarefresh: Delete expired metadata entry ' .
+					var_export($entityId, TRUE) . ' in set ' . var_export($set, TRUE) . '.');
+				$metaHandler->deleteMetadata($entityId, $set);
+			}
+		}
+	}
+
+
 	private function getTime() {
 		/* The current date, as a string. */
 		date_default_timezone_set('UTC');