Skip to content

Commit

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

/**
* Delete the entire contents of the cache. Use wisely!
*/
def removeAll(): Future[Unit]

/**
* You should call this when you have finished using this Cache.
* (e.g. when your application shuts down)
Expand Down
6 changes: 6 additions & 0 deletions core/src/main/scala/scalacache/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ package object scalacache extends StrictLogging {
def remove(keyParts: Any*)(implicit scalaCache: ScalaCache): Future[Unit] =
scalaCache.cache.remove(toKey(keyParts))

/**
* Delete the entire contents of the cache. Use wisely!
*/
def removeAll()(implicit scalaCache: ScalaCache): Future[Unit] =
scalaCache.cache.removeAll()

/**
* Wrap the given block with a caching decorator.
* First look in the cache. If the value is found, then return it immediately.
Expand Down
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,20 +8,23 @@ 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 removeAll() = 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 removeAll() = 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 removeAll() = Future.successful((): Unit)
override def close(): Unit = {}
}

Expand All @@ -44,6 +47,9 @@ class MockCache extends Cache {
def remove(key: String) =
Future.successful(mmap.remove(key))

def removeAll() =
Future.successful(mmap.clear())

def close(): Unit = {}

}
Expand Down
2 changes: 2 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,8 @@ class EhcacheCache(underlying: Ehcache)
*/
override def remove(key: String) = Future.successful(underlying.remove(key))

override def removeAll() = Future.successful(underlying.removeAll())

override def close(): Unit = {
// Nothing to do
}
Expand Down
2 changes: 2 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,8 @@ class GuavaCache(underlying: GCache[String, Object])
*/
override def remove(key: String) = Future.successful(underlying.invalidate(key))

override def removeAll() = Future.successful(underlying.invalidateAll())

override def close(): Unit = {
// Nothing to do
}
Expand Down
4 changes: 3 additions & 1 deletion lrumap/src/main/scala/scalacache/lrumap/LruMapCache.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class LruMapCache(underlying: LruMap[String, Object])
override def remove(key: String): Future[Unit] =
Future.successful(underlying.remove(key))

override def removeAll() = Future.successful(underlying.clear())

override def close(): Unit = {
// Nothing to do
}
Expand All @@ -85,4 +87,4 @@ object LruMapCache {
*/
def isExpired: Boolean = expiresAt.exists(_.isBeforeNow)
}
}
}
11 changes: 11 additions & 0 deletions memcached/src/main/scala/scalacache/memcached/MemcachedCache.scala
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ class MemcachedCache(client: MemcachedClient, keySanitizer: MemcachedKeySanitize
p.future
}

override def removeAll() = {
val p = Promise[Unit]()
val f = client.flush()
f.addListener(new OperationCompletionListener {
def onComplete(g: OperationFuture[_]): Unit = p.complete {
Success(())
}
})
p.future
}

override def close(): Unit = {
client.shutdown()
}
Expand Down
8 changes: 8 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,14 @@ class RedisCache(jedisPool: JedisPool, override val customClassloader: Option[Cl
}
}

override def removeAll() = Future {
blocking {
withJedisClient { client =>
client.flushDB()
}
}
}

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

0 comments on commit 15cefea

Please sign in to comment.