Module: TinyCache::ActiveRecord::Base::InstanceMethods

Defined in:
lib/tiny_cache/active_record/base.rb

Instance Method Summary collapse

Instance Method Details

#expire_tiny_cacheObject



95
96
97
# File 'lib/tiny_cache/active_record/base.rb', line 95

def expire_tiny_cache
  self.class.expire_tiny_cache(self.id)
end

#refresh_tiny_cache_updated_atObject



117
118
119
# File 'lib/tiny_cache/active_record/base.rb', line 117

def refresh_tiny_cache_updated_at
  self.updated_at = Time.now
end

#tiny_cache_keyObject

缓存的key



91
92
93
# File 'lib/tiny_cache/active_record/base.rb', line 91

def tiny_cache_key
  self.class.tiny_cache_key(self.id)
end

#tiny_cache_method_expire(*args) ⇒ Object



107
108
109
110
111
# File 'lib/tiny_cache/active_record/base.rb', line 107

def tiny_cache_method_expire *args
  options = args.extract_options!

  self.class.cache_store.delete self.tiny_cache_method_key(args)
end

#tiny_cache_method_fetch(*args, &block) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/tiny_cache/active_record/base.rb', line 99

def tiny_cache_method_fetch *args, &block
  options = args.extract_options!

  self.class.cache_store.fetch self.tiny_cache_method_key(args), options do
    block.call
  end
end

#tiny_cache_method_key(args) ⇒ Object



113
114
115
# File 'lib/tiny_cache/active_record/base.rb', line 113

def tiny_cache_method_key args
  "#{self.tiny_cache_key}/method_fetch/#{args.join('/')}"
end

#try_load_from_tiny_cacheObject



86
87
88
# File 'lib/tiny_cache/active_record/base.rb', line 86

def try_load_from_tiny_cache
  self.class.read_tiny_cache(self.id) || self
end

#write_tiny_cacheObject Also known as: update_tiny_cache



121
122
123
124
125
126
127
# File 'lib/tiny_cache/active_record/base.rb', line 121

def write_tiny_cache
  if self.class.tiny_cache_enabled?
    ::TinyCache.cache_store.write(
      self.tiny_cache_key, RecordMarshal.dump(self), :expires_in => self.class.tiny_cache_options[:expires_in]
    )
  end
end