class Moneta::Adapters::File
Filesystem backend @api public
Public Class Methods
new(options = {})
click to toggle source
@param [Hash] options @option options [String] :dir Directory where files will be stored
# File lib/moneta/adapters/file.rb, line 18 def initialize(options = {}) configure(**options) FileUtils.mkpath(config.dir) raise "#{config.dir} is not a directory" unless ::File.directory?(config.dir) end
Public Instance Methods
clear(options = {})
click to toggle source
(see Proxy#clear
)
# File lib/moneta/adapters/file.rb, line 73 def clear(options = {}) temp_dir = "#{config.dir}-#{$PROCESS_ID}-#{Thread.current.object_id}" ::File.rename(config.dir, temp_dir) FileUtils.mkpath(config.dir) self rescue Errno::ENOENT self ensure FileUtils.rm_rf(temp_dir) end
create(key, value, options = {})
click to toggle source
(see Proxy#create
)
# File lib/moneta/adapters/file.rb, line 105 def create(key, value, options = {}) path = store_path(key) FileUtils.mkpath(::File.dirname(path)) # Call native java.io.File#createNewFile return false unless ::Java::JavaIo::File.new(path).createNewFile ::File.open(path, 'wb+') { |f| f.write(value) } true end
delete(key, options = {})
click to toggle source
(see Proxy#delete
)
# File lib/moneta/adapters/file.rb, line 64 def delete(key, options = {}) value = load(key, options) ::File.unlink(store_path(key)) value rescue Errno::ENOENT nil end
each_key() { |k| ... }
click to toggle source
(see Proxy#each_key
)
# File lib/moneta/adapters/file.rb, line 30 def each_key(&block) entries = ::Dir.entries(config.dir).reject do |k| ::File.directory?(::File.join(config.dir, k)) end if block_given? entries.each { |k| yield(k) } self else enum_for(:each_key) { ::Dir.entries(config.dir).length - 2 } end end
increment(key, amount = 1, options = {})
click to toggle source
(see Proxy#increment
)
# File lib/moneta/adapters/file.rb, line 85 def increment(key, amount = 1, options = {}) path = store_path(key) FileUtils.mkpath(::File.dirname(path)) ::File.open(path, ::File::RDWR | ::File::CREAT) do |f| Thread.pass until f.flock(::File::LOCK_EX) content = f.read amount += Integer(content) unless content.empty? content = amount.to_s f.binmode f.pos = 0 f.write(content) f.truncate(content.bytesize) amount end end
key?(key, options = {})
click to toggle source
(see Proxy#key?
)
# File lib/moneta/adapters/file.rb, line 25 def key?(key, options = {}) ::File.exist?(store_path(key)) end
load(key, options = {})
click to toggle source
(see Proxy#load
)
# File lib/moneta/adapters/file.rb, line 44 def load(key, options = {}) ::File.read(store_path(key), mode: 'rb') rescue Errno::ENOENT nil end
store(key, value, options = {})
click to toggle source
(see Proxy#store
)
# File lib/moneta/adapters/file.rb, line 51 def store(key, value, options = {}) temp_file = ::File.join(config.dir, "value-#{$PROCESS_ID}-#{Thread.current.object_id}") path = store_path(key) FileUtils.mkpath(::File.dirname(path)) ::File.open(temp_file, 'wb') { |f| f.write(value) } ::File.rename(temp_file, path) value rescue File.unlink(temp_file) rescue nil raise end
Protected Instance Methods
store_path(key)
click to toggle source
# File lib/moneta/adapters/file.rb, line 130 def store_path(key) ::File.join(config.dir, key) end