Class: Momm::Memcached

Inherits:
Storage show all
Defined in:
lib/momm/memcached.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  connection: "localhost:11211", namespace: "momm", compress: true
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Storage

#update

Constructor Details

#initialize(options = {}) ⇒ Memcached

Returns a new instance of Memcached.



10
11
12
13
14
# File 'lib/momm/memcached.rb', line 10

def initialize(options={})
  _options = DEFAULT_OPTIONS.dup.merge options
  @connection = _options.delete(:connection)
  @options = _options
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



8
9
10
# File 'lib/momm/memcached.rb', line 8

def connection
  @connection
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/momm/memcached.rb', line 8

def options
  @options
end

Instance Method Details

#clientObject



16
17
18
# File 'lib/momm/memcached.rb', line 16

def client
  @client ||= Dalli::Client.new connection, options
end

#get_rate(currency, date = Date.today) ⇒ Object



25
26
27
28
# File 'lib/momm/memcached.rb', line 25

def get_rate(currency, date = Date.today)
  date = Date.parse(date) if date.is_a? String
  client.get("#{date}#{currency}").to_f
end

#set_rate(currency, rate, date = Date.today) ⇒ Object



20
21
22
23
# File 'lib/momm/memcached.rb', line 20

def set_rate(currency, rate, date = Date.today)
  date = Date.parse(date) if date.is_a? String
  client.set "#{date}#{currency}", rate
end