Class: Lore::Cache::Memory_Entity_Cache

Inherits:
Abstract_Entity_Cache show all
Extended by:
Cache_Helpers
Defined in:
lib/lore/cache/memory_entity_cache.rb

Overview

Implementation of entity cache using MMapped PStor files. Derived from Abstract_Entity_Cache. Uses Lore::Cache::Cache_Helpers for generating PStor files.

Constant Summary collapse

@@logger =
Logger.new('/tmp/lore_cache.log')
@@store =
ActiveSupport::Cache::MemoryStore.new()

Class Method Summary collapse

Methods included from Cache_Helpers

create_store, index_for, storefile_of

Class Method Details

.create(accessor, query_object, result) ⇒ Object



55
56
57
58
# File 'lib/lore/cache/memory_entity_cache.rb', line 55

def self.create(accessor, query_object, result)
  entry = query_object.update({ :values => result })
  @@store.write("#{accessor.table_name}--#{index_for(query_object[:query])}", entry) 
end

.delete(index) ⇒ Object



67
68
69
70
# File 'lib/lore/cache/memory_entity_cache.rb', line 67

def self.delete(index)
  @@logger.debug { "Deleting index #{index}" }
  @@store.delete(index)
end

.flush(accessor) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/lore/cache/memory_entity_cache.rb', line 33

def self.flush(accessor)
  index = accessor.table_name
  return unless Lore.cache_enabled? 
  Dir.glob("/tmp/lore_cache__#{index}*").each { |cache_file|
    @@logger.debug('Clearing cache file ' << cache_file.to_s.split('_').last)
    begin
      @@store.delete(cache_file.to_s.split('_').last)
    rescue ::Exception => excep
      # Another process already killed this file
    end
  }
end

.include?(accessor, query_obj) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
63
64
65
# File 'lib/lore/cache/memory_entity_cache.rb', line 60

def self.include?(accessor, query_obj)
  hit = @@store.exist?("#{accessor.table_name}--#{index_for(query_obj[:query])}")
  @@logger.debug { 'Cache miss: ' << index_for(query_obj[:query]) } unless hit
  @@logger.info { 'CACHE MISS on ' << query_obj[:query] } unless hit
  return hit
end

.read(accessor, query_obj) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/lore/cache/memory_entity_cache.rb', line 46

def self.read(accessor, query_obj)
  @@logger.debug { 'Loading from cache: ' << index_for(query_obj[:query]) }
  store = @@store.read("#{accessor.table_name}--#{index_for(query_obj[:query])}")
  return [] unless store
  
  return store[:values]
  # result << accessor.new(r, joined_models, :cached)
end