Sha256: 4fb1d897965cda77622f988ce42ee9f629a09cfe086e24a7c55214e844543290

Contents?: true

Size: 828 Bytes

Versions: 1

Compression:

Stored size: 828 Bytes

Contents

require "fileutils"

class Stormy::Caches::FileCache < Stormy::Caches::Base

  def initialize(app)
    super
    @base_path =  app.join("tmp","cache")
    clean_path
  end

  protected

  def build_cache_key(category,key)
    path("#{category}--#{key.gsub("/","--")}.cache")
  end


  def get(category,key)
    filename = build_cache_key(category,key)
    if File.exists?(filename)
      File.read(filename)
    end
  end


  def put(category,key,value)
    filename = build_cache_key(category,key)
    File.open(filename,"wt") do |fp|
      fp.write(value)
    end
    value
  end

  def clean_path
    path_exists = File.directory?(@base_path)
    if path_exists
      FileUtils.rm Dir.glob(path("*.cache"))
    else
      FileUtils.mkpath(@base_path)
    end
  end

  def path(file)
    File.join(@base_path,file)
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
stormy-0.0.1 lib/stormy/caches/file_cache.rb