Module: Cache
- Defined in:
- lib/cache.rb
Class Method Summary collapse
- .invalidate(*paths) ⇒ Object
- .invalidate_on_disk(*paths) ⇒ Object
- .invalidate_with_cloudflare(*paths) ⇒ Object
- .invalidate_with_nginx(*paths) ⇒ Object
Class Method Details
.invalidate(*paths) ⇒ Object
16 17 18 19 20 |
# File 'lib/cache.rb', line 16 def invalidate(*paths) invalidate_on_disk(*paths) if $CONFIG.caching #invalidate_with_nginx(*paths) if ENV['DOCKERIZED'] invalidate_with_cloudflare(*paths) if $CONFIG.cloudflare_token end |
.invalidate_on_disk(*paths) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/cache.rb', line 22 def invalidate_on_disk(*paths) files = [] paths.each do |f| f = '/index' if f == '/' if f[-1,1] == '/' files << File.join(STATIC_PATH, f) f = f[0...-1] end files << File.join(STATIC_PATH, f + '.html') end rm_cmd = "rm -rf #{files.join(' ')}" Helpers.sh(rm_cmd, title: "Flushing cache") end |
.invalidate_with_cloudflare(*paths) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/cache.rb', line 53 def invalidate_with_cloudflare(*paths) [$CONFIG.cloudflare_zones].flatten.compact.each do |zone| uri = URI("https://api.cloudflare.com") uri_path = "/client/v4/zones/#{zone}/purge_cache" headers = { "Content-Type" => "application/json", "Authorization" => "Bearer #{$CONFIG.cloudflare_token}" } http = Net::HTTP::Persistent.new puts "Flushing CloudFlare cache: #{paths}" paths.each_slice(30) do |path_slice| begin req = Net::HTTP::Post.new(uri_path, headers) req.body = { "files" => path_slice }.to_json http.request(uri, req) rescue puts "#{Time.now}: Could not invalidate CF cache for: #{path}" end end http.shutdown end end |
.invalidate_with_nginx(*paths) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/cache.rb', line 37 def invalidate_with_nginx(*paths) uri = URI("https://nginx") http = Net::HTTP::Persistent.new http.verify_mode = OpenSSL::SSL::VERIFY_NONE puts "Flushing Nginx cache on #{uri}: #{paths}" paths.each do |path| begin http.request(uri, Net::HTTP::Get.new(path, 'Cache-Bypass' => 'true')) rescue => e puts "#{Time.now}: Could not invalidate cache on #{uri}#{path}" puts e. end end http.shutdown end |