Sha256: 2f8cd766a4d225206cfb44002f0543c9fbd7029ca124dfe411955ff43fce1aa4

Contents?: true

Size: 598 Bytes

Versions: 1

Compression:

Stored size: 598 Bytes

Contents

require 'rufus-scheduler'

class Cache

  attr_accessor :expiry_time, :refresh, :store

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

  def create_store
    @cache = Hash.new
  end

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

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

  def invalidate(key)
    @cache.delete key
  end

  def invalidateAll
    @cache.clear
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
libcache-0.1.0 lib/libcache/cache.rb