Class: Moneta::Adapters::File
- Inherits:
-
Object
- Object
- Moneta::Adapters::File
- Includes:
- Defaults
- Defined in:
- lib/moneta/adapters/file.rb
Overview
Filesystem backend
Instance Method Summary collapse
-
#clear(options = {}) ⇒ void
Clear all keys in this store.
-
#create(key, value, options = {}) ⇒ Boolean
Atomically sets a key to value if it’s not set.
-
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value.
-
#each_key(&block) ⇒ Object
Calls block once for each key in store, passing the key as a parameter.
-
#increment(key, amount = 1, options = {}) ⇒ Object
Atomically increment integer value with key.
-
#initialize(options = {}) ⇒ File
constructor
A new instance of File.
-
#key?(key, options = {}) ⇒ Boolean
Exists the value with key.
-
#load(key, options = {}) ⇒ Object
Fetch value with key.
-
#store(key, value, options = {}) ⇒ Object
Store value with key.
Methods included from Defaults
#[], #[]=, #close, #decrement, #features, #fetch, #fetch_values, included, #merge!, #slice, #supports?, #update, #values_at
Methods included from OptionSupport
#expires, #prefix, #raw, #with
Constructor Details
#initialize(options = {}) ⇒ File
Returns a new instance of File.
14 15 16 17 18 |
# File 'lib/moneta/adapters/file.rb', line 14 def initialize( = {}) raise ArgumentError, 'Option :dir is required' unless @dir = [:dir] FileUtils.mkpath(@dir) raise "#{@dir} is not a directory" unless ::File.directory?(@dir) end |
Instance Method Details
#clear(options = {}) ⇒ void
This method returns an undefined value.
Clear all keys in this store
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/moneta/adapters/file.rb', line 65 def clear( = {}) temp_dir = "#{@dir}-#{$PROCESS_ID}-#{Thread.current.object_id}" ::File.rename(@dir, temp_dir) FileUtils.mkpath(@dir) self rescue Errno::ENOENT self ensure FileUtils.rm_rf(temp_dir) end |
#create(key, value, options = {}) ⇒ Boolean
Not every Moneta store implements this method, a NotImplementedError is raised if it is not supported.
Atomically sets a key to value if it’s not set.
97 98 99 100 101 102 103 104 |
# File 'lib/moneta/adapters/file.rb', line 97 def create(key, value, = {}) 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 = {}) ⇒ Object
Delete the key from the store and return the current value
56 57 58 59 60 61 62 |
# File 'lib/moneta/adapters/file.rb', line 56 def delete(key, = {}) value = load(key, ) ::File.unlink(store_path(key)) value rescue Errno::ENOENT nil end |
#each_key ⇒ Enumerator #each_key {|key| ... } ⇒ self
Not every Moneta store implements this method, a NotImplementedError is raised if it is not supported.
Calls block once for each key in store, passing the key as a parameter. If no block is given, an enumerator is returned instead.
26 27 28 29 30 31 32 33 |
# File 'lib/moneta/adapters/file.rb', line 26 def each_key(&block) entries = ::Dir.entries(@dir).reject { |k| ::File.directory?(::File.join(@dir, k)) } return enum_for(:each_key) { ::Dir.entries(@dir).length - 2 } unless block_given? entries.each { |k| yield(k) } self end |
#increment(key, amount = 1, options = {}) ⇒ Object
Not every Moneta store implements this method, a NotImplementedError is raised if it is not supported.
Atomically increment integer value with key
This method also accepts negative amounts.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/moneta/adapters/file.rb', line 77 def increment(key, amount = 1, = {}) 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 = {}) ⇒ Boolean
Exists the value with key
21 22 23 |
# File 'lib/moneta/adapters/file.rb', line 21 def key?(key, = {}) ::File.exist?(store_path(key)) end |
#load(key, options = {}) ⇒ Object
Fetch value with key. Return nil if the key doesn’t exist
36 37 38 39 40 |
# File 'lib/moneta/adapters/file.rb', line 36 def load(key, = {}) ::File.read(store_path(key), mode: 'rb') rescue Errno::ENOENT nil end |
#store(key, value, options = {}) ⇒ Object
Store value with key
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/moneta/adapters/file.rb', line 43 def store(key, value, = {}) temp_file = ::File.join(@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 |