Searching a scala caching framework with annotation like (spring + ehcache)

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:

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

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 = 
   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.

Need Your Help

Simple, efficient way to writing to a specific line number in python

python file-io

I have a file and I need to write some contents on a specific line. I know which lines and I want to override the contents of that line.

Socket Connection Error

c# .net sockets

I am facing a wierd socket connection problem in .net windows application.I am using socket from .net to asynchonously communicate to a legacy intersystems cache database.I have a specific timeout ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.