Class: Cache

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

Direct Known Subclasses

FileCache

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCache

Returns a new instance of Cache.



7
8
9
10
# File 'lib/libcache/cache.rb', line 7

def initialize
  ENV['TZ'] = 'UTC'
  @scheduler = Rufus::Scheduler.new
end

Instance Attribute Details

#expiry_timeObject

Returns the value of attribute expiry_time.



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

def expiry_time
  @expiry_time
end

#refreshObject

Returns the value of attribute refresh.



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

def refresh
  @refresh
end

#storeObject

Returns the value of attribute store.



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

def store
  @store
end

Instance Method Details

#create_storeObject



12
13
14
# File 'lib/libcache/cache.rb', line 12

def create_store
  @cache = Hash.new
end

#get(key) ⇒ Object



23
24
25
26
27
28
# File 'lib/libcache/cache.rb', line 23

def get(key)
  if @cache[key] == nil
    return refresh.call key
  end
  return @cache[key]
end

#invalidate(key) ⇒ Object



30
31
32
# File 'lib/libcache/cache.rb', line 30

def invalidate(key)
  @cache.delete key
end

#invalidateAllObject



34
35
36
# File 'lib/libcache/cache.rb', line 34

def invalidateAll
  @cache.clear
end

#put(key, value) ⇒ Object



16
17
18
19
20
21
# File 'lib/libcache/cache.rb', line 16

def put(key, value)
  @cache[key] = value
  @scheduler.in expiry_time, :blocking => true do
    invalidate key
  end
end