Class: Moneta::Adapters::File

Inherits:
Object
  • Object
show all
Includes:
Config, Defaults
Defined in:
lib/moneta/adapters/file.rb

Overview

Filesystem backend

Instance Method Summary collapse

Methods included from Config

#config, included

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.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :dir (String)

    Directory where files will be stored



18
19
20
21
22
# 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

Instance Method Details

#clear(options = {}) ⇒ void

This method returns an undefined value.

Clear all keys in this store

Parameters:

  • options (Hash) (defaults to: {})


73
74
75
76
77
78
79
80
81
82
# 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 = {}) ⇒ Boolean

Note:

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.

Parameters:

  • key (Object)
  • value (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :expires (Integer)

    Update expiration time (See Expires)

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

Returns:

  • (Boolean)

    key was set



105
106
107
108
109
110
111
112
# 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 = {}) ⇒ Object

Delete the key from the store and return the current value

Parameters:

  • key (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    current value



64
65
66
67
68
69
70
# 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_keyEnumerator #each_key {|key| ... } ⇒ self

Note:

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.

Overloads:

  • #each_keyEnumerator

    Returns An all-the-keys enumerator.

    Returns:

    • (Enumerator)

      An all-the-keys enumerator

  • #each_key {|key| ... } ⇒ self

    Yield Parameters:

    • key (Object)

      Each key is yielded to the supplied block

    Returns:

    • (self)


30
31
32
33
34
35
36
37
38
39
40
41
# 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 = {}) ⇒ Object

Note:

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.

Parameters:

  • key (Object)
  • amount (Integer) (defaults to: 1)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    value from store



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# 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 = {}) ⇒ Boolean

Exists the value with key

Parameters:

  • key (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :expires (Integer)

    Update expiration time (See Expires)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Boolean)


25
26
27
# File 'lib/moneta/adapters/file.rb', line 25

def key?(key, options = {})
  ::File.exist?(store_path(key))
end

#load(key, options = {}) ⇒ Object

Fetch value with key. Return nil if the key doesn’t exist

Parameters:

  • key (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :expires (Integer)

    Update expiration time (See Expires)

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • :sync (Boolean)

    Synchronized load (Cache reloads from adapter, Daybreak syncs with file)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    value



44
45
46
47
48
# 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 = {}) ⇒ Object

Store value with key

Parameters:

  • key (Object)
  • value (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :expires (Integer)

    Set expiration time (See Expires)

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • value



51
52
53
54
55
56
57
58
59
60
61
# 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