Sha256: b65f4e47532f22b07da4c364f870fce7eb031f4e36af038a48a21b389157d18c

Contents?: true

Size: 1.35 KB

Versions: 7

Compression:

Stored size: 1.35 KB

Contents

require 'coin'
require 'digest'
require 'singleton'
require 'colored2'

module Sym
  module App
    module Password
      class Cache
        URI             = 'druby://127.0.0.1:24924'
        DEFAULT_TIMEOUT = 300

        include Singleton

        attr_accessor :provider, :enabled, :timeout

        def configure(provider: Coin, enabled: true, timeout: DEFAULT_TIMEOUT)
          Coin.uri = URI if provider == Coin

          self.provider = provider
          self.enabled  = enabled
          self.timeout  = timeout

          self
        end

        TRIES = 2

        def operation
          retries ||= TRIES
          yield if self.enabled
        rescue StandardError => e
          if retries == TRIES && Coin.remote_uri.nil?
            Coin.remote_uri = URI if provider == Coin
            retries         -= 1
            retry
          end
          puts 'WARNING: error reading from DRB server: ' + e.message.red
          nil
        end


        def [] (key)
          cache = self
          operation do
            cache.provider.read(cache.md5(key))
          end
        end

        def []=(key, value)
          cache = self
          operation do
            cache.provider.write(cache.md5(key), value, cache.timeout)
          end
        end

        def md5(string)
          Digest::MD5.base64digest(string)
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
sym-2.1.2 lib/sym/app/password/cache.rb
sym-2.1.1 lib/sym/app/password/cache.rb
sym-2.1.0 lib/sym/app/password/cache.rb
sym-2.0.3 lib/sym/app/password/cache.rb
sym-2.0.2 lib/sym/app/password/cache.rb
sym-2.0.1 lib/sym/app/password/cache.rb
sym-2.0.0 lib/sym/app/password/cache.rb