Sha256: 143641f4c2e6b47b7715bbdc9ae9a5c5b0974b64ee7b90db0309171163706c7a

Contents?: true

Size: 789 Bytes

Versions: 4

Compression:

Stored size: 789 Bytes

Contents

# -*- encoding : utf-8 -*-
module SecondLevelCache
  module ActiveRecord
    module FetchByUniqKey
      def fetch_by_uniq_key(value, uniq_key_name)
        if iid = SecondLevelCache.cache_store.read(cache_uniq_key(value, uniq_key_name))
          self.find_by_id(iid)
        else
          record = self.where(uniq_key_name => value).first
          record.tap{|record| SecondLevelCache.cache_store.write(cache_uniq_key(value, uniq_key_name), record.id)} if record
        end
      end

      def fetch_by_uniq_key!(value, uniq_key_name)
        fetch_by_uniq_key(value, uniq_key_name) || raise(::ActiveRecord::RecordNotFound)
      end

      private

      def cache_uniq_key(value, uniq_key_name)
        "uniq_key_#{self.name}_#{uniq_key_name}_#{value}"
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
second_level_cache-1.6.1 lib/second_level_cache/active_record/fetch_by_uniq_key.rb
second_level_cache-1.6.0 lib/second_level_cache/active_record/fetch_by_uniq_key.rb
second_level_cache-1.5.1 lib/second_level_cache/active_record/fetch_by_uniq_key.rb
second_level_cache-1.5.0 lib/second_level_cache/active_record/fetch_by_uniq_key.rb