Sha256: 43044c7fc0ef2ef48c461ab1541a83d499502dde013db20b4ef98ba88fffa8ef

Contents?: true

Size: 573 Bytes

Versions: 2

Compression:

Stored size: 573 Bytes

Contents

require 'fileutils'
require 'digest/sha2'

module Rhinestone
  class FilesystemCacheBackend
    takes :base_directory

    def get(key)
      File.read(file_name(key)) rescue nil
    end

    def set(key, value)
      file = file_name(key)
      FileUtils.mkdir_p(File.dirname(file))
      File.open(file, "w") do |f|
        f.write value
      end
    end

    private

    def file_name(key)
      sha = Digest::SHA2.hexdigest(key)
      parts = sha.gsub(/.{16}/){|s| s+'|'}.split('|')
      return File.expand_path(File.join(*parts), base_directory)
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rhinestone-0.0.2 lib/rhinestone/filesystem_cache_backend.rb
rhinestone-0.0.1 lib/rhinestone/filesystem_cache_backend.rb