Class: Web::MemoryCache

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

Instance Method Summary collapse

Constructor Details

#initializeMemoryCache

Returns a new instance of MemoryCache.



5
6
7
8
# File 'lib/web/cache/memory_cache.rb', line 5

def initialize
  @expirations = {}
  @hash = {}
end

Instance Method Details

#get(key) ⇒ Object



10
11
12
13
# File 'lib/web/cache/memory_cache.rb', line 10

def get(key)
  expires = @expirations[key]
  @hash[key] if expires.nil? || Time.now < expires
end

#set(key, value, expires = nil) ⇒ Object



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

def set(key, value, expires = nil)
  @hash[key] = value
  @expirations[key] = Time.now + expires if expires
end