Class: Cachetastic::Adapters::File

Inherits:
Base
  • Object
show all
Defined in:
lib/cachetastic/adapters/file.rb

Instance Attribute Summary

Attributes inherited from Base

#klass

Instance Method Summary collapse

Methods inherited from Base

#debug?, #marshal, #unmarshal, #valid?

Constructor Details

#initialize(klass) ⇒ File

Returns a new instance of File.



5
6
7
8
9
10
11
# File 'lib/cachetastic/adapters/file.rb', line 5

def initialize(klass)
  define_accessor(:storage_path)
  self.storage_path = ::File.join(FileUtils.pwd, 'cachetastic')
  super
  self.marshal_method = :yaml if self.marshal_method == :none
  @_file_paths = {}
end

Instance Method Details

#delete(key) ⇒ Object

set



27
28
29
# File 'lib/cachetastic/adapters/file.rb', line 27

def delete(key)
  FileUtils.rm(file_path(key))
end

#expire_allObject

delete



31
32
33
34
35
# File 'lib/cachetastic/adapters/file.rb', line 31

def expire_all
  @_file_paths = {}
  ::FileUtils.rm_rf(::File.join(self.storage_path, klass.name.underscore))
  return nil
end

#file_path(key) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/cachetastic/adapters/file.rb', line 41

def file_path(key)
  path = @_file_paths[key]
  if path.nil?
    path = ::File.join(self.storage_path, klass.name.underscore, transform_key(key).scan(/(.{1,4})/).flatten, 'cache.data')
    @_file_paths[key] = path
    FileUtils.mkdir_p(::File.dirname(path))
  end
  return path
end

#get(key, &block) ⇒ Object



13
14
15
16
17
18
# File 'lib/cachetastic/adapters/file.rb', line 13

def get(key, &block)
  path = file_path(key)
  val = nil
  val = ::File.read(path) if ::File.exists?(path)
  return val
end

#set(key, value, expiry_time = configatron.cachetastic.defaults.default_expiry) ⇒ Object

get



20
21
22
23
24
25
# File 'lib/cachetastic/adapters/file.rb', line 20

def set(key, value, expiry_time = configatron.cachetastic.defaults.default_expiry)
  so = Cachetastic::Cache::StoreObject.new(key, value, expiry_time.from_now)
  path = file_path(key)
  ::File.open(path, 'w') {|f| f.write marshal(so)}
  value
end

#transform_key(key) ⇒ Object

expire_all



37
38
39
# File 'lib/cachetastic/adapters/file.rb', line 37

def transform_key(key)
  key.to_s.hexdigest
end