class Cream::Cache

Attributes

duration_seconds[R]
items[R]

Public Class Methods

new(duration_seconds) click to toggle source
# File lib/cream/cache.rb, line 7
def initialize(duration_seconds)
  @duration_seconds = duration_seconds
  @items = {}
end

Public Instance Methods

get(key) click to toggle source
# File lib/cream/cache.rb, line 18
def get(key)
  item = items[key]

  if item&.expired?
    items.delete(key)
    return nil
  end

  item&.value
end
set(key, value) click to toggle source
# File lib/cream/cache.rb, line 12
def set(key, value)
  expires_at = Time.now + duration_seconds
  item = Cream::CacheItem.new(value, expires_at)
  items.merge!(Hash[key, item])
end