Class: Zm::Support::Cache::FileStore

Inherits:
Store
  • Object
show all
Defined in:
lib/zm/support/cache/file_store.rb

Constant Summary collapse

GITKEEP_FILES =
%w[.gitkeep .keep].freeze

Instance Attribute Summary collapse

Attributes inherited from Store

#logger, #options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Store

#delete, #exist?, #fetch, #read, #write

Constructor Details

#initialize(**options) ⇒ FileStore

Returns a new instance of FileStore.



21
22
23
24
# File 'lib/zm/support/cache/file_store.rb', line 21

def initialize(**options)
  @cache_path = options.delete(:cache_path).to_s
  super(options)
end

Instance Attribute Details

#cache_pathObject (readonly)

Returns the value of attribute cache_path.



17
18
19
# File 'lib/zm/support/cache/file_store.rb', line 17

def cache_path
  @cache_path
end

Class Method Details

.test_required_options(options) ⇒ Object



12
13
14
# File 'lib/zm/support/cache/file_store.rb', line 12

def test_required_options(options)
  options.key?(:cache_path)
end

Instance Method Details

#cleanup(options = nil) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/zm/support/cache/file_store.rb', line 33

def cleanup(options = nil)
  options = merged_options(options)
  search_dir(cache_path) do |fname|
    entry = read_entry(fname, **options)
    delete_entry(fname, **options) if entry&.expired?
  end
end

#clear(_options = nil) ⇒ Object



26
27
28
29
30
31
# File 'lib/zm/support/cache/file_store.rb', line 26

def clear(_options = nil)
  root_dirs = (Dir.children(cache_path) - GITKEEP_FILES)
  FileUtils.rm_r(root_dirs.collect { |f| File.join(cache_path, f) })
rescue Errno::ENOENT, Errno::ENOTEMPTY => e
  @logger&.error "FileStoreError (#{e}): #{e.message}"
end

#inspectObject

:nodoc:



41
42
43
# File 'lib/zm/support/cache/file_store.rb', line 41

def inspect # :nodoc:
  "#<#{self.class.name} cache_path=#{@cache_path}, options=#{@options.inspect}>"
end