开发者

Persistent resources in a rails app

First, this may not be the best title, but it seems to make sense at this time.

What I'm looking at is loading a resource which should live for the life of the web app. There may be some provisioning at a later point for a manual refresh, but currently that is not the case.

We have a complex permission structure which resides in the database for multiple reasons. I do not want to incur the overhead of retrieving this for each page load, thus I want it to reside in memory. My first instinct is to create a singleton which I load this into and use it whenever needed to lookup a permission. I understand the hesitance to开发者_运维问答wards singletons and wonder if that is a poor approach.

I do not want to go down the route of yaml or another storage mechanism, the permissions must reside in the DB for other dependencies. That said, in Rails, what would be the most appropriate way to efficiently load and read the data?


This sounds like the perfect use of the cache

permissions = Rails.cache.fetch( 'permissions' ) do
  # Permissions don't exist yet, perform long operation and load from DB
  load_permissions_from_db
end

More details here.


I'm not totally sure what you mean but i think there are a few ways you could go

  • caching (e.g. caches_page :page or caches_action :action in the controller )
  • or possibly storing something in a cookie/ session data, of course i don't totally understand the nature of this data so I don't Know what would work better, if at all
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜