« Non-greedy mode in regex | Main | Serializing data speed comparison: Marshal vs. JSON vs. Eval vs. YAML »

August 19, 2008

Comments

hypa

No disrespect but this post is useless if you're not going fully explain the solution to the problem.

Off the top of my head the model should look something like:

class Post < ActiveRecord::Base

  after_save do |post| # or before_save (does it even matter?)
    post.reset_cache
    Post.expire_cache("permalink:#{post.permalink}")
  end

  def self.permalink_cached(permalink)
    get_cache("permalink:#{permalink}") do
      find_by_permalink(permalink)
    end
  end

end

Then in your controller you would do:

@post = Post.permalink_cached(params[:permalink])

I haven't tested the above code but it should get one on the right track. Hopefully this will help someone who came to this page experiencing this problem and is saying WTF due to many rails bloggers not completely explaining a topic they are blogging about.

Paul Dix

First, you must have missed the title of my blog. Second, I didn't include exact code because I thought the harder part of the problem was figuring out what was going on. When I hit this problem it took me a while to figure out what was happening and seconds to implement a fix. I assumed that readers using cache_fu would understand what The last sentence of the post meant, which tells you what to do to fix it.

However, this is the great part about comments on blogs. The readers are able to contribute. Your comment adds that extra piece for people who may not have understood what I meant. Congratulations on contributing. Comments are where it starts, ditching the attitude is next.

The comments to this entry are closed.

My Photo

Talks

Linkage

  • My Github
  • Feedzirra
    My Ruby library for parsing and fetching feeds at blinding speed.
  • SAX Machine
    My Ruby library exposes a DSL for building Nokogiri backed SAX parsers.
  • Typhoeus
    My Ruby library for running HTTP requests quickly, easily, and in parallel.
  • NYC Machine Learning Meetup
    The meetup I organize. Talks from researchers and practitioners on machine learning and related technologies and techniques.
  • Benchmark Solutions
    The financial market data startup I work for in NYC. We're hiring and need Javascript, Scala, C++, and Ruby programmers. We're also on the lookout for PhDs in statistics or machine learning.

Twitter / pauldix