Sha256: 79252826d1a8637345fc60d763fa516944dc542c4ef729649d1d4c5b405e158f

Contents?: true

Size: 1.03 KB

Versions: 2

Compression:

Stored size: 1.03 KB

Contents

module Sequel
  class Model
    def self.set_cache(store, opts = {})
      @cache_store = store
      if (ttl = opts[:ttl])
        set_cache_ttl(ttl)
      end
      
      meta_def(:[]) do |*args|
        if (args.size == 1) && (Hash === (h = args.first))
          return dataset[h]
        end
        
        unless obj = @cache_store.get(cache_key_from_values(args))
          obj = dataset[primary_key_hash((args.size == 1) ? args.first : args)]
          @cache_store.set(cache_key_from_values(args), obj, cache_ttl)
        end
        obj
      end
      
      class_def(:update_values) {|v| store.delete(cache_key); super}
      class_def(:save) {store.delete(cache_key) unless new?; super}
      class_def(:delete) {store.delete(cache_key); super}
    end
    
    def self.set_cache_ttl(ttl)
      @cache_ttl = ttl
    end
    
    def self.cache_store
      @cache_store
    end
    
    def self.cache_ttl
      @cache_ttl ||= 3600
    end
    
    def self.cache_key_from_values(values)
      "#{self}:#{values.join(',')}"
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sequel-1.5.1 lib/sequel_model/caching.rb
sequel-1.5.0 lib/sequel_model/caching.rb