Sha256: 3b8ff9e1cb7d47e5fcffe790138abb81dc98d4bf0ec4ace908c5b661a24978f4

Contents?: true

Size: 1.45 KB

Versions: 21

Compression:

Stored size: 1.45 KB

Contents

module RailsConnector

  class DictStorage

    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

21 entries across 21 versions & 1 rubygems

Version Path
infopark_cloud_connector-6.8.3.115.227021242 lib/rails_connector/dict_storage.rb
infopark_cloud_connector-6.8.3.73.75172665 lib/rails_connector/dict_storage.rb
infopark_cloud_connector-6.8.3.2.153416031 lib/rails_connector/dict_storage.rb
infopark_cloud_connector-6.8.3.67.110847540 lib/rails_connector/dict_storage.rb
infopark_cloud_connector-6.8.3.1.23895778 lib/rails_connector/dict_storage.rb
infopark_cloud_connector-6.8.2.36.82613853 lib/rails_connector/dict_storage.rb
infopark_cloud_connector-6.8.2.7.128454143 lib/rails_connector/dict_storage.rb
infopark_cloud_connector-6.8.1.26.236500544 lib/rails_connector/dict_storage.rb
infopark_cloud_connector-6.8.1 lib/rails_connector/dict_storage.rb
infopark_cloud_connector-6.8.0.539.93842523 lib/rails_connector/dict_storage.rb
infopark_cloud_connector-6.8.0.529.114081979 lib/rails_connector/dict_storage.rb
infopark_cloud_connector-6.8.0.518.30999728 lib/rails_connector/dict_storage.rb
infopark_cloud_connector-6.8.0.515.34928522 lib/rails_connector/dict_storage.rb
infopark_cloud_connector-6.8.0.498.46559598 lib/rails_connector/dict_storage.rb
infopark_cloud_connector-6.8.0.480.261594408 lib/rails_connector/dict_storage.rb
infopark_cloud_connector-6.8.0.444.171626367 lib/rails_connector/dict_storage.rb
infopark_cloud_connector-6.8.0.406.131718077 lib/rails_connector/dict_storage.rb
infopark_cloud_connector-6.8.0.356.19698103 lib/rails_connector/dict_storage.rb
infopark_cloud_connector-6.8.0.348.160665197 lib/rails_connector/dict_storage.rb
infopark_cloud_connector-6.8.0.328.4a753fc lib/rails_connector/dict_storage.rb