<commit_before>class StaffAnonymousIdentifier < Struct.new(:staff_id, :anonymous_id)

  STRING_PREFIX = 'uq-staff-ref'
  DB_PREFIX = self.name.dasherize

  def to_s
    "#{STRING_PREFIX}:#{anonymous_id}"
  end

  class << self

    def find_by_anonymous_id(anonymous_id)
      find(anonymous_id)
    end

    def find_or_create_by_staff_id(staff_id)
      anonymous_id = generate_anonymous_id(staff_id)
      find(anonymous_id) or create(staff_id, anonymous_id)
    end

    def generate_anonymous_id(staff_id)
      digest = sha1_algo
      digest << staff_id
      digest << ENV['STAFF_ID_SALT']
      digest.hexdigest
    end

    private

    def find(anonymous_id)
      staff_id = redis.get("#{DB_PREFIX}:#{anonymous_id}")
      staff_id and new(staff_id, anonymous_id)
    end

    def create(staff_id, anonymous_id = generate_anonymous_id(staff_id))
      redis.set("#{DB_PREFIX}:#{anonymous_id}", staff_id)
      new(staff_id, anonymous_id)
    end

    def redis
      @@redis ||= case Rails.env
        when 'test'
          MockRedis.new
        else
          Redis.new(:path => "./tmp/redis.sock")
        end
    end

    def sha1_algo
      begin
        require 'openssl'
        algo = OpenSSL::Digest::SHA1.new
      rescue LoadError
        algo = Digest::SHA1.new
      end
    end
  end

end<commit_msg>Improving performance for anonymous identifiers by only loading openssl once.<commit_after>class StaffAnonymousIdentifier < Struct.new(:staff_id, :anonymous_id)

  STRING_PREFIX = 'uq-staff-ref'
  DB_PREFIX = self.name.dasherize

  def to_s
    "#{STRING_PREFIX}:#{anonymous_id}"
  end

  # Class Methods
  class << self

    def find_by_anonymous_id(anonymous_id)
      find(anonymous_id)
    end

    def find_or_create_by_staff_id(staff_id)
      anonymous_id = generate_anonymous_id(staff_id)
      find(anonymous_id) or create(staff_id, anonymous_id)
    end

    def generate_anonymous_id(staff_id)
      digest = sha1_algo.new
      digest << staff_id
      digest << ENV['STAFF_ID_SALT']
      digest.hexdigest
    end

    private

    def find(anonymous_id)
      staff_id = redis.get("#{DB_PREFIX}:#{anonymous_id}")
      staff_id and new(staff_id, anonymous_id)
    end

    def create(staff_id, anonymous_id = generate_anonymous_id(staff_id))
      redis.set("#{DB_PREFIX}:#{anonymous_id}", staff_id)
      new(staff_id, anonymous_id)
    end

    def redis
      @@redis ||= case Rails.env
        when 'test'
          MockRedis.new
        else
          Redis.new(:path => "./tmp/redis.sock")
        end
    end

    def sha1_algo
      @algo ||= begin
        require 'openssl'
        OpenSSL::Digest::SHA1
      rescue LoadError
        Digest::SHA1
      end
    end
  end

end
{'fraction_non_alphanumeric': 0.09508076358296623, 'fraction_numerical': 0.002936857562408223, 'mean_word_length': 2.717598908594816, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4878663', 'n_tokens_mistral': 963, 'n_tokens_neox': 939, 'n_words': 177}