Sha256: 6097056a2a107bff8c0eaf50c4bbbebc90ec8bbaa34aa49bef67b21b3f51135f

Contents?: true

Size: 1.01 KB

Versions: 33

Compression:

Stored size: 1.01 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(:set) {|v| store.delete(cache_key); super}
      class_def(:save) {store.delete(cache_key); 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

33 entries across 33 versions & 2 rubygems

Version Path
sequel-0.4.0 lib/sequel-core/model/caching.rb
sequel-0.3.0.1 lib/sequel/model/caching.rb
sequel-0.3.0 lib/sequel/model/caching.rb
sequel-0.3.1 lib/sequel/model/caching.rb
sequel-0.3.4.1 lib/sequel/model/caching.rb
sequel-0.4.1.1 lib/sequel/model/caching.rb
sequel-0.3.2 lib/sequel/model/caching.rb
sequel-0.3.3 lib/sequel/model/caching.rb
sequel-0.4.1.3 lib/sequel/model/caching.rb
sequel-0.4.2.1 lib/sequel/model/caching.rb
sequel-0.4.2.2 lib/sequel/model/caching.rb
sequel-0.4.1 lib/sequel/model/caching.rb
sequel-0.4.1.2 lib/sequel/model/caching.rb
sequel-0.4.4.2 lib/sequel/model/caching.rb
sequel-0.4.3 lib/sequel/model/caching.rb
sequel-0.4.4 lib/sequel/model/caching.rb
sequel-0.4.5 lib/sequel/model/caching.rb
sequel-0.4.4.1 lib/sequel/model/caching.rb
sequel-0.4.2 lib/sequel/model/caching.rb
sequel-1.4.0 lib/sequel_model/caching.rb