Sha256: 7d0fb8fe7da8b03bf0a0abb28b5c5b6156c00cc503b154cab073172396e9c801

Contents?: true

Size: 1.83 KB

Versions: 6

Compression:

Stored size: 1.83 KB

Contents

# frozen_string_literal: true
require 'sprockets/base'

module Sprockets
  # `CachedEnvironment` is a special cached version of `Environment`.
  #
  # The exception is that all of its file system methods are cached
  # for the instances lifetime. This makes `CachedEnvironment` much faster. This
  # behavior is ideal in production environments where the file system
  # is immutable.
  #
  # `CachedEnvironment` should not be initialized directly. Instead use
  # `Environment#cached`.
  class CachedEnvironment < Base
    def initialize(environment)
      initialize_configuration(environment)

      @cache   = environment.cache
      @stats   = Concurrent::Map.new
      @entries = Concurrent::Map.new
      @uris    = Concurrent::Map.new
      @processor_cache_keys = Concurrent::Map.new
      @resolved_dependencies = Concurrent::Map.new
    end

    # No-op return self as cached environment.
    def cached
      self
    end
    alias_method :index, :cached

    # Internal: Cache Environment#entries
    def entries(path)
      @entries.fetch_or_store(path) { super(path) }
    end

    # Internal: Cache Environment#stat
    def stat(path)
      @stats.fetch_or_store(path) { super(path) }
    end

    # Internal: Cache Environment#load
    def load(uri)
      @uris.fetch_or_store(uri) { super(uri) }
    end

    # Internal: Cache Environment#processor_cache_key
    def processor_cache_key(str)
      @processor_cache_keys.fetch_or_store(str) { super(str) }
    end

    # Internal: Cache Environment#resolve_dependency
    def resolve_dependency(str)
      @resolved_dependencies.fetch_or_store(str) { super(str) }
    end

    private
      # Cache is immutable, any methods that try to change the runtime config
      # should bomb.
      def config=(config)
        raise RuntimeError, "can't modify immutable cached environment"
      end
  end
end

Version data entries

6 entries across 5 versions & 4 rubygems

Version Path
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.1.0/gems/sprockets-4.2.1/lib/sprockets/cached_environment.rb
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.3.0/gems/sprockets-4.2.1/lib/sprockets/cached_environment.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/sprockets-4.2.1/lib/sprockets/cached_environment.rb
tinymce-rails-7.1.2 vendor/bundle/ruby/3.3.0/gems/sprockets-4.2.1/lib/sprockets/cached_environment.rb
sprockets-4.2.1 lib/sprockets/cached_environment.rb
sprockets-4.2.0 lib/sprockets/cached_environment.rb