mirror of
https://github.com/ysoftdevs/odc-analyzer.git
synced 2026-03-19 15:55:42 +01:00
Caching using FileCacheApi (for dev env) is now less memory hungry
This commit is contained in:
@@ -34,11 +34,9 @@ class FileCacheApi(path: Path) extends CacheApi{
|
|||||||
private def cacheFile(name: String) = path.resolve("X-"+URLEncoder.encode(name, "utf-8"))
|
private def cacheFile(name: String) = path.resolve("X-"+URLEncoder.encode(name, "utf-8"))
|
||||||
override def remove(key: String): Unit = Files.deleteIfExists(cacheFile(key))
|
override def remove(key: String): Unit = Files.deleteIfExists(cacheFile(key))
|
||||||
|
|
||||||
private def serialize(value: Any, duration: Duration) = {
|
private def serialize(out: ObjectOutputStream, value: Any, duration: Duration) = {
|
||||||
val out = new ByteArrayOutputStream()
|
|
||||||
import com.github.nscala_time.time.Imports._
|
import com.github.nscala_time.time.Imports._
|
||||||
new ObjectOutputStream(out).writeObject((value, if(duration.isFinite()) Some(DateTime.now.plus(duration.toMillis)) else None))
|
out.writeObject((value, if(duration.isFinite()) Some(DateTime.now.plus(duration.toMillis)) else None))
|
||||||
out.toByteArray
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private def unserialize[T](data: Array[Byte]): Try[T] = {
|
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 = {
|
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] = {
|
override def get[T: ClassTag](key: String): Option[T] = {
|
||||||
|
|||||||
Reference in New Issue
Block a user