Sha256: 99a939059fe5fda5dffa52ae47d497df2c43f3bd140a7eb94304d4ceadf8eaed

Contents?: true

Size: 1.46 KB

Versions: 27

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true
require 'set'
require 'thread'
require 'mustermann'

module Mustermann
  # A simple, persistent cache for creating repositories.
  #
  # @example
  #   require 'mustermann/pattern_cache'
  #   cache = Mustermann::PatternCache.new
  #
  #   # use this instead of Mustermann.new
  #   pattern = cache.create_pattern("/:name", type: :rails)
  #
  # @note
  #   {Mustermann::Pattern.new} (which is used by {Mustermann.new}) will reuse instances that have
  #   not yet been garbage collected. You only need an extra cache if you do not keep a reference to
  #   the patterns around.
  #
  # @api private
  class PatternCache
    # @param [Hash] pattern_options default options used for {#create_pattern}
    def initialize(**pattern_options)
      @cached          = Set.new
      @mutex           = Mutex.new
      @pattern_options = pattern_options
    end

    # @param (see Mustermann.new)
    # @return (see Mustermann.new)
    # @raise (see Mustermann.new)
    # @see Mustermann.new
    def create_pattern(string, **pattern_options)
      pattern = Mustermann.new(string, **pattern_options, **@pattern_options)
      @mutex.synchronize { @cached.add(pattern) } unless @cached.include? pattern
      pattern
    end

    # Removes all pattern instances from the cache.
    def clear
      @mutex.synchronize { @cached.clear }
    end

    # @return [Integer] number of currently cached patterns
    def size
      @mutex.synchronize { @cached.size }
    end
  end
end

Version data entries

27 entries across 24 versions & 5 rubygems

Version Path
mustermann-3.0.3 lib/mustermann/pattern_cache.rb
mustermann-3.0.2 lib/mustermann/pattern_cache.rb
mustermann-3.0.1 lib/mustermann/pattern_cache.rb
rubypitaya-3.12.5 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/mustermann-3.0.0/lib/mustermann/pattern_cache.rb
rubypitaya-3.12.4 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/mustermann-3.0.0/lib/mustermann/pattern_cache.rb
rubypitaya-3.12.3 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/mustermann-3.0.0/lib/mustermann/pattern_cache.rb
rubypitaya-3.12.2 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/mustermann-3.0.0/lib/mustermann/pattern_cache.rb
mustermann-3.0.0 lib/mustermann/pattern_cache.rb
mustermann-2.0.2 lib/mustermann/pattern_cache.rb
mustermann-2.0.1 lib/mustermann/pattern_cache.rb
mustermann-2.0.0 lib/mustermann/pattern_cache.rb
mustermann-1.1.2 lib/mustermann/pattern_cache.rb
devcycle-ruby-server-sdk-1.1.0 examples/sinatra/vendor/bundle/ruby/3.0.0/gems/mustermann-1.1.1/lib/mustermann/pattern_cache.rb
devcycle-ruby-server-sdk-1.1.0 examples/sinatra/vendor/bundle/ruby/3.1.0/gems/mustermann-1.1.1/lib/mustermann/pattern_cache.rb
devcycle-ruby-server-sdk-1.1.0 examples/sinatra/vendor/bundle/ruby/2.6.0/gems/mustermann-1.1.1/lib/mustermann/pattern_cache.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/mustermann-1.0.3/lib/mustermann/pattern_cache.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/mustermann-1.1.1/lib/mustermann/pattern_cache.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/mustermann-1.0.3/lib/mustermann/pattern_cache.rb
mustermann-1.1.1 lib/mustermann/pattern_cache.rb
mustermann-1.1.0 lib/mustermann/pattern_cache.rb