Class: Moneta::Adapters::File
- Inherits:
-
Object
- Object
- Moneta::Adapters::File
- 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 Config
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.
19 20 21 22 23 |
# File 'lib/moneta/adapters/file.rb', line 19 def initialize( = {}) configure(**) config.dir.mkpath raise "#{config.dir} is not a directory" unless config.dir.directory? end |
Instance Method Details
#clear(options = {}) ⇒ void
This method returns an undefined value.
Clear all keys in this store
82 83 84 85 86 87 88 89 90 91 |
# File 'lib/moneta/adapters/file.rb', line 82 def clear( = {}) temp_dir = Pathname.new("#{config.dir}-#{Process.pid}-#{Thread.current.object_id}") config.dir.rename(temp_dir.to_path) config.dir.mkpath self rescue Errno::ENOENT self ensure temp_dir.rmtree 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.
111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/moneta/adapters/file.rb', line 111 def create(key, value, = {}) path = store_path(key) path.dirname.mkpath path.open(::File::WRONLY | ::File::CREAT | ::File::EXCL) do |f| f.binmode f.write(value) end true rescue Errno::EEXIST false end |
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/moneta/adapters/file.rb', line 63 def delete(key, = {}) temp_file = config.dir.join("value-#{Process.pid}-#{Thread.current.object_id}") path = store_path(key) path.rename(temp_file.to_path) temp_file.read.tap do temp_file.unlink path.ascend.lazy.drop(1).each do |path| break if (path <=> config.dir) <= 0 path.unlink rescue break end end 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.
31 32 33 34 35 36 37 38 39 |
# File 'lib/moneta/adapters/file.rb', line 31 def each_key(&block) return enum_for(:each_key) unless block_given? config.dir.find do |pathname| yield pathname.relative_path_from(config.dir).to_path unless pathname.directory? end 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.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/moneta/adapters/file.rb', line 94 def increment(key, amount = 1, = {}) path = store_path(key) path.dirname.mkpath path.open(::File::RDWR | ::File::CREAT) do |f| Thread.pass until f.flock(::File::LOCK_EX | ::File::LOCK_NB) 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
26 27 28 |
# File 'lib/moneta/adapters/file.rb', line 26 def key?(key, = {}) store_path(key).file? end |
#load(key, options = {}) ⇒ Object
Fetch value with key. Return nil if the key doesn’t exist
42 43 44 45 46 |
# File 'lib/moneta/adapters/file.rb', line 42 def load(key, = {}) store_path(key).read(mode: 'rb') rescue Errno::ENOENT nil end |
#store(key, value, options = {}) ⇒ Object
Store value with key
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/moneta/adapters/file.rb', line 49 def store(key, value, = {}) temp_file = config.dir.join("value-#{Process.pid}-#{Thread.current.object_id}") path = store_path(key) raise "path is a directory" if path.directory? path.dirname.mkpath temp_file.write(value, mode: 'wb') temp_file.rename(path.to_path) value rescue temp_file.unlink rescue nil raise end |