class Mustermann::PatternCache
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
Public Class Methods
new(**pattern_options)
click to toggle source
@param [Hash] pattern_options default options used for {#create_pattern}
# File lib/mustermann/pattern_cache.rb, line 23 def initialize(**pattern_options) @cached = Set.new @mutex = Mutex.new @pattern_options = pattern_options end
Public Instance Methods
clear()
click to toggle source
Removes all pattern instances from the cache.
# File lib/mustermann/pattern_cache.rb, line 40 def clear @mutex.synchronize { @cached.clear } end
create_pattern(string, **pattern_options)
click to toggle source
@param (see Mustermann.new) @return (see Mustermann.new) @raise (see Mustermann.new) @see Mustermann.new
# File lib/mustermann/pattern_cache.rb, line 33 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
size()
click to toggle source
@return [Integer] number of currently cached patterns
# File lib/mustermann/pattern_cache.rb, line 45 def size @mutex.synchronize { @cached.size } end