Created
March 16, 2015 16:21
-
-
Save rahilb/1e61739c00f5554f5763 to your computer and use it in GitHub Desktop.
In memory ttl cache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class InMemoryTTLCache[T <: AnyRef](settings: TTLCacheSettings, zeroFun: String => T) extends Cache[T] { | |
val underlying = CacheBuilder.newBuilder() | |
.initialCapacity(30) | |
.expireAfterWrite(settings.maxAgeInSeconds, TimeUnit.SECONDS) | |
.build[String, T](new CacheLoader[String, T] { | |
override def load(key: String): T = zeroFun(key) | |
}) | |
override def get(cacheKey: String): Option[T] = Option(underlying.get(cacheKey)) | |
override def set(cacheKey: String, value: T): Unit = underlying.put(cacheKey, value) | |
override def delete(cacheKeys: Seq[String]): Unit = cacheKeys.foreach(underlying.invalidate) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment