Skip to content
This repository has been archived by the owner on Jul 24, 2018. It is now read-only.

Commit

Permalink
Add memcachier caching
Browse files Browse the repository at this point in the history
  • Loading branch information
gdiggs committed Jan 22, 2013
1 parent 504c446 commit f10d812
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Gemfile
Expand Up @@ -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'
Expand Down
4 changes: 4 additions & 0 deletions Gemfile.lock
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
16 changes: 15 additions & 1 deletion cataloguais.rb
Expand Up @@ -27,6 +27,8 @@
# initialize the graph urls on startup
set :graph_urls, {}

set :cache, Dalli::Client.new

end

configure :production do
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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']
Expand Down
4 changes: 4 additions & 0 deletions models/item.rb
Expand Up @@ -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]
Expand Down

0 comments on commit f10d812

Please sign in to comment.