Sha256: 4eebf9c48edf3d8b51da1f28934079dfc8eb2fbe885bca7fdc6a406347d59958

Contents?: true

Size: 1.13 KB

Versions: 5

Compression:

Stored size: 1.13 KB

Contents


#
# Author: Waldemar Hummer (hummer@dsg.tuwien.ac.at)
#

require "toaster/db/cache"

module Toaster
  class CachedDB

    def initialize(actual_db, config={})
      @db = actual_db
      @cache_result_lists = config["cache_result_lists"]
    end

    def find(criteria = {})
      if !@cache_result_lists
        return @db.find(criteria)
      end

      cached = Cache.by_obj_props(criteria)
      if cached
        #puts "DEBUG: Found cached object for criteria: #{criteria}"
        @db.fix_db_object(cached)
        return cached
      end

      obj = @db.find(criteria)
      Cache.set(obj)
      Cache.set(obj, [Cache::KEY_QUERIES, criteria.inspect])
      return obj
    end
    def find_one(criteria)
      cached = Cache.by_obj_props(criteria)
      if cached
        cached = [cached] if !cached.kind_of?(Array)
        @db.fix_db_object(cached)
        return cached[0] if cached.size == 1
      end

      obj = @db.find_one(criteria)
      Cache.set(obj)
      Cache.set(obj, [Cache::KEY_QUERIES, criteria.inspect])
      return obj
    end

    def method_missing(meth, *args, &block)
      @db.send(meth, *args, &block)
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
cloud-toaster-1.1.6 lib/toaster/db/cached_db.rb
cloud-toaster-1.1.5 lib/toaster/db/cached_db.rb
cloud-toaster-1.1.4 lib/toaster/db/cached_db.rb
cloud-toaster-1.1.3 lib/toaster/db/cached_db.rb
cloud-toaster-1.1.2 lib/toaster/db/cached_db.rb