Class: Toaster::CachedDB

Inherits:
Object
  • Object
show all
Defined in:
lib/toaster/db/cached_db.rb

Instance Method Summary collapse

Constructor Details

#initialize(actual_db, config = {}) ⇒ CachedDB

Returns a new instance of CachedDB.



12
13
14
15
# File 'lib/toaster/db/cached_db.rb', line 12

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



48
49
50
# File 'lib/toaster/db/cached_db.rb', line 48

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

Instance Method Details

#find(criteria = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/toaster/db/cached_db.rb', line 17

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

#find_one(criteria) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/toaster/db/cached_db.rb', line 34

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