You can simply cache the answer of the Anorm methods. For example, real method that I use:
def findById(id: Long): Option[User] = {
Cache.getOrElse(userCacheKey + id, 60*60) {
DB.withConnection {
implicit connection =>
SQL("select * from publisher where id = {id}").on('id -> id).as(User.simple.singleOpt)
}
}
}
The code does the Select and stores the answer in the cache via getOrElse
. If the value is in the Cache, it will ber etrieved and no query will be executed.
The only issue is that when you update the entity User you'll have to update the cache (so it doesn't keep stale data):
// Assumes a user: User object available with the updated user
Cache.set(userCacheKey + id, cached.copy(name = user.name, avatar = user.avatar, bio = user.bio, url = user.url, location = user.location), 60*60)