scalacache: simple caching in scala
DESCRIPTION
https://github.com/cb372/scalacacheTRANSCRIPT
![Page 1: ScalaCache: simple caching in Scala](https://reader036.vdocuments.net/reader036/viewer/2022081809/55649142d8b42a73418b5390/html5/thumbnails/1.jpg)
ScalaCacheChris Birchall#m3dev Tech Talk2014/04/23
![Page 2: ScalaCache: simple caching in Scala](https://reader036.vdocuments.net/reader036/viewer/2022081809/55649142d8b42a73418b5390/html5/thumbnails/2.jpg)
ScalaCache
Simple facade for popular cache systems● Google Guava● Ehcache● Memcached● Redis● … easy to add your own
![Page 3: ScalaCache: simple caching in Scala](https://reader036.vdocuments.net/reader036/viewer/2022081809/55649142d8b42a73418b5390/html5/thumbnails/3.jpg)
Example
Full source herehttps://gist.github.com/cb372/11200131
![Page 4: ScalaCache: simple caching in Scala](https://reader036.vdocuments.net/reader036/viewer/2022081809/55649142d8b42a73418b5390/html5/thumbnails/4.jpg)
The Java way:
● Unavoidable repetition● Weak against refactoring
KILLER FEATURE! Method memoization
public int foo(int a, int b, String c) { String key = toKey(“foo”, a, b, c); return withCache(key, () -> { // … do stuff … return x; });}
![Page 5: ScalaCache: simple caching in Scala](https://reader036.vdocuments.net/reader036/viewer/2022081809/55649142d8b42a73418b5390/html5/thumbnails/5.jpg)
The Spring way:
● AOP, Proxy objects = runtime magic● Some restrictions, e.g. cannot call method
from the same class
KILLER FEATURE! Method memoization
@Cacheable(“cacheName”)public int foo(int a, int b, String c) { // … do stuff ... return x;}
![Page 6: ScalaCache: simple caching in Scala](https://reader036.vdocuments.net/reader036/viewer/2022081809/55649142d8b42a73418b5390/html5/thumbnails/6.jpg)
The ScalaCache way:
● Macros = compile-time magic!● Auto-generates cache key, e.g. “com.
bar.Baz.foo(123, 456, xyz)”
KILLER FEATURE! Method memoization
def foo(a: Int, b: Int, c: String): Int = memoize { // … do stuff … x}
![Page 7: ScalaCache: simple caching in Scala](https://reader036.vdocuments.net/reader036/viewer/2022081809/55649142d8b42a73418b5390/html5/thumbnails/7.jpg)
Aside: Quasiquotes
Macros got a lot easier to write in 2.11e.g.
Before
After
![Page 8: ScalaCache: simple caching in Scala](https://reader036.vdocuments.net/reader036/viewer/2022081809/55649142d8b42a73418b5390/html5/thumbnails/8.jpg)
Roadmap
Investigating new features for ScalaCache● Async support● Versioning, a.k.a. key-based expiration● Customizable serialization● API improvements● Integration with browser caching à la Play
Cache
![Page 9: ScalaCache: simple caching in Scala](https://reader036.vdocuments.net/reader036/viewer/2022081809/55649142d8b42a73418b5390/html5/thumbnails/9.jpg)
Try it!scalaVersion := 2.11.0
resolvers += “Sonatype snapshots” at
“https://oss.sonatype.org/content/repositories/snapshots/”
libraryDependencies +=
“com.github.cb372” %% “scalacache” % “0.3.0-SNAPSHOT”
More info at https://github.com/cb372/scalacache