Class: Memoize::PStore
- Inherits:
-
MemoryStore
- Object
- Storable
- MemoryStore
- Memoize::PStore
- 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
-
#store ⇒ Object
{{{.
Instance Method Summary collapse
-
#delete_all ⇒ Object
Delete all memoized data and persisten cache fille.
-
#initialize(name) ⇒ PStore
constructor
A new instance of PStore.
Methods inherited from MemoryStore
Methods inherited from Storable
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
#store ⇒ Object
{{{
78 79 80 |
# File 'lib/memoize.rb', line 78 def store @store end |
Instance Method Details
#delete_all ⇒ Object
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 |