Class: AppCache::LocalFileCache

Inherits:
Object
  • Object
show all
Defined in:
lib/app_cache/local_file_cache.rb

Instance Method Summary collapse

Constructor Details

#initialize(path = '') ⇒ LocalFileCache

Returns a new instance of LocalFileCache.



3
4
5
6
7
8
9
# File 'lib/app_cache/local_file_cache.rb', line 3

def initialize(path = '')
  if ! path.empty?
    @file_path = path
  else
    @file_path = '/tmp'
  end
end

Instance Method Details

#del(key) ⇒ Object



27
28
29
# File 'lib/app_cache/local_file_cache.rb', line 27

def del key
  File.delete("#{@file_path}/#{key}") if has? key
end

#flushObject



31
32
33
34
35
36
37
# File 'lib/app_cache/local_file_cache.rb', line 31

def flush
  Dir.foreach(@file_path) do |item|
    next if item == '.' || item == '..'
    delete item
  end
  Dir.rmdir(@file_path)
end

#get(key) ⇒ Object



19
20
21
# File 'lib/app_cache/local_file_cache.rb', line 19

def get key
  has?(key) ? File.read("#{@file_path}/#{key}") : nil
end

#has?(key) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/app_cache/local_file_cache.rb', line 23

def has? key
  File.exists?("#{@file_path}/#{key}")
end

#set(key, value) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/app_cache/local_file_cache.rb', line 11

def set(key, value)
  FileUtils.mkdir_p("#{@file_path}")
  #File.write("#{@file_path}/#{key}", value)
  file = File.open("#{@file_path}/#{key}", "w")
  file.write(value)
  file.close
end