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
}
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:
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.
This for example will work fine if you have the following implicit conversion in scope:
Finally it is also quite easy to add some sugar to the guava cache itself:
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.