29. class Presentation < ActiveRecord::Base
def self.get_cache(id)
if data = @cache.get(id)
data
else
data = find(id)
@cache.set(id, data)
data
end
end
end
30. class Presentation ActiveRecord::Base
def self.get_cache(id)
@cache.get(id) ||
@cache.set(id, find(id))
end
end
51. class Presentation ActiveRecord::Base
acts_as_cached
after_save :expire_cache
end
52. class Presentation ActiveRecord::Base
def self.get_cache(id)
if data = @cache.get(id)
data
else
data = find(id)
@cache.set(id, data)
data
end
end
end
53. class Presentation ActiveRecord::Base
def self.get_cache(id)
if not (data = @cache.get(id)).nil?
data
else
data = find(id)
@cache.set(id, data)
data
end
end
end
54. class Presentation ActiveRecord::Base
def self.get_cache(id)
if not (data = @cache.get(id)).nil?
data
else
data = find(id) || false
@cache.set(id, data)
data
end
end
end
70. A Ruby object acting as cached
- should be able to retrieve a cached version of itself
- should be able to set itself to the cache
- should pass its cached self into a block when supplied
- should be able to expire its cache
- should be able to reset its cache
- should be able to tell if it is cached
- should be able to set itself to the cache with an arbitrary ttl
Finished in 0.028509 seconds.
28 specifications (53 requirements), 0 failures
71. context quot;Calling #cached_weekly_popularquot; do
specify quot;should call #weekly_popular if not cachedquot; do
Topic.expects(:fetch_cache).returns(nil)
Topic.cached_weekly_popular.should.equal Topic.weekly_popular
end
specify quot;should return if cachedquot; do
Topic.expects(:get_cache).returns(true)
Topic.expects(:weekly_popular).never
Topic.cached_weekly_popular
end
end
74. def self.cache_key_with_date(id)
date = Date.today.to_s.tr(' ', '_')
cache_key_without_date(id) + ':' + date
end
class self
alias_method_chain :cache_key, :date
end
75. class Topic ActiveRecord::Base
def self.date_for_key
Date.today.to_s.tr(' ', '_')
end
def self.cached_weekly_popular
key = 'weekly_popular' + date_for_key
get_cache(key) { weekly_popular }
end
end
82. # pulls from memcache
@user = User.get_cache(1)
# pulls from local cache
@user = User.get_cache(1)
83. class ApplicationController
before_filter :set_cache_override
def set_cache_override
returning true do
ActsAsCached.skip_cache_gets =
!!params[:skip_cache]
end
end
end