Class: Grenache::Cache

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

Instance Method Summary collapse

Constructor Details

#initialize(expiring = 5) ⇒ Cache

Returns a new instance of Cache.



4
5
6
7
# File 'lib/grenache/cache.rb', line 4

def initialize expiring=5
  @cache = {}
  @expiring = expiring
end

Instance Method Details

#has?(key) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
12
13
# File 'lib/grenache/cache.rb', line 9

def has?(key)
  if @cache.keys.include?(key) && @cache[key][:expire] >= Time.now
    return @cache[key][:val]
  end
end

#save(key, val) ⇒ Object



15
16
17
18
# File 'lib/grenache/cache.rb', line 15

def save(key, val)
  @cache[key] = { val: val, expire: Time.now+@expiring }
  val
end