Class: VarnishRailsService

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/varnish_rails/varnish_rails_service.rb

Class Method Summary collapse

Class Method Details

.ban_cache(pattern) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/varnish_rails/varnish_rails_service.rb', line 62

def self.ban_cache(pattern)
  return if !VarnishRails.configuration.enable

  uri                                                       = URI.parse(VarnishRails.configuration.url)
  http_request                                              = Net::HTTPGenericRequest.new("BAN", false, false, uri)
  http_request[VarnishRails.configuration.header_x_ban_url] = pattern
  req_options                                               = { use_ssl: uri.scheme == "https" }

  response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
    http.request(http_request)
  end

  puts "Varnish - ban_cache - #{pattern}".varnish_debug_color if VarnishRails.configuration.print_requests
  puts response.inspect.varnish_debug_color if VarnishRails.configuration.print_requests
end

.purge_cache(url) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/varnish_rails/varnish_rails_service.rb', line 47

def self.purge_cache(url)
  return if !VarnishRails.configuration.enable

  uri           = URI.parse("#{VarnishRails.configuration.url}#{url}")
  http_request  = Net::HTTPGenericRequest.new("PURGE", false, false, uri)
  req_options   = { use_ssl: uri.scheme == "https" }

  response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
    http.request(http_request)
  end

  puts "Varnish - purge_cache - #{url}".varnish_debug_color if VarnishRails.configuration.print_requests
  puts response.inspect.varnish_debug_color if VarnishRails.configuration.print_requests
end

.purge_xkey_cache(*xkeys) ⇒ Object

Class Methods ========================================================



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/varnish_rails/varnish_rails_service.rb', line 25

def self.purge_xkey_cache(*xkeys)
  return if !VarnishRails.configuration.enable

  xkey_purge = ''
  xkeys.each do |xkey|
    xkey_purge = xkey_purge + xkey + ' '
  end
  xkey_purge = xkey_purge.strip

  uri                                                         = URI.parse(VarnishRails.configuration.url)
  http_request                                                = Net::HTTPGenericRequest.new("PURGE", false, false, uri)
  http_request[VarnishRails.configuration.header_xkey_purge]  = xkey_purge
  req_options                                                 = { use_ssl: uri.scheme == "https" }

  response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
    http.request(http_request)
  end

  puts "Varnish - purge_xkey_cache - #{xkey_purge}".varnish_debug_color if VarnishRails.configuration.print_requests
  puts response.inspect.varnish_debug_color if VarnishRails.configuration.print_requests
end