123456789_123456789_123456789_123456789_123456789_

Class: ActiveSupport::CachingKeyGenerator

Relationships & Source Files
Inherits: Object
Defined in: activesupport/lib/active_support/key_generator.rb

Overview

Caching Key Generator

CachingKeyGenerator is a wrapper around KeyGenerator which allows users to avoid re-executing the key generation process when it's called using the same salt and key_size.

Class Method Summary

Instance Method Summary

Constructor Details

.new(key_generator) ⇒ CachingKeyGenerator

[ GitHub ]

  
# File 'activesupport/lib/active_support/key_generator.rb', line 60

def initialize(key_generator)
  @key_generator = key_generator
  @cache_keys = Concurrent::Map.new
  @ractor_key = nil
end

Instance Method Details

#cache_keys (private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/key_generator.rb', line 79

def cache_keys
  @cache_keys || (Ractor[@ractor_key] ||= Concurrent::Map.new)
end

#freeze

[ GitHub ]

  
# File 'activesupport/lib/active_support/key_generator.rb', line 66

def freeze
  @ractor_key = "_caching_key_generator_#{object_id}".to_sym
  Ractor[@ractor_key] = @cache_keys
  @cache_keys = nil
  super
end

#generate_key(*args)

Returns a derived key suitable for use.

[ GitHub ]

  
# File 'activesupport/lib/active_support/key_generator.rb', line 74

def generate_key(*args)
  cache_keys[args.join("|")] ||= @key_generator.generate_key(*args)
end