Skip to content

Instantly share code, notes, and snippets.

@rahilb
Created March 16, 2015 16:21
Show Gist options
  • Save rahilb/1e61739c00f5554f5763 to your computer and use it in GitHub Desktop.
Save rahilb/1e61739c00f5554f5763 to your computer and use it in GitHub Desktop.
In memory ttl cache
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