From 0528d6abbe6aec7652db117b3a2f5da1157c09b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0est=C3=A1k=20V=C3=ADt?= Date: Mon, 19 Dec 2016 14:48:01 +0100 Subject: [PATCH] Caching using FileCacheApi (for dev env) is now less memory hungry --- app/modules/ConfigModule.scala | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/app/modules/ConfigModule.scala b/app/modules/ConfigModule.scala index 359c119..c66ad90 100644 --- a/app/modules/ConfigModule.scala +++ b/app/modules/ConfigModule.scala @@ -34,11 +34,9 @@ class FileCacheApi(path: Path) extends CacheApi{ private def cacheFile(name: String) = path.resolve("X-"+URLEncoder.encode(name, "utf-8")) override def remove(key: String): Unit = Files.deleteIfExists(cacheFile(key)) - private def serialize(value: Any, duration: Duration) = { - val out = new ByteArrayOutputStream() + private def serialize(out: ObjectOutputStream, value: Any, duration: Duration) = { import com.github.nscala_time.time.Imports._ - new ObjectOutputStream(out).writeObject((value, if(duration.isFinite()) Some(DateTime.now.plus(duration.toMillis)) else None)) - out.toByteArray + out.writeObject((value, if(duration.isFinite()) Some(DateTime.now.plus(duration.toMillis)) else None)) } private def unserialize[T](data: Array[Byte]): Try[T] = { @@ -56,7 +54,9 @@ class FileCacheApi(path: Path) extends CacheApi{ } override def set(key: String, value: Any, expiration: Duration): Unit = { - Files.write(cacheFile(key), serialize(value, expiration)) + for(out <- resource.managed(new ObjectOutputStream(new FileOutputStream(cacheFile(key).toFile)))){ + serialize(out, value, expiration) + } } override def get[T: ClassTag](key: String): Option[T] = {