Skip to content

Commit

Permalink
Avoid tuple usage in AttributeCaching constructor
Browse files Browse the repository at this point in the history
it creates synthetic sugar methods that appear to cause binary compatability problems.
  • Loading branch information
echeipesh committed Nov 16, 2017
1 parent 7c37663 commit 5e273bd
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions spark/src/main/scala/geotrellis/spark/io/AttributeCaching.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,45 +24,48 @@ import com.typesafe.config.ConfigFactory

import scala.concurrent.duration._

trait AttributeCaching { self: AttributeStore =>
object AttributeCaching {
private val config = ConfigFactory.load()
val expiration: Int = config.getInt("geotrellis.attribute.caching.expirationMinutes")
val maxSize: Int = config.getInt("geotrellis.attribute.caching.maxSize")
val enabled: Boolean = config.getBoolean("geotrellis.attribute.caching.enabled")
}

private final val (cacheEnabled, cache) = {
val config = ConfigFactory.load()
val expiration = config.getInt("geotrellis.attribute.caching.expirationMinutes")
val maxSize = config.getInt("geotrellis.attribute.caching.maxSize")
val enabled = config.getBoolean("geotrellis.attribute.caching.enabled")
trait AttributeCaching { self: AttributeStore =>
import AttributeCaching._

enabled -> Scaffeine()
private final val cache = {
Scaffeine()
.recordStats()
.expireAfterWrite(expiration.minutes)
.maximumSize(maxSize)
.build[(LayerId, String), Any]
}

def cacheRead[T: JsonFormat](layerId: LayerId, attributeName: String): T = {
if(cacheEnabled)
if(enabled)
cache.get(layerId -> attributeName, { _ => read[T](layerId, attributeName) }).asInstanceOf[T]
else
read[T](layerId, attributeName)
}

def cacheWrite[T: JsonFormat](layerId: LayerId, attributeName: String, value: T): Unit = {
if(cacheEnabled) cache.put(layerId -> attributeName, value)
if(enabled) cache.put(layerId -> attributeName, value)
write[T](layerId, attributeName, value)
}

def clearCache(): Unit = {
if(cacheEnabled) cache.invalidateAll()
if(enabled) cache.invalidateAll()
}

def clearCache(id: LayerId): Unit = {
if(cacheEnabled) {
if(enabled) {
val toInvalidate = cache.asMap.keys.filter(_._1 == id)
cache.invalidateAll(toInvalidate)
}
}

def clearCache(id: LayerId, attribute: String): Unit = {
if(cacheEnabled) cache.invalidate(id -> attribute)
if(enabled) cache.invalidate(id -> attribute)
}
}

0 comments on commit 5e273bd

Please sign in to comment.