Say I have a controller that returns a list of users.
The users are to be returned from memcache if the cache key exists, otherwise hit the mysql db.
This logic will be re-used in say a web service layer or something.
action:
def list
if in cache
@userlist = ...
else
@userlist = User.all()
end
end
In the Java world, you would create a UserService layer that would wrap additional logic (like first checking the cache layer, etc.).
In rails it people tend to put all this logic in the controller.
What is the Rails ‘best-practise’ here?
The “Rails way” is: skinny controllers, fat models.
You can simply change the model to support cache:
Or create an injector to support cache the way you want for multiple models:
Remember: Ruby, as a dynamic language, is very easy to extend. Mixins, interceptors, aspects, all those things that are a PITA to implement in Java, are very easy and natural on Ruby. Give it a try.