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
46 47 48 49 50 51 52 53 54 |
# File 'app/services/cache/edge_cache_utility.rb', line 46 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
60 61 62 63 64 |
# File 'app/services/cache/edge_cache_utility.rb', line 60 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 |
# 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 flat_urls.in_groups_of(30, false).each do |url_batch| purge_cache(environment, files: url_batch) end flat_urls end |
#zone_id(environment = Rails.env.to_sym) ⇒ String
Get zone ID for an environment
69 70 71 |
# File 'app/services/cache/edge_cache_utility.rb', line 69 def zone_id(environment = Rails.env.to_sym) ZONE_MAP[environment.to_sym] end |