Sha256: ea722563c16647301a030966b7529eb0b41628934c31836f6e03a6afe81d7c29

Contents?: true

Size: 688 Bytes

Versions: 5

Compression:

Stored size: 688 Bytes

Contents

module Munin
  module Cache
    def cache(key)
      raise RuntimeError, "Block required." unless block_given?
      data = cache_get(key)
      if data.nil?
        data = yield
        cache_set(key, data)
      end
      data
    end
    
    def flush_cache
      instance_variables.select { |l| l =~ /^@cache_/ }.each do |var|
        remove_instance_variable(var)
      end
      true
    end
    
    protected
    
    def cache_key(key)
      "@cache_#{key.gsub(/[\.-]/,'__')}".to_sym
    end
      
    def cache_get(key)
      instance_variable_get(cache_key(key))
    end
    
    def cache_set(key, value)
      instance_variable_set(cache_key(key), value)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
munin-ruby-0.2.5 lib/munin-ruby/cache.rb
munin-ruby-0.2.4 lib/munin-ruby/cache.rb
munin-ruby-0.2.3 lib/munin-ruby/cache.rb
munin-ruby-0.2.2 lib/munin-ruby/cache.rb
munin-ruby-0.2.1 lib/munin-ruby/cache.rb