Class: Xunch::NginxCacheHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/xunch/utils/nginx_cache_helper.rb

Instance Method Summary collapse

Constructor Details

#initialize(domain, port = nil, timeout = nil) ⇒ NginxCacheHelper

Returns a new instance of NginxCacheHelper.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/xunch/utils/nginx_cache_helper.rb', line 6

def initialize(domain,port = nil,timeout = nil)
  if domain == nil || domain.strip.empty?
    raise ArgumentError.new("domain can't be nil or empty string.")
  end
  if port == nil
    port = 80
  else
    port = port.to_i
  end
  @http = Net::HTTP.new(domain, port)
  if timeout != nil
    @http.read_timeout = timeout.to_i
  else
    @http.read_timeout = 30
  end
end

Instance Method Details

#cache(path) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/xunch/utils/nginx_cache_helper.rb', line 37

def cache(path)
  respose = nil
  begin
    respose = @http.get(path)
  rescue SocketError => e
    raise e
  end
  if respose == nil || respose.code != "200"
    return false
  else
    return true
  end
end

#evict(path) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/xunch/utils/nginx_cache_helper.rb', line 23

def evict(path)
  respose = nil
  begin
    respose = @http.get("/purge" + path)
  rescue SocketError => e
    raise e
  end
  if respose == nil || (respose.code != "200" && respose.code != "404")
    return false
  else
    return true
  end
end