Module: Ramaze::FileCache

Defined in:
lib/ramaze/contrib/file_cache.rb

Overview

drop-in replacement for Ramaze’s built-in MemoryCache built on the filesystem. # to use with sessions do

Ramaze::Global::cache_alternative[:sessions] = Ramaze::FileCache

to use with everything do

Ramaze::Global::cache = Ramaze::FileCache

Constant Summary collapse

Host =
Socket.gethostname
Pid =
Process.pid
Fu =
FileUtils
Root =
File.join Ramaze::APPDIR, "cache"

Class Method Summary collapse

Class Method Details

.[](key) ⇒ Object



23
24
25
26
27
28
# File 'lib/ramaze/contrib/file_cache.rb', line 23

def self.[] key
  path = File.join Root, key, "data"
  Marshal.load(IO.read(path))
rescue
  nil
end

.[]=(key, value) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ramaze/contrib/file_cache.rb', line 30

def self.[]= key, value
  tmp = File.join Root, key, "data.#{ Host }.#{ Pid }"
  dirname = File.join Root, key
  path = File.join Root, key, "data"
  data = Marshal.dump value
  Fu.rm_rf dirname rescue nil
  Fu.mkdir_p dirname rescue nil
  open(tmp, 'w'){|fd| fd.write data}
  Fu.mv tmp, path
rescue
  nil
end

.clearObject



54
55
56
# File 'lib/ramaze/contrib/file_cache.rb', line 54

def self.clear
  Dir["#{ Root }/*"].each{|entry| Fu.rm_rf entry}
end

.delete(*keys) ⇒ Object



47
48
49
50
51
52
# File 'lib/ramaze/contrib/file_cache.rb', line 47

def self.delete *keys
  keys.map do |key|
    dirname = File.join Root, key
    Fu.rm_rf dirname rescue next
  end
end

.newObject



58
59
60
# File 'lib/ramaze/contrib/file_cache.rb', line 58

def self.new
  self
end

.to_symObject



62
63
64
# File 'lib/ramaze/contrib/file_cache.rb', line 62

def self.to_sym
  name.split(%r/::/).last.to_sym
end

.values_at(*keys) ⇒ Object



43
44
45
# File 'lib/ramaze/contrib/file_cache.rb', line 43

def self.values_at *keys
  keys.map{|key| self[key]}
end