Giter Site home page Giter Site logo

preheat's Introduction

Preheat

IN DEVELOPMENT DISCLAMER: This gem is still under development and hasn’t been put through intensive real-world testing yet. Any comments/concerns/recommendations would be greatly appreciated.

Keep your Rails.cache up to date without:

  • code bloat in your Rails.cache.fetch invocations

  • tedious dependency setup in a cron task to mimic what your controller does, just so you can call your model/library methods which have the Rails.cache.fetch invocations

Anything executed in a Preheat.it block will change all fetch calls into a fetch calls with :force => true. (:force => true will force a cache-miss and a subsequent cache-write)

Example

This will “preheat” all your Rails.cache.fetch calls on your homepage. It is as simple as that!

Preheat.it do
  app.get("/")
end

Note: If you have not seen “app.get” used before, the “app” object is not related to my preheat gem. It ships with rails: more detail here. I use app.get because ActiveSupport’s fetch method is being modified only in the ruby process which is using Preheat, so something like mechanize/wget/curl would call the page through your frontend webserver and would not be effected by Preheat.it, while app.get will directly call your controller in that same ruby process.

A more detailed example

Let’s say we have a list of product pages where we want the cached to be updated every hour.

#app/models/product.rb
def slow_method
  Rails.cache.fetch("product-slow-method-#{self.id}") do
    sleep 15
    Time.now
  end
end

#lib/tasks/preheat.rake
namespace :preheat do
  desc "Preheat product caches"
  task (:products => :environment) do
    Preheat.it do
      Product.all.each do |product|
        app.get(app.products_path(product)) #or you could just call product.slow_method directly, whatever makes more sense
      end
    end
  end
end

#crontab -e
0 * * * * /path/to/rake preheat:products RAILS_ENV=production 2>&1 >> #{Rails.root}/log/preheat.log &

Installation

  1. gem install preheat

  2. Add the gem to environment.rb or your Gemfile

Thanks

John Hume for helping me cleanup part of my code. Brian Guthrie for getting me all learned up on gem dependencies and the like.

preheat's People

Contributors

tommyh avatar

Watchers

James Cloos avatar Kurt Tomlinson avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.