Sha256: 4403d26606424cc151004daa4291037109190c0737435e7522856d2c9b279004

Contents?: true

Size: 1.09 KB

Versions: 17

Compression:

Stored size: 1.09 KB

Contents

require 'contracts'
require 'moneta'

require_relative 'config'

module Cloudstrap
  class Cache
    include ::Contracts::Core
    include ::Contracts::Builtin

    Contract RespondTo[:to_s], Maybe[RespondTo[:call]] => Maybe[Any]
    def get(key)
      persistence.fetch(key.to_s) do
        # TODO: Remove symbol fallback for 1.0
        persistence.fetch(key.to_s.to_sym) do
          yield if block_given?
        end
      end
    end

    Contract RespondTo[:to_s], Any => Any
    def set(key, value)
      persistence.store key.to_s, value
    end

    Contract RespondTo[:to_s] => Maybe[Any]
    def delete(key)
      #TODO: Remove symbol fallback for 1.0
      persistence.delete(key.to_s) || persistence.delete(key.to_s.to_sym)
    end

    Contract RespondTo[:to_s], Maybe[RespondTo[:call]] => Maybe[Any]
    def call(key)
      get(key) { set key, yield if block_given? }
    end

    Contract None => ::Moneta::Proxy
    def persistence
      @persistence ||= ::Moneta.new :File, dir: config.cache_path
    end

    Contract None => Config
    def config
      @config ||= Config.new
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
cloudstrap-0.51.1.pre lib/cloudstrap/cache.rb
cloudstrap-0.50.1.pre lib/cloudstrap/cache.rb
cloudstrap-0.49.12.pre lib/cloudstrap/cache.rb
cloudstrap-0.49.11.pre lib/cloudstrap/cache.rb
cloudstrap-0.49.10.pre lib/cloudstrap/cache.rb
cloudstrap-0.49.8.pre lib/cloudstrap/cache.rb
cloudstrap-0.49.4.pre lib/cloudstrap/cache.rb
cloudstrap-0.49.2.pre lib/cloudstrap/cache.rb
cloudstrap-0.49.1.pre lib/cloudstrap/cache.rb
cloudstrap-0.49.0.pre lib/cloudstrap/cache.rb
cloudstrap-0.48.15.pre lib/cloudstrap/cache.rb
cloudstrap-0.48.2.pre lib/cloudstrap/cache.rb
cloudstrap-0.48.0.pre lib/cloudstrap/cache.rb
cloudstrap-0.47.8.pre lib/cloudstrap/cache.rb
cloudstrap-0.47.7.pre lib/cloudstrap/cache.rb
cloudstrap-0.47.2.pre lib/cloudstrap/cache.rb
cloudstrap-0.47.0.pre lib/cloudstrap/cache.rb