Rails 3: Caching to Global Variable -


i'm sure "global variable" hair on of everyone's neck standing up. i'm trying store hierarchical menu in acts_as_tree data table (done). in application_helper.rb, create html menu querying database , walking tree (done). don't want every page load.

here's tried:

application.rb

config.menu = nil 

application_helper.rb

def my_menu_builder   return myapp::application.config.menu if myapp::application.config.menu   # menu building code should run once   myapp::application.config.menu = menu_html end 

menu_controller.rb

def create   # whatever create code   expire_menu_cache end  protected def expire_menu_cache   myapp::application.config.menu = nil end 

where stand right on first page load, database is, indeed, queried , menu built. results stored in config variable , database never again hit this.

it's cache expiration part that's not working. when reset config.menu variable nil, presumably next time through my_menu_builder, detect change , rebuild menu, caching new results. doesn't seem happen.

questions:

is application.config place store stuff this? see obvious flaw in caching strategy?

don't premature optimization -- that's phase i'm in. premature-optimization iteration :)

thanks!

one way achieve set empty hash in application.rb file:

my_vars = {} 

then can add whatever want in hash accessible everywhere.

my_vars[:foo] = "bar" 

and elsewhere:

my_vars[:foo] 

as felt, not rails way behave, if works. there different ways use caching in rails:

  • simple cache in memory explained here:

    rails.cache.read("city") # => nil

    rails.cache.write("city", "duckburgh")

    rails.cache.read("city") # => "duckburgh"

  • use of real engine memcached

i encourage have @ http://railslab.newrelic.com/scaling-rails

this place learn caching in it's shapes.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -