Class: Memoize::PStore

Inherits:
MemoryStore show all
Defined in:
lib/memoize.rb

Overview

PStore is class which store memoization data with local file system. This store class be able to use persistent cache, because this class write out cache data on file when process is exit.

Constant Summary collapse

PREFIX_SAVE_FILE_DIR =
"/tmp"
SUFFIX_SAVE_FILE_EXT =
".cache"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from MemoryStore

#delete, #get, #set

Methods inherited from Storable

#delete, #get, #set, #update

Constructor Details

#initialize(name) ⇒ PStore

Returns a new instance of PStore.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/memoize.rb', line 82

def initialize(name)
  @name = name
  filename = encoded_filename(@name)
  begin
    if File.exists?(filename)
      File.open(filename, "rb") do |io|
        @store = Marshal.load(io)
      end
    else
      @store = {}
    end
  rescue
    @store = {}
  end
  # Write out cache 
  at_exit {
    File.open("#{filename}", "wb+") do |io|
      io.flock(File::LOCK_EX)
      Marshal.dump(@store, io)
      io.flock(File::LOCK_UN)
    end
  }
end

Instance Attribute Details

#storeObject

{{{



78
79
80
# File 'lib/memoize.rb', line 78

def store
  @store
end

Instance Method Details

#delete_allObject

Delete all memoized data and persisten cache fille



107
108
109
110
111
# File 'lib/memoize.rb', line 107

def delete_all
  filename = encoded_filename(@name)
  File.exists?(filename) && File.unlink(filename)
  @store = {}
end