Class: Lore::Cache::Mmap_Entity_Cache

Inherits:
Abstract_Entity_Cache show all
Extended by:
Cache_Helpers
Defined in:
lib/lore/cache/mmap_entity_cache.rb,
lib/lore/cache/mmap_entity_cache_bork.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')

Class Method Summary collapse

Methods included from Cache_Helpers

create_store, index_for, storefile_of

Class Method Details

.create(accessor, query_object, result) ⇒ Object



45
46
47
# File 'lib/lore/cache/mmap_entity_cache.rb', line 45

def self.create(accessor, query_object, result)
  storefile = create_mmap(accessor, query_object, result)
end

.create_mmap(accessor, query_object, result) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/lore/cache/mmap_entity_cache.rb', line 48

def self.create_mmap(accessor, query_object, result)
  storefile = storefile_of(accessor.table_name, query_object[:query])
  store = create_store(storefile)
  store.transaction do
    store['dump'] = result
  end
  @@logger.debug('Creating cache entry for ' << storefile )
  Mmap.new(storefile)
  return storefile
end

.delete(index) ⇒ Object



63
64
# File 'lib/lore/cache/mmap_entity_cache.rb', line 63

def self.delete(index)
end

.flush(accessor) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/lore/cache/mmap_entity_cache.rb', line 23

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)
    begin
      File.unlink(cache_file)
    rescue ::Exception => excep
      # Another process already killed this file
    end
  }
end

.include?(accessor, query_obj) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/lore/cache/mmap_entity_cache.rb', line 59

def self.include?(accessor, query_obj)
  FileTest.exist?(storefile_of(accessor.table_name, query_obj[:query]))
end

.read(accessor, query_obj) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/lore/cache/mmap_entity_cache.rb', line 36

def self.read(accessor, query_obj)
  Lore.log { 'Loading from cache: ' << index_for(query_obj[:query]) }
  store = Mmap.new(storefile_of(accessor.table_name, query_obj[:query]))
  Lore.log { 'STORE: ' << store.inspect }
  return [] unless store
  result = Marshal::load(store)
  return result['dump']
end