Class: Slugable::CacheLayer

Inherits:
Object
  • Object
show all
Defined in:
lib/slugable/cache_layer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(storage, model) ⇒ CacheLayer

Returns a new instance of CacheLayer.



5
6
7
8
# File 'lib/slugable/cache_layer.rb', line 5

def initialize(storage, model)
  @storage = storage
  @model = model
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



3
4
5
# File 'lib/slugable/cache_layer.rb', line 3

def model
  @model
end

#storageObject (readonly)

Returns the value of attribute storage.



3
4
5
# File 'lib/slugable/cache_layer.rb', line 3

def storage
  @storage
end

Instance Method Details

#read_slug(slug_column, id) ⇒ Object



10
11
12
# File 'lib/slugable/cache_layer.rb', line 10

def read_slug(slug_column, id)
  storage.fetch(cache_key(slug_column, id)) { model.find(id).public_send(slug_column) }
end

#update(slug_column, id, value) ⇒ Object



14
15
16
# File 'lib/slugable/cache_layer.rb', line 14

def update(slug_column, id, value)
  storage.write(cache_key(slug_column, id), value)
end