Sha256: b20a4f15c9735ff3a7bcd668e9bf4d28be9138fe8dc440d82e87d6b9ec594544

Contents?: true

Size: 885 Bytes

Versions: 1

Compression:

Stored size: 885 Bytes

Contents

# -*- encoding : utf-8 -*-
module SecondLevelCache
  module ActiveRecord
    module FetchByUniqKey
      def fetch_by_uniq_key(value, uniq_key_name)
        return self.where(uniq_key_name => value).first unless self.second_level_cache_enabled?
        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

1 entries across 1 versions & 1 rubygems

Version Path
second_level_cache-1.6.2 lib/second_level_cache/active_record/fetch_by_uniq_key.rb