1

I'm at the moment using spring AOP + Ehcache for our scala application. I really like the simplicity of using it with annotation but the not the part of using a big framework like spring to perform caching.

Do you know scala caching solution without being obliged to use a big framework like spring ?

Something like that without spring :

 @Cacheable(cacheName = "users") def getByUserName(userName: String): User = { val userEntityFound = dao.findOne(dao.createQuery.field("email").equal(userName)) userEntityMapper mapToDomainObject userEntityFound } 
2
  • "Native Scala" as in "written in Scala"? I mean, ehcache doesn't require Spring. Commented Sep 8, 2012 at 13:30
  • I added more information to my question Dave, thanks ! Commented Sep 8, 2012 at 13:51

1 Answer 1

6

I think you'll have to differentiate between the caching library and the API to access it. The caching libraries I have seen been used in a Scala environment are the same than the ones used in Java land. For (potentially) distributed caching something like Ehcache or memcached is used. For local caching the cache utilities of the guava library are commonly used.

It is quite easy to write a wrapper to the native cache APIs in order to have a more scala idiomatic way to access the cache. Annotations as you have used them in your example is a common way to access library abstractions in Java but it is not that common in the Scala world. The play! framework for example has a cache abstraction which is, by default, bound to Ehcache. It allows constructs like:

val user: User = Cache.getOrElseAs[User]("item.key") { User.findById(connectedUser) } 

Twitter had a wrapper for the guava cache in their scala utils but removed them. I think that is the case because in current guava versions it is very simple / pragmatic to access it directly.

val cache = CacheBuilder.newBuilder(). maximumSize(1000). build((key:String) => q(key)) 

This for example will work fine if you have the following implicit conversion in scope:

implicit def functionToCacheLoader[F, T](f: F => T) = { new CacheLoader[F, T] { def load(key: F) = f(key) } } 

Finally it is also quite easy to add some sugar to the guava cache itself:

implicit def pimpCache[F, T](cache: Cache[F, T]) = { new PimpedCache(cache) } class PimpedCache[F, T](cache: Cache[F, T]) { def getOption(key: F) = { val value = cache.getIfPresent(key) if(value == null) None else Some(value) } } 

Those are the main abstractions for caching I use. Here is a link to a gist with all the code I need. I also have a plugin for the play! Cache abstraction which uses guava under the hood which I can share if you have the need to use it.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.