Thursday, October 18, 2012

Rails page cache sweeping

Fwoosh fwoosh.  Caching is easy.  Expiring those caches on the other hand can drive you absolutely crazy.

Two things I've learned.

Expiring the cache when your Model is used in a single monolithic pattern is a piece of cake.  Following any standard documentation is easy peasy.

However, one of my Models generates an N number of directories and files dependent on the user's setup.

For example, SiteA can have About Us, FAQ, and Contacts as directories with a number of db served pages for each that are cached.

After many many failed attempts here's what I've learned:

  1. You must setup in config/application.rb the config.active_record.observers = :observer or it will never be found.
  2. Sweepers need to be explicitly told which model to observe "observe :model"
  3. use of {page.cache.directory} needs to be more explicit with  "#{ActionController::Base.page_cache_directory}"
  4. For situations where mass numbers of pages need to be expired simultaneously, be very careful in the use of FileUtils.rm_rf improper usage can blow up your file system.

The last one was a tip learned from the venerable Ryan Bates of Railscasts fame -- of course I'm a subscriber. 

Additionally, when I had a Model that generated a single directory with n number of pages inside such as when caching a paged view, the solution was to do:

FileUtils.rm_rf "#{ActionController::Base.page_cache_directory}/directory"

But I had one model that produced an unknown number of pages.  At first I wanted to hard code the directory structure by copy and pasting the above line changing the final directory.  But that's just stupid.  I've learned that every time I found myself hacking out that pattern that I needed to stop, and rethink the problem.

Since the problem was DB generated, the solution had to be as well.

I simply yanked out all the pathnames from the db and rm -rf each of those.

Model.all.each do |page|
  FileUtils.rm_rf "#{ActionController::Base.page_cache_directory}/#{page.path}"
end




12 comments:

  1. Sweeping services are needed for cleaning environment. City Linemarking & Maintenance who provides the schools sweeping services sydney and line marking services at the reasonable cost.

    ReplyDelete
  2. I must thank you for posting this blog because the topic is very much in demand today and everyone wants to read about it. The blog contains very valuable information.For More linemarking.service visit at citylinemarking.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Nice article. thanks for share this. i like your blog post becuse your post is very usful for me. we also provide service for line marking services Sydney. for more information visit on our website.

    ReplyDelete
  5. Great information, this information is necessary and essential for everyone. I am thankful to you for providing this kind of good information. line marking gold coast

    ReplyDelete
  6. I truly value your work which you have shared here. The article you have shared is extremely enlightening and the focuses you have referenced are exceptionally useful. Much obliged to you for sharing this postSafety Line Marking

    ReplyDelete
  7. Mega Moolah is much from the only epic slot recreation at Jackpot City. This website has all the favourites from all one of the best software providers. Today, we’re going to take a look at|to have a look at} the highest on-line pokie sites in New Zealand and one of the best pokies that each 온라인카지노 of them has to offer.

    ReplyDelete
  8. Russia has been a hotbed of slots-related malfeasance since 2009, when the nation outlawed just about all playing. Some of those cut-rate slots wound up within the palms of counterfeiters eager to methods to|learn to} load new games onto old circuit 카지노 사이트 boards. Others apparently went to Murat Bliev’s bosses in St. Petersburg, who had been keen to probe the machines’ source code for vulnerabilities. Modern gaming machines use computer technology to function their functions. Slot machine outcomes are determined unsing a Random Number Generator which is a mathematically-based program that selects teams of numbers to determine out} which symbols are selected to provide a successful or dropping outcome. Each image is chosen randomly and the selection of that image just isn't influenced by outside factors like earlier outcomes of winning/losing history.

    ReplyDelete