Class: Lore::Cache::Memcache_Entity_Cache

Inherits:
Abstract_Entity_Cache show all
Extended by:
Cache_Helpers
Defined in:
lib/lore/cache/memcache_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::MemCacheStore.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



68
69
70
71
# File 'lib/lore/cache/memcache_entity_cache.rb', line 68

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



79
80
81
82
# File 'lib/lore/cache/memcache_entity_cache.rb', line 79

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

.flush(accessor) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/lore/cache/memcache_entity_cache.rb', line 40

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)


73
74
75
76
77
# File 'lib/lore/cache/memcache_entity_cache.rb', line 73

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
  return hit
end

.read(accessor, query_obj) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/lore/cache/memcache_entity_cache.rb', line 53

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 = []
  store.each { |r|
    joined_models   = r[:joined_models]
    joined_models ||= []
    result <<  accessor.new(r[:values].dup, joined_models, true)
  }
  return result
end