File: memory.rb

package info (click to toggle)
ruby-httpx 1.7.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,816 kB
  • sloc: ruby: 12,209; makefile: 4
file content (42 lines) | stat: -rw-r--r-- 958 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# frozen_string_literal: true

module HTTPX
  module Resolver::Cache
    # Implementation of a thread-safe in-memory LRU resolver cache.
    class Memory < Base
      def initialize
        super
        @hostnames = []
        @lookups = Hash.new { |h, k| h[k] = [] }
        @lookup_mutex = Thread::Mutex.new
      end

      def get(hostname)
        now = Utils.now
        synchronize do |lookups, hostnames|
          _get(hostname, lookups, hostnames, now)
        end
      end

      def set(hostname, family, entries)
        synchronize do |lookups, hostnames|
          _set(hostname, family, entries, lookups, hostnames)
        end
      end

      def evict(hostname, ip)
        ip = ip.to_s

        synchronize do |lookups, hostnames|
          _evict(hostname, ip, lookups, hostnames)
        end
      end

      private

      def synchronize
        @lookup_mutex.synchronize { yield(@lookups, @hostnames) }
      end
    end
  end
end