Class: Toaster::CGISessionCache

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

Constant Summary collapse

KEY_CACHE =
"__cache__"
KEY_CACHE_SIZE =
"__cachesize__"
KEY_QUERY_KEYS =
"__keys__"
DO_CACHE_OBJECTS =
false
DO_CACHE_QUERIES =
true

Constants inherited from Cache

Toaster::Cache::KEY_OBJECTS, Toaster::Cache::KEY_QUERIES

Instance Method Summary collapse

Methods inherited from Cache

by_db_type, by_id, by_key, by_obj_props, clear, flush, get_cache, get_hits, get_misses, set, set_cache

Constructor Details

#initialize(cgi_session) ⇒ CGISessionCache

Returns a new instance of CGISessionCache.



20
21
22
23
24
25
26
# File 'lib/toaster/db/cgi_session_cache.rb', line 20

def initialize(cgi_session)
  @session = cgi_session
  @cache = nil
  @hits = []
  @misses = []
  @dirty = false
end

Instance Method Details

#by_db_type(db_type) ⇒ Object



28
29
30
31
32
# File 'lib/toaster/db/cgi_session_cache.rb', line 28

def by_db_type(db_type)
  load_cache()

  return by_obj_prop("db_type", db_type)
end

#by_id(id) ⇒ Object



34
35
36
37
38
# File 'lib/toaster/db/cgi_session_cache.rb', line 34

def by_id(id)
  load_cache()

  return by_obj_props({"id" => id})
end

#by_key(key_path_array) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/toaster/db/cgi_session_cache.rb', line 80

def by_key(key_path_array)
  load_cache()

  if DO_CACHE_QUERIES
    key_path_str = key_path_array.inspect
    cached = @cache[KEY_QUERIES][key_path_str]
    if cached
      @hits << key_path_array
      return cached
    end
  end

  if DO_CACHE_OBJECTS
    key_path_array = [key_path_array] if !key_path_array.kind_of?(Array)
    obj = @cache
    key_path_array.each do |k|
      obj = obj[k]
      if !obj
        @misses << key_path_array
        return nil
      end
    end
    @hits << key_path_array
    #@cache[KEY_QUERIES][key_path_str] = obj
    return obj
  end

  @misses << key_path_array
  return nil
end

#by_obj_props(props_hash) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/toaster/db/cgi_session_cache.rb', line 40

def by_obj_props(props_hash)
  load_cache()

  if DO_CACHE_QUERIES
    props_str = props_hash.inspect
    cached = @cache[KEY_QUERIES][props_str]
    if cached
      @hits << props_hash
      return cached
    end
  end

  if DO_CACHE_OBJECTS
    @cache[KEY_OBJECTS] = [] if !@cache[KEY_OBJECTS]
    puts "size: #{@cache[KEY_OBJECTS].size}"
    objs = @cache[KEY_OBJECTS]
    result = []
    objs.each do |o|
      arr = o.kind_of?(Array) ? o : [o]
      all_match = true
      arr.each do |item|
        if !item || !matches(item, props_hash)
          all_match = false
          break
        end
      end
      result << o if all_match
    end
    if result.size == 0
      @misses << props_hash
      return nil 
    end
    @hits << props_hash
    return result
  end

  @misses << props_hash
  return nil
end

#clearObject



138
139
140
141
142
143
144
145
146
147
# File 'lib/toaster/db/cgi_session_cache.rb', line 138

def clear()
  @cache = nil
  @session[KEY_CACHE] = "{}"
  begin
    @session.update()
  rescue
    # fix for ruby on rails
  end
  load_cache()
end

#flushObject



157
158
159
160
161
# File 'lib/toaster/db/cgi_session_cache.rb', line 157

def flush()
  return if !@dirty
  @session[KEY_CACHE] = MarkupUtil.to_json(@cache)
  @session.update()
end

#get_hitsObject



149
150
151
# File 'lib/toaster/db/cgi_session_cache.rb', line 149

def get_hits()
  @hits
end

#get_missesObject



153
154
155
# File 'lib/toaster/db/cgi_session_cache.rb', line 153

def get_misses()
  @misses
end

#set(value, key = KEY_OBJECTS) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/toaster/db/cgi_session_cache.rb', line 111

def set(value, key=KEY_OBJECTS)
  load_cache()

  if key.kind_of?(Array) && key[0] == KEY_QUERIES
    #puts "setting cache value: #{value}"
    @cache[KEY_QUERIES][key[1]] = value
    @dirty = true
    return
  end

  if DO_CACHE_OBJECTS
    i = next_index(key)
    obj_key = "#{KEY_CACHE}.#{key}.#{i}"
    #key = "#{KEY_CACHE}.#{key}"
    #puts "setting '#{obj_key}'"
    value = value[0] if value.kind_of?(Array) && value.size == 1
    @session[obj_key] = value

    @cache[key] = [] if !@cache[key]
    value = [value] if !value.kind_of?(Array)
    @cache[key].concat(value)
    #@cache[key] = @cache[key][0] if @cache[key].size == 1
    @dirty = true
  end

end