Sha256: e78bcf79f7e9e08e77357d352dfcde18def57d27d267d87a91c5658137dd8217

Contents?: true

Size: 855 Bytes

Versions: 1

Compression:

Stored size: 855 Bytes

Contents

require_relative 'cache'

class FileCache < Cache

  def initialize
    super
  end

  def create_store
    @cache = Hash.new
    if store == nil
      raise 'Store path is missing!'
    end
  end

  def put(key, value)
    @cache[key] = value
    File.open(File.join(store, key.to_s), 'w') do |f|
      f.write(value)
    end
    @scheduler.in expiry_time, :blocking => true do
      invalidate key
    end
  end

  def get(key)
    if @cache[key] == nil
      val = refresh.call(key)
      put(key, val)
      return val
    end
    return File.read(File.join(store, key.to_s))
  end

  def invalidate(key)
    super
    File.delete(File.join(store, key.to_s))
  end

  def invalidateAll
    super
    Dir.foreach(store) { |f|
      File.delete(File.join(store, f)) if f != '.' && f != '..'
    }
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

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