Class: Cache::EdgeCacheUtility
- Inherits:
-
Object
- Object
- Cache::EdgeCacheUtility
- Includes:
- Singleton
- Defined in:
- app/services/cache/edge_cache_utility.rb
Overview
Cloudflare Edge Cache management utility
Uses Faraday directly instead of Cloudflair gem for better control and reliability
Constant Summary collapse
- BASE_URL =
URL for base.
'https://api.cloudflare.com/client/v4'- ZONE_MAP =
Mapping of zone.
{ development: '4a387e118ce5ea4917eb8dc724f3e3a9', # warmlyyours.me staging: 'd39acaed475782c4901d4a8e5908c1cb', # warmlyyours.ws production: 'cd991c21281a518afa7a0822dca3d434' # warmlyyours.com }.freeze
Class Method Summary collapse
Instance Method Summary collapse
-
#purge_by_tags(*tags, environment: Rails.env.to_sym) ⇒ Array<String>
Purge cache by Cloudflare cache tags More efficient than URL-based purging for bulk invalidation.
-
#purge_everything(environment = Rails.env.to_sym) ⇒ Hash
Purge all cached content from Cloudflare This is the most comprehensive purge option - removes ALL files from cache.
-
#purge_url(*urls, environment: Rails.env.to_sym) ⇒ Array<String>
Purge cache for specific URLs.
-
#zone_id(environment = Rails.env.to_sym) ⇒ String
Get zone ID for an environment.
Class Method Details
.edge_cache_enabled? ⇒ Boolean
20 21 22 |
# File 'app/services/cache/edge_cache_utility.rb', line 20 def self.edge_cache_enabled? Rails.env.production? || Rails.env.staging? end |
Instance Method Details
#purge_by_tags(*tags, environment: Rails.env.to_sym) ⇒ Array<String>
Purge cache by Cloudflare cache tags
More efficient than URL-based purging for bulk invalidation
56 57 58 59 60 61 62 63 64 |
# File 'app/services/cache/edge_cache_utility.rb', line 56 def (*, environment: Rails.env.to_sym) return if .blank? = .flatten.compact.uniq return if .empty? purge_cache(environment, tags: ) end |
#purge_everything(environment = Rails.env.to_sym) ⇒ Hash
Purge all cached content from Cloudflare
This is the most comprehensive purge option - removes ALL files from cache
70 71 72 73 74 |
# File 'app/services/cache/edge_cache_utility.rb', line 70 def purge_everything(environment = Rails.env.to_sym) env_sym = (environment || Rails.env).to_sym purge_cache(env_sym, purge_everything: true) end |
#purge_url(*urls, environment: Rails.env.to_sym) ⇒ Array<String>
Purge cache for specific URLs
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'app/services/cache/edge_cache_utility.rb', line 28 def purge_url(*urls, environment: Rails.env.to_sym) return if urls.blank? flat_urls = urls.flatten.compact return if flat_urls.empty? # Cloudflare limit is 30 URLs per request. A failing batch must not abandon # the batches behind it: callers order the list by importance (a publication # leads with its own landing pages and PDF, then fans out over hundreds of # product/support pages), so raising on batch 3 of 18 left the URLs that # actually needed refreshing stale until their edge TTL ran out. Purge # everything we can, then raise once so EdgeCacheWorker still retries. failures = [] flat_urls.in_groups_of(30, false).each do |url_batch| purge_cache(environment, files: url_batch) rescue StandardError => e failures << e. end raise "Cloudflare purge failed for #{failures.size} batch(es): #{failures.uniq.join('; ')}" if failures.any? flat_urls end |
#zone_id(environment = Rails.env.to_sym) ⇒ String
Get zone ID for an environment
79 80 81 |
# File 'app/services/cache/edge_cache_utility.rb', line 79 def zone_id(environment = Rails.env.to_sym) ZONE_MAP[environment.to_sym] end |