Class: Moneta::Adapters::File

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

Overview

Filesystem backend

Instance Method Summary collapse

Methods included from Defaults

#[], #[]=, #close, #decrement, #features, #fetch, included, #supports?

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

Raises:

  • (ArgumentError)


13
14
15
16
17
# File 'lib/moneta/adapters/file.rb', line 13

def initialize(options = {})
  raise ArgumentError, 'Option :dir is required' unless @dir = options[: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

Parameters:

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


53
54
55
56
57
58
59
60
61
# File 'lib/moneta/adapters/file.rb', line 53

def clear(options = {})
  temp_dir = "#{@dir}-#{$$}-#{Thread.current.object_id}"
  ::File.rename(@dir, temp_dir)
  FileUtils.mkpath(@dir)
  FileUtils.rm_rf(temp_dir)
  self
rescue Errno::ENOENT
  self
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



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/moneta/adapters/file.rb', line 84

def create(key, value, options = {})
  path = store_path(key)
  FileUtils.mkpath(::File.dirname(path))
  ::File.open(path, ::File::WRONLY | ::File::CREAT | ::File::EXCL) do |file|
    file.binmode
    file.write(value)
  end
  true
rescue Errno::EEXIST
  false
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



45
46
47
48
49
50
# File 'lib/moneta/adapters/file.rb', line 45

def delete(key, options = {})
  value = load(key, options)
  ::File.unlink(store_path(key))
  value
rescue Errno::ENOENT
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



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/moneta/adapters/file.rb', line 64

def increment(key, amount = 1, options = {})
  path = store_path(key)
  ::File.open(path, 'rb+') do |f|
    Thread.pass until f.flock(::File::LOCK_EX)
    value = Utils.to_int(f.read) + amount
    f.truncate(0)
    f.pos = 0
    f.write(value.to_s)
    value
  end
rescue Errno::ENOENT
  if create(key, amount.to_s, options)
    amount
  else
    # Concurrent modification
    retry
  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)


20
21
22
# File 'lib/moneta/adapters/file.rb', line 20

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)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    value



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

def load(key, options = {})
  ::File.read(store_path(key))
rescue Errno::ENOENT
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



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/moneta/adapters/file.rb', line 31

def store(key, value, options = {})
  path = store_path(key)
  temp_file = ::File.join(@dir, "value-#{$$}-#{Thread.current.object_id}")
  FileUtils.mkpath(::File.dirname(path))
  ::File.open(temp_file, 'wb') {|file| file.write(value) }
  ::File.unlink(path) if ::File.exist?(path)
  ::File.rename(temp_file, path)
  value
rescue Errno::ENOENT
  ::File.unlink(temp_file) rescue nil
  value
end