Class: VinExploder::Cache::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/vin_exploder/cache.rb

Overview

An abstract cache store class that acts as a null cache as well

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ Store

Create a new cache.



13
14
15
# File 'lib/vin_exploder/cache.rb', line 13

def initialize(options = nil)
  @options = options ? options.dup : {}
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



10
11
12
# File 'lib/vin_exploder/cache.rb', line 10

def connection
  @connection
end

Instance Method Details

#delete(vin) ⇒ Object

Deletes an entry in the cache. Returns true if an entry is deleted.



60
61
62
# File 'lib/vin_exploder/cache.rb', line 60

def delete(vin)
  true
end

#fetch(vin) ⇒ Object

Fetches VIN data from the cache using the given key. If the VIN has been cached, then the VIN attributes are returned.

If the VIN is not in the cache (a cache miss occurred), then nil will be returned. However, if a block has been passed, then that block will be run in the event of a cache miss. The return value of the block will be written to the cache under the given VIN, and that return value will be returned.

cache.write("VIN_NUMBER", {:make => 'Ford', :model => 'F150'})
cache.fetch("VIN_NUMBER")  # => {:make => 'Ford', :model => 'F150'}

cache.fetch("VIN_NUMBER_2")   # => nil
cache.fetch("VIN_NUMBER_2") do
  {:make => 'Dodge', :model => '1500'}
end
cache.fetch("VIN_NUMBER_2")   # => {:make => 'Dodge', :model => '1500'}


35
36
37
38
39
40
41
42
43
44
45
# File 'lib/vin_exploder/cache.rb', line 35

def fetch(vin)
  hash = read(vin)
  if block_given?
    if hash.nil?
      hash = yield
      # adapter should raise exception on error but in case it doesn't don't write to cache
      write(vin, hash) unless hash.empty? || (hash[:errors] && !hash[:errors].empty?)
    end
  end
  hash
end

#read(vin) ⇒ Object

Fetches VIN data from the cache, using the given key. If VIN has been cached with the given key, then the VIN attributes are returned. Otherwise, nil is returned.



50
51
52
# File 'lib/vin_exploder/cache.rb', line 50

def read(vin)
  nil
end

#write(vin, hash) ⇒ Object

Writes the value to the cache, with the key.



55
56
57
# File 'lib/vin_exploder/cache.rb', line 55

def write(vin, hash)
  hash
end