Sha256: 0a1dc5e1bb853664a26bb63514d238aacdb7bf5670e9449c3cd1329e312c584f

Contents?: true

Size: 435 Bytes

Versions: 15

Compression:

Stored size: 435 Bytes

Contents

module Ro
  class Cache < ::Map
    def write(key, value)
      invalidate(key)
      set(key => value)
    end

    def read(key, &block)
      if has?(key)
        get(key)
      else
        if block
          value = block.call
          write(key, value)
        else
          nil
        end
      end
    end

    def invalidate(key)
      prefix = Array(key).dup.tap{|array| array.pop}
      set(prefix, {})
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
ro-1.4.6 lib/ro/cache.rb
ro-1.4.4 lib/ro/cache.rb
ro-1.4.3 lib/ro/cache.rb
ro-1.4.2 lib/ro/cache.rb
ro-1.4.1 lib/ro/cache.rb
ro-1.4.0 lib/ro/cache.rb
ro-1.3.8 lib/ro/cache.rb
ro-1.3.7 lib/ro/cache.rb
ro-1.3.6 lib/ro/cache.rb
ro-1.3.5 lib/ro/cache.rb
ro-1.3.4 lib/ro/cache.rb
ro-1.3.3 lib/ro/cache.rb
ro-1.3.2 lib/ro/cache.rb
ro-1.3.1 lib/ro/cache.rb
ro-1.2.0 lib/ro/cache.rb