Sha256: 878040f64ad11ae81aed420221ded00a1b4805160eb447d2928291566132eb09

Contents?: true

Size: 1.47 KB

Versions: 8

Compression:

Stored size: 1.47 KB

Contents

module RailsConnector

  class DictStorage #:nodoc: all

    class << self

      def configure(config)
        @config = config.present? ? config.symbolize_keys : {}
        @storage = nil
      end

      def get(key)
        cache.fetch(key) { storage.get(key) }
      end

      # clears the in-memory cache.
      # does not affect a potential second level cache present in the underlying storage.
      def clear_first_level_cache
        @cache = nil
      end

      attr_reader :config

      def storage
        @storage ||=
            case config[:type]
            when "s3"
              s3_storage(config)
            when "file"
              file_storage(config)
            else
              raise "Unsupported dict storage type #{config[:type]}"
            end
      end

      private

      def cache
        @cache ||= Cache.new
      end

      def s3_storage(config)
        if config && !config.key?("s3_endpoint") && !config.key?(:s3_endpoint)
          config = config.merge(:s3_endpoint => "s3-eu-west-1.amazonaws.com")
        end

        bucket = AWS::S3.new(config).buckets[config[:bucket_name]]
        Kvom::Storage::S3Storage.new({
          :bucket => bucket,
          :bucket_prefix => "dict",
          :cache => s3_storage_cache,
          :cache_prefix => "dict.",
        })
      end

      def s3_storage_cache
        Rails.cache
      end

      def file_storage(config)
        Kvom::Storage::FileSystemStorage.new(config)
      end

    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
infopark_cloud_connector-6.8.0.210.ed204b0 lib/rails_connector/dict_storage.rb
infopark_cloud_connector-6.8.0.110.6570b45 lib/rails_connector/dict_storage.rb
infopark_cloud_connector-6.8.0.72.d18d096 lib/rails_connector/dict_storage.rb
infopark_cloud_connector-6.8.0.23.da7f96b lib/rails_connector/dict_storage.rb
infopark_cloud_connector-6.8.0.16.def5e85 lib/rails_connector/dict_storage.rb
infopark_cloud_connector-6.8.0.15.a24f5ff lib/rails_connector/dict_storage.rb
infopark_cloud_connector-6.8.0.beta.200.891.647580e lib/rails_connector/dict_storage.rb
infopark_cloud_connector-6.8.0.beta.200.889.d503e42 lib/rails_connector/dict_storage.rb