-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add caching + don't push :latest tag on feature branches
- Loading branch information
Showing
15 changed files
with
473 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
CLOUDFLARE_API_TOKEN=cloudflare-token | ||
CLOUDFLARE_ZONE_ID=zone-id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
--require rails_helper --color |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
module Cloudflare | ||
BASE_URL = "https://api.cloudflare.com/client/v4".freeze | ||
|
||
# https://developers.cloudflare.com/api/operations/zone-purge#purge-cached-content-by-tag-host-or-prefix | ||
# | ||
# Rate-limiting: Cache-Tag, host and prefix purging each have a rate limit | ||
# of 30,000 purge API calls in every 24 hour period. You may purge up to | ||
# 30 tags, hosts, or prefixes in one API call. This rate limit can be | ||
# raised for customers who need to purge at higher volume. | ||
# | ||
# Provide tags as an Array of Strings, eg: ["mnd-assets-id-xxx", ...] or a single String | ||
def self.purge_by_tags(tags, zone_id: ENV.fetch("CLOUDFLARE_ZONE_ID")) | ||
tags = Array.wrap(tags) | ||
|
||
post("zones/#{zone_id}/purge_cache", tags:) | ||
end | ||
|
||
# https://developers.cloudflare.com/api/operations/zone-purge#purge-cached-content-by-url | ||
def self.purge_by_urls(urls, zone_id: ENV.fetch("CLOUDFLARE_ZONE_ID")) | ||
urls = Array.wrap(urls) | ||
|
||
post("zones/#{zone_id}/purge_cache", files: urls) | ||
end | ||
|
||
%w[get post delete patch].each do |verb| | ||
define_singleton_method(verb) do |path, params = {}| | ||
request(verb.upcase, path, params) | ||
end | ||
end | ||
|
||
def self.request(verb, path, params) | ||
HTTPX.send( | ||
verb.downcase, | ||
"#{BASE_URL}/#{path}", | ||
headers: { | ||
"Authorization" => "Bearer #{ENV.fetch('CLOUDFLARE_API_TOKEN')}", | ||
"Accept" => "application/json", | ||
}, | ||
json: params, | ||
).raise_for_status | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class CachedUrl < ApplicationRecord | ||
scope :tagged_one_of, -> (tags) { where("tags && ARRAY[?]::varchar[]", tags) } | ||
|
||
def self.expire_by_tags(tags) | ||
transaction do | ||
cached_urls = tagged_one_of(tags) | ||
|
||
now = Time.now | ||
urls_to_purge = cached_urls.map { |cu| cu.url unless cu.expires_at < now }.compact | ||
|
||
Cloudflare.purge_by_urls(urls_to_purge) | ||
|
||
cached_urls.delete_all | ||
end | ||
end | ||
end |
Oops, something went wrong.