Sha256: f543360924b1de132c9eb5606051f5a1255774159d7ca8ed759906391bfaee3b

Contents?: true

Size: 1.28 KB

Versions: 7

Compression:

Stored size: 1.28 KB

Contents

require 'digest/sha2'

module InfoparkComponentCache
  module KeyGenerator
    # @author Tomasz Przedmojski <tomasz.przedmojski@infopark.de>
    #
    # Generates a key identifing an Object
    #
    # @param [#cache_key, #to_param] anything an Object that responds to #cache_key.
    #   It is assumed that calls to #cache_key produce consistent and deterministic
    #   results. Futhermore for no two distinct objects should their #cache_key be equal
    # @return [String] string that does not contain file-system insecure characters (\, / etc.)
    def self.generate_key(anything)
      encode_key(Rails.cache.send(:expanded_key, anything))
    end

    # Encodes a provided key yielding a string that only consists of
    # alphanumeric characters of limited length. Underlying implementation
    # uses some kind of hashing algorithm and therefore has the same
    # characteristics: equal inputs yield equal outputs, but different
    # inputs can yield same outputs (although it is very very unlikely)
    # 
    # @param [String] string input string to be encoded
    # @return [String] string that is guaranteed to
    #   consist only of alphanumeric characters,
    #   and not to exceed 100 characters
    def self.encode_key(string) #:nodoc:
      Digest::SHA2.hexdigest(string)
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
infopark_component_cache-3.2.0 lib/infopark_component_cache/key_generator.rb
infopark_component_cache-3.1.1 lib/infopark_component_cache/key_generator.rb
infopark_component_cache-3.1.0 lib/infopark_component_cache/key_generator.rb
infopark_component_cache-3.0.0 lib/infopark_component_cache/key_generator.rb
infopark_component_cache-2.0.0 lib/infopark_component_cache/key_generator.rb
infopark_component_cache-1.1.0 lib/infopark_component_cache/key_generator.rb
infopark_component_cache-1.0.0 lib/infopark_component_cache/key_generator.rb