Skip to content

Commit

Permalink
Add annotation to exclude params from memoize cache keys
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Birchall committed Feb 10, 2015
1 parent 92728af commit 634705c
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 1 deletion.
8 changes: 7 additions & 1 deletion core/src/main/scala/scalacache/memoization/Macros.scala
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,13 @@ object Macros {

private def paramListsToTree(c: blackbox.Context)(symbolss: List[List[c.Symbol]]): c.Tree = {
import c.universe._
val identss: List[List[Ident]] = symbolss.map(ss => ss.map(s => Ident(s.name)))
val cacheKeyExcludeType = c.typeOf[cacheKeyExclude]
def shouldExclude(s: c.Symbol) = {
s.annotations.exists(a => a.tree.tpe == cacheKeyExcludeType)
}
val identss: List[List[Ident]] = symbolss.map(ss => ss.collect {
case s if !shouldExclude(s) => Ident(s.name)
})
listToTree(c)(identss.map(is => listToTree(c)(is)))
}

Expand Down
6 changes: 6 additions & 0 deletions core/src/main/scala/scalacache/memoization/annotations.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package scalacache.memoization

import scala.annotation.StaticAnnotation

final class cacheKeyExclude extends StaticAnnotation

Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ class CacheKeyExcludingConstructorParamsSpec extends FlatSpec with CacheKeySpecC
}
}

it should "exclude values of arguments annotated with @cacheKeyExclude" in {
checkCacheKey("scalacache.memoization.CacheKeySpecCommon.withExcludedParams(1, 3)()") {
withExcludedParams(1, "2", "3")(4)
}
}

it should "work for a method inside a class" in {
checkCacheKey("scalacache.memoization.AClass.insideClass(1)") {
new AClass().insideClass(1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ class CacheKeyIncludingConstructorParamsSpec extends FlatSpec with CacheKeySpecC
}
}

it should "exclude values of constructor params annotated with @cacheKeyExclude" in {
val instance = new ClassWithExcludedConstructorParam(50, 10)
instance.scalaCache = scalaCache

checkCacheKey("scalacache.memoization.ClassWithExcludedConstructorParam(50).foo(42)") {
instance.foo(42)
}
}

it should "include values of all arguments for all argument lists" in {
checkCacheKey("scalacache.memoization.CacheKeySpecCommon.multipleArgLists(1, 2)(3, 4)") {
multipleArgLists(1, "2")("3", 4)
Expand All @@ -43,6 +52,12 @@ class CacheKeyIncludingConstructorParamsSpec extends FlatSpec with CacheKeySpecC
}
}

it should "exclude values of arguments annotated with @cacheKeyExclude" in {
checkCacheKey("scalacache.memoization.CacheKeySpecCommon.withExcludedParams(1, 3)()") {
withExcludedParams(1, "2", "3")(4)
}
}

it should "work for a method inside a class" in {
// The class's implicit param (the ScalaCache) should be included in the cache key)
checkCacheKey(s"scalacache.memoization.AClass()(${scalaCache.toString}).insideClass(1)") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ trait CacheKeySpecCommon extends Suite with Matchers with ScalaFutures with Befo
123
}

def withExcludedParams(a: Int, @cacheKeyExclude b: String, c: String)(@cacheKeyExclude d: Int): Int = memoize {
123
}

}

class AClass(implicit val scalaCache: ScalaCache) {
Expand Down Expand Up @@ -83,3 +87,10 @@ class ClassWithConstructorParams(b: Int) {
a + b
}
}

class ClassWithExcludedConstructorParam(b: Int, @cacheKeyExclude c: Int) {
implicit var scalaCache: ScalaCache = null
def foo(a: Int): Int = memoize {
a + b + c
}
}

0 comments on commit 634705c

Please sign in to comment.