Class: VinExploder::Cache::Store
- Inherits:
-
Object
- Object
- VinExploder::Cache::Store
- Defined in:
- lib/vin_exploder/cache.rb
Overview
An abstract cache store class that acts as a null cache as well
Direct Known Subclasses
ActiveRecordCacheStore, CouchrestCacheStore, EMSynchronyCouchDBCacheStore, SequelCacheStore
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
Instance Method Summary collapse
-
#delete(vin) ⇒ Object
Deletes an entry in the cache.
-
#fetch(vin) ⇒ Object
Fetches VIN data from the cache using the given key.
-
#initialize(options = nil) ⇒ Store
constructor
Create a new cache.
-
#read(vin) ⇒ Object
Fetches VIN data from the cache, using the given key.
-
#write(vin, hash) ⇒ Object
Writes the value to the cache, with the key.
Constructor Details
#initialize(options = nil) ⇒ Store
Create a new cache.
13 14 15 |
# File 'lib/vin_exploder/cache.rb', line 13 def initialize( = nil) @options = ? .dup : {} end |
Instance Attribute Details
#connection ⇒ Object (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 |