Sha256: 56af18f05b63825a48d6628e5eaf60c7b5273dd5095b9f49ccfbbebc634f87ff

Contents?: true

Size: 1.2 KB

Versions: 70

Compression:

Stored size: 1.2 KB

Contents

module Redcar
  class PersistentCache
    class << self
      attr_accessor :storage_dir
    end
    
    def self.all
      Dir[File.join(storage_dir, "*.cache")].map do |fn|
        PersistentCache.new(File.basename(fn)[/^(.*)\.cache$/, 1])
      end
    end
    
    attr_reader :name
    
    def initialize(name)
      @name = name
    end
    
    def cache
      if result = read_cache_file
        result
      else
        result = yield
        write_cache_file(Marshal.dump(result))
        result
      end
    end
    
    def clear
      FileUtils.rm_f(cache_file_name)
    end
    
    def cache_file_name
      File.expand_path(File.join(PersistentCache.storage_dir, name)) + ".cache"
    end
    
    private
  
    def write_cache_file(contents)
      FileUtils.mkdir_p(PersistentCache.storage_dir)
      File.open(cache_file_name, "w") {|f| f.puts contents }
    end
    
    def read_cache_file
      if File.exist?(cache_file_name)
        begin
          Marshal.load(File.read(cache_file_name))
        rescue => e
          puts "WARNING: couldn't load #{cache_file_name}, despite it existing."
          puts e.class.name + " " + e.message
          puts e.backtrace
        end
      end
    end
  end
end

Version data entries

70 entries across 70 versions & 2 rubygems

Version Path
redcar-0.13 plugins/core/lib/core/persistent_cache.rb
redcar-dev-0.13.5dev plugins/core/lib/core/persistent_cache.rb
redcar-dev-0.13.4dev plugins/core/lib/core/persistent_cache.rb
redcar-dev-0.13.3dev plugins/core/lib/core/persistent_cache.rb
redcar-dev-0.13.2dev plugins/core/lib/core/persistent_cache.rb
redcar-dev-0.13.1dev plugins/core/lib/core/persistent_cache.rb
redcar-0.12.1 plugins/core/lib/core/persistent_cache.rb
redcar-dev-0.13.0dev plugins/core/lib/core/persistent_cache.rb
redcar-0.12 plugins/core/lib/core/persistent_cache.rb
redcar-dev-0.12.27dev plugins/core/lib/core/persistent_cache.rb
redcar-dev-0.12.26dev plugins/core/lib/core/persistent_cache.rb
redcar-dev-0.12.25dev plugins/core/lib/core/persistent_cache.rb
redcar-dev-0.12.24dev plugins/core/lib/core/persistent_cache.rb
redcar-dev-0.12.23dev plugins/core/lib/core/persistent_cache.rb
redcar-dev-0.12.22dev plugins/core/lib/core/persistent_cache.rb
redcar-dev-0.12.21dev plugins/core/lib/core/persistent_cache.rb
redcar-dev-0.12.20dev plugins/core/lib/core/persistent_cache.rb
redcar-dev-0.12.19dev plugins/core/lib/core/persistent_cache.rb
redcar-dev-0.12.18dev plugins/core/lib/core/persistent_cache.rb
redcar-dev-0.12.17dev plugins/core/lib/core/persistent_cache.rb