Class: DnsOne::Cache

Inherits:
Object show all
Defined in:
lib/dns_one/cache.rb

Constant Summary collapse

DEFAULT_MAX_SIZE =
100000

Instance Method Summary collapse

Constructor Details

#initialize(max_size = nil) ⇒ Cache

Returns a new instance of Cache.



5
6
7
8
# File 'lib/dns_one/cache.rb', line 5

def initialize max_size = nil
    @max_size = max_size || DEFAULT_MAX_SIZE
    @cache = {}
end

Instance Method Details

#add(k, v) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/dns_one/cache.rb', line 10

def add k, v
    @cache[k] = v
    if @cache.length > @max_size
        @cache.delete @cache.keys.first 
    end
    v
end

#find(k) ⇒ Object



18
19
20
# File 'lib/dns_one/cache.rb', line 18

def find k
    @cache[k]
end

#statObject



22
23
24
# File 'lib/dns_one/cache.rb', line 22

def stat
    "#{@cache.length}/#{@max_size}"
end