Module: Lore::Cache::Cacheable

Included in:
Model
Defined in:
lib/lore/cache/cacheable.rb

Constant Summary collapse

@@cache_control =
Hash.new
@@cache_on_pkeys =
Hash.new
@@distinctive_params =
Hash.new
@@cache_entities =
false
@@logger =
Lore.logger

Instance Method Summary collapse

Instance Method Details

#cache_params(controller) ⇒ Object



82
83
84
85
# File 'lib/lore/cache/cacheable.rb', line 82

def cache_params(controller)
  return @@distinctive_params[controller] unless @@distinctive_params[controller].nil?
  return Hash.new
end

#create_cache(controller, mode, keys, output) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/lore/cache/cacheable.rb', line 87

def create_cache(controller, mode, keys, output)

  @@logger.log('cache_on_pkeys: '+@@cache_on_pkeys.inspect)
  if !@@cache_on_pkeys[controller.intern].nil? && 
     @@cache_on_pkeys[controller.intern] 
  then
    keys = filter_pkeys(self, controller, keys)
    @@logger.log('Filtered keys: '+keys.inspect)
  end

  if !@@cache_control[controller.intern].nil? && 
     @@cache_control[controller.intern] 
  then
  
    if !@@cache_control[:self].nil? && 
       @@cache_control[:self]
    then 
      class_copy = self
    end

    if is_cached(controller, mode, keys) then 
      return 
    end

    store = create_store(controller, mode, keys, output)
    store.transaction do
      store['klass']      = class_copy unless class_copy.nil?
      store['controller'] = controller
      store['mode']       = mode
      store['keys']       = keys
      store['output']     = output
    end
    
  else 
    raise Cache_Write_Exception.new(self.name.to_s, controller, mode, keys)
  end
  
end

#create_cache!(controller, mode, keys, output) ⇒ Object



128
129
130
131
# File 'lib/lore/cache/cacheable.rb', line 128

def create_cache!(controller, mode, keys, output)
  invalidate(controller, mode, keys)
  create_cache(controller, mode, keys)
end

#invalidate(controller, mode, keys) ⇒ Object



156
157
158
# File 'lib/lore/cache/cacheable.rb', line 156

def invalidate(controller, mode, keys)
  File.unlink(Lore::Cache::store_name(self.name.to_s, controller, mode, keys))
end

#invalidate_all(controller = :all, mode = :all, keys = :all) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/lore/cache/cacheable.rb', line 159

def invalidate_all(controller=:all, mode=:all, keys=:all)
  cwd = Dir.getwd
  Dir.chdir('/tmp/')
  if keys == :all then keys = Hash.new end
  invalid_cache_pattern = Lore::Cache::store_name(self.name.to_s, controller, mode, keys)
  @@logger.log('Deleting cache for: '+invalid_cache_pattern)
  invalid_cache_files = Dir.glob(invalid_cache_pattern)
  invalid_cache_files.each { |cache_file|
    @@logger.log('Deleting '+cache_file)
    File.delete(cache_file)
  }
  Dir.chdir(cwd)
end

#is_cacheable(controller) ⇒ Object



21
22
23
24
# File 'lib/lore/cache/cacheable.rb', line 21

def is_cacheable(controller)
  controller = controller.intern unless controller.instance_of? Symbol
  return !@@cache_control[controller].nil? && @@cache_control[controller]
end

#is_cached(controller, mode, keys) ⇒ Object

Returns whether a Table_Accessor state is cached or not.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/lore/cache/cacheable.rb', line 28

def is_cached(controller, mode, keys)
  
  store_filename = Lore::Cache::store_name(self.name.to_s, controller, mode, keys)
  if store_filename.nil? then
    excep_mesg  = 'Cannot resolve store filename for ['+self.name.to_s+'|'+controller+'|'+mode+']'
    excep_mesg += ' Keys: '+keys.to_s
    raise ::Exception.new(excep_mesg)
  end

  if FileTest.exist?(store_filename) then
    true
  else 
    false
  end

end

#read_cache(controller, mode, keys) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/lore/cache/cacheable.rb', line 135

def read_cache(controller, mode, keys)
  
  if !@@cache_control[controller.intern].nil? && 
     @@cache_control[controller.intern] &&
     is_cached(controller, mode, keys) 
  then
  
    output = ''
    store = create_store(controller, mode, keys, output)
    store.transaction do
      output = store['output']
    end

    return output

  else 
    raise Cache_Read_Exception.new(self.name.to_s, controller, mode, keys)
  end

end