Module: TinyCache::ActiveRecord::FetchByUniqKey
- Defined in:
- lib/tiny_cache/active_record/fetch_by_uniq_key.rb
Instance Method Summary collapse
-
#tiny_cache_find_by(conditions = {}) ⇒ Object
用来根据属性进行查询缓存, 一般适合是唯一不变的条件 eg: 1.
- #tiny_cache_find_by!(conditions = {}) ⇒ Object
Instance Method Details
#tiny_cache_find_by(conditions = {}) ⇒ Object
用来根据属性进行查询缓存, 一般适合是唯一不变的条件 eg:
1. topic.tiny_cache_find_by :uid => 1, :category_id => 2
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/tiny_cache/active_record/fetch_by_uniq_key.rb', line 10 def tiny_cache_find_by conditions={} uniq_cache_key = tiny_cache_uniq_key(conditions) # 根据cache_key获取id if iid = TinyCache.cache_store.read(uniq_cache_key) begin self.find iid ensure ::ActiveRecord::RecordNotFound nil end else record = self.where(conditions).first record.tap do |record| TinyCache.cache_store.write(uniq_cache_key, record.id) end if record end end |
#tiny_cache_find_by!(conditions = {}) ⇒ Object
28 29 30 |
# File 'lib/tiny_cache/active_record/fetch_by_uniq_key.rb', line 28 def tiny_cache_find_by! conditions={} tiny_cache_find_by(conditions) || raise(::ActiveRecord::RecordNotFound) end |