Skip to content

Commit

Permalink
Add a close() method to Cache
Browse files Browse the repository at this point in the history
  • Loading branch information
cb372 committed May 17, 2015
1 parent 32d28f5 commit d450130
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 0 deletions.
10 changes: 10 additions & 0 deletions core/src/main/scala/scalacache/Cache.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,15 @@ trait Cache {
*/
def remove(key: String): Future[Unit]

/**
* You should call this when you have finished using this Cache.
* (e.g. when your application shuts down.
*
* It will take care of gracefully shutting down the underlying cache client.
*
* Note that you should not try to use this Cache instance after you have called this method.
*/
def close(): Unit

}

6 changes: 6 additions & 0 deletions core/src/test/scala/scalacache/mocks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@ class EmptyCache extends Cache {
override def get[V](key: String): Future[Option[V]] = Future.successful(None)
override def put[V](key: String, value: V, ttl: Option[Duration]) = Future.successful((): Unit)
override def remove(key: String) = Future.successful((): Unit)
override def close(): Unit = {}
}

class FullCache(value: Any) extends Cache {
override def get[V](key: String): Future[Option[V]] = Future.successful(Some(value).asInstanceOf[Option[V]])
override def put[V](key: String, value: V, ttl: Option[Duration]) = Future.successful((): Unit)
override def remove(key: String) = Future.successful((): Unit)
override def close(): Unit = {}
}

class FailedFutureReturningCache extends Cache {
override def get[V](key: String): Future[Option[V]] = Future.failed(new RuntimeException("failed to read"))
override def put[V](key: String, value: V, ttl: Option[Duration]): Future[Unit] = Future.failed(new RuntimeException("failed to write"))
override def remove(key: String) = Future.successful((): Unit)
override def close(): Unit = {}
}

/**
Expand All @@ -40,6 +43,9 @@ class MockCache extends Cache {

def remove(key: String) =
Future.successful(mmap.remove(key))

def close(): Unit = {}

}

/**
Expand Down
4 changes: 4 additions & 0 deletions ehcache/src/main/scala/scalacache/ehcache/EhcacheCache.scala
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ class EhcacheCache(underlying: Ehcache)
*/
override def remove(key: String) = Future.successful(underlying.remove(key))

override def close(): Unit = {
// Nothing to do
}

}

object EhcacheCache {
Expand Down
4 changes: 4 additions & 0 deletions guava/src/main/scala/scalacache/guava/GuavaCache.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ class GuavaCache(underlying: GCache[String, Object])
*/
override def remove(key: String) = Future.successful(underlying.invalidate(key))

override def close(): Unit = {
// Nothing to do
}

private def toExpiryTime(ttl: Duration): DateTime = DateTime.now.plusMillis(ttl.toMillis.toInt)

}
Expand Down
4 changes: 4 additions & 0 deletions lrumap/src/main/scala/scalacache/lrumap/LruMapCache.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ class LruMapCache(underlying: LruMap[String, Object])
override def remove(key: String): Future[Unit] =
Future.successful(underlying.remove(key))

override def close(): Unit = {
// Nothing to do
}

private def toExpiryTime(ttl: Duration): DateTime = DateTime.now.plusMillis(ttl.toMillis.toInt)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ class MemcachedCache(client: MemcachedClient, keySanitizer: MemcachedKeySanitize
p.future
}

override def close(): Unit = {
client.shutdown()
}

}

object MemcachedCache {
Expand Down
4 changes: 4 additions & 0 deletions redis/src/main/scala/scalacache/redis/RedisCache.scala
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ class RedisCache(jedisPool: JedisPool, override val customClassloader: Option[Cl
}
}

override def close(): Unit = {
jedisPool.close()
}

private def withJedisClient[T](f: Jedis => T): T = {
val jedis = jedisPool.getResource()
try {
Expand Down

0 comments on commit d450130

Please sign in to comment.