Class: Mova::Storage::Memory

Inherits:
Object
  • Object
show all
Defined in:
lib/mova/storage/memory.rb

Overview

Note:

This class was designed to be not thread-safe for the sake of speed. However, if you store translations in static YAML files and do not write to the storage during application runtime, this is not a big deal.

If you need thread-safe in-memory implemenation, use ActiveSupport::Cache::MemoryStore.

Also note that with any in-memory implementation you’ll have a copy of all translations data in each spawned worker. Depending on number of your locales, translation keys and workers, it may take up a lot of memory. This is the fastest storage though.

Thin wrapper around Hash.

Since:

  • 0.1.0

Instance Method Summary collapse

Constructor Details

#initializeMemory

Returns a new instance of Memory.

Since:

  • 0.1.0



20
21
22
# File 'lib/mova/storage/memory.rb', line 20

def initialize
  @storage = {}
end

Instance Method Details

#clearvoid

This method returns an undefined value.

Since:

  • 0.1.0



57
58
59
# File 'lib/mova/storage/memory.rb', line 57

def clear
  @storage.clear
end

#exist?(key) ⇒ Boolean

Since:

  • 0.1.0



52
53
54
# File 'lib/mova/storage/memory.rb', line 52

def exist?(key)
  @storage.key?(key)
end

#read(key) ⇒ String?

Since:

  • 0.1.0



26
27
28
# File 'lib/mova/storage/memory.rb', line 26

def read(key)
  @storage[key]
end

#read_multi(*keys) ⇒ Hash

Examples:

storage.write("foo", "bar")
storage.write("baz", "qux")
storage.read_multi("foo", "baz") #=> {"foo" => "bar", "baz" => "qux"}

Since:

  • 0.1.0



36
37
38
39
40
41
# File 'lib/mova/storage/memory.rb', line 36

def read_multi(*keys)
  keys.each_with_object({}) do |key, memo|
    result = read(key)
    memo[key] = result if result
  end
end

#write(key, value) ⇒ void

This method returns an undefined value.

Since:

  • 0.1.0



46
47
48
# File 'lib/mova/storage/memory.rb', line 46

def write(key, value)
  @storage[key] = value
end