-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support custom classloader for Redis deserialization
This is needed when using ScalaCache with Play. Fixes #32
- Loading branch information
Showing
8 changed files
with
217 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ target | |
coveralls-token.txt | ||
/*.html | ||
dump.rdb | ||
logs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package scalacache.redis | ||
|
||
import org.scalatest.{ BeforeAndAfter, Matchers, FlatSpec } | ||
|
||
import scalacache._ | ||
import memoization._ | ||
import redis._ | ||
|
||
case class User(id: Int, name: String) | ||
|
||
/** | ||
* Test to check the sample code in issue #32 runs OK | ||
* (just to isolate the use of the List[User] type from the Play classloader problem) | ||
*/ | ||
class Issue32Spec | ||
extends FlatSpec | ||
with Matchers | ||
with BeforeAndAfter | ||
with RedisTestUtil { | ||
|
||
assumingRedisIsRunning { (pool, client) => | ||
|
||
implicit val scalaCache = ScalaCache(RedisCache(pool)) | ||
|
||
def getUser(id: Int): List[User] = memoize { | ||
List(User(id, "Taro")) | ||
} | ||
|
||
"memoize and Redis" should "work with List[User]" in { | ||
getUser(1) should be(List(User(1, "Taro"))) | ||
getUser(1) should be(List(User(1, "Taro"))) | ||
} | ||
} | ||
|
||
} |
48 changes: 48 additions & 0 deletions
48
redis/src/test/scala/scalacache/redis/PlayIntegrationSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package scalacache.redis | ||
|
||
import _root_.redis.clients.jedis.JedisPool | ||
import org.scalatest.{ FlatSpec, Matchers, TestData } | ||
import org.scalatestplus.play.OneAppPerTest | ||
import play.api.test.FakeApplication | ||
import play.api.{ Application, GlobalSettings } | ||
|
||
import scalacache._ | ||
import scalacache.memoization._ | ||
|
||
class PlayIntegrationSpec extends FlatSpec with Matchers with OneAppPerTest { | ||
|
||
override def newAppForTest(testData: TestData) = new FakeApplication( | ||
withGlobal = Some(Global) | ||
) | ||
|
||
"Redis and memoization" should "work with Play in one application" in { | ||
Global.getItems(List(1, 2)) should be(List(Item(1, "Chris"), Item(2, "Chris"))) | ||
Global.getItems(List(1, 2)) should be(List(Item(1, "Chris"), Item(2, "Chris"))) | ||
} | ||
|
||
"Redis and memoization" should "work with Play in another application" in { | ||
Global.getItems(List(1, 2)) should be(List(Item(1, "Chris"), Item(2, "Chris"))) | ||
Global.getItems(List(1, 2)) should be(List(Item(1, "Chris"), Item(2, "Chris"))) | ||
} | ||
|
||
} | ||
|
||
case class Item(id: Int, name: String) | ||
|
||
object Global extends GlobalSettings { | ||
@volatile implicit var jedisPool: JedisPool = _ | ||
@volatile implicit var scalaCache: ScalaCache = _ | ||
|
||
override def onStart(app: Application): Unit = { | ||
jedisPool = new JedisPool("localhost", 6379) | ||
scalaCache = ScalaCache(RedisCache(jedisPool, customClassloader = Some(app.classloader))) | ||
} | ||
|
||
override def onStop(app: Application): Unit = { | ||
jedisPool.destroy() | ||
} | ||
|
||
def getItems(ids: List[Int]): List[Item] = memoize { | ||
ids map { Item(_, "Chris") } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.