Permalink
Browse files

Add memcachier caching

  • Loading branch information...
gdiggs committed Jan 22, 2013
1 parent 504c446 commit f10d812e2365e206c5161eb1044e0edec98b5c87
Showing with 25 additions and 1 deletion.
  1. +2 −0 Gemfile
  2. +4 −0 Gemfile.lock
  3. +15 −1 cataloguais.rb
  4. +4 −0 models/item.rb
View
@@ -12,6 +12,8 @@ gem 'sinatra-config-file'
gem 'json'
gem 'tux'
gem 'googlecharts', :git => 'git://github.com/mattetti/googlecharts.git'
+gem 'dalli'
+gem 'memcachier'
group :development do
gem 'shotgun'
View
@@ -12,6 +12,7 @@ GEM
bond (0.4.1)
bson_ext (1.3.1)
daemons (1.1.4)
+ dalli (2.6.0)
data_mapper (1.0.2)
dm-aggregates (= 1.0.2)
dm-constraints (= 1.0.2)
@@ -66,6 +67,7 @@ GEM
haml (3.1.3)
json (1.5.3)
json_pure (1.6.5)
+ memcachier (0.0.2)
rack (1.3.2)
rack-test (0.6.0)
rack (>= 1.0)
@@ -106,12 +108,14 @@ PLATFORMS
DEPENDENCIES
bson_ext
+ dalli
data_mapper
dm-ar-finders
dm-postgres-adapter
googlecharts!
haml
json
+ memcachier
rack-test
sass
shotgun
View
@@ -27,6 +27,8 @@
# initialize the graph urls on startup
set :graph_urls, {}
+ set :cache, Dalli::Client.new
+
end
configure :production do
@@ -79,7 +81,15 @@
end
@direction = params[:direction] || :asc
@direction = @direction.to_sym if @direction
- @items = Item.search_and_sort(@sort.dup, @direction, params[:search])
+
+ # try to fetch results from cache
+ cache_key = get_cache_key [@sort, @direction, params[:search]]
+ @items = settings.cache.get(cache_key)
+ if !@items
+ @items = Item.search_and_sort(@sort.dup, @direction, params[:search])
+ settings.cache.set(cache_key, @items)
+ end
+
haml :index
end
@@ -169,6 +179,10 @@
# catch trailing spaces from https://gist.github.com/867165
get %r{(.+)/$} do |r| redirect r; end;
+def get_cache_key(arr)
+ "#{Digest::MD5.hexdigest(arr.join('::'))}_#{Item.cache_key}"
+end
+
# render the row of the table for a given partial
def item_table_row(item)
@fields ||= settings.fields + ['Added On']
View
@@ -16,6 +16,10 @@ class Item
alias :"field#{i}" :"#{field.robotize}"
end
+ def self.cache_key
+ Item.max(:updated_at).strftime("%s")
+ end
+
# Item.fields returns an array of field names
def self.fields
self.properties.collect{ |p| p.name }[1..-1]

0 comments on commit f10d812

Please sign in to comment.