Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a close() method to Cache #49

Merged
merged 2 commits into from
May 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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