Class: Async::DNS::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/async/dns/cache.rb

Overview

Provides a local in-memory cache for DNS resources.

Defined Under Namespace

Classes: Entry

Instance Method Summary collapse

Constructor Details

#initializeCache

Create a new cache.



25
26
27
# File 'lib/async/dns/cache.rb', line 25

def initialize
	@store = {}
end

Instance Method Details

#fetch(name, resource_classes) ⇒ Object

Fetch a resource from the cache, or if it is not present, yield to the block to fetch it.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/async/dns/cache.rb', line 34

def fetch(name, resource_classes)
	now = Async::Clock.now
	
	resource_classes.map do |resource_class|
		key = [name, resource_class]
		
		if entries = @store[key]
			entries.delete_if do |entry|
				!entry.fresh?(now)
			end
		else
			entries = (@store[key] = [])
		end
		
		if entries.empty?
			yield(name, resource_class)
		end
		
		entries
	end.flatten.map(&:resource)
end

#store(name, resource_class, resource) ⇒ Object

Store a resource in the cache.



61
62
63
64
65
66
# File 'lib/async/dns/cache.rb', line 61

def store(name, resource_class, resource)
	key = [name, resource_class]
	entries = (@store[key] ||= [])
	
	entries << Entry.new(Async::Clock.now, name, resource_class, resource)
end