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 =
'https://api.cloudflare.com/client/v4'- ZONE_MAP =
{ 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
18 19 20 |
# File 'app/services/cache/edge_cache_utility.rb', line 18 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 55 56 |
# File 'app/services/cache/edge_cache_utility.rb', line 46 def (*, environment: Rails.env.to_sym) return unless .present? = .flatten.compact.uniq return if .empty? Retryable.retryable(tries: 3, sleep: ->(n) { 4**n }, on: Retryable::TIMEOUT_CLASSES) do purge_cache(environment, tags: ) end 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
62 63 64 65 66 67 68 |
# File 'app/services/cache/edge_cache_utility.rb', line 62 def purge_everything(environment = Rails.env.to_sym) env_sym = (environment || Rails.env).to_sym Retryable.retryable(tries: 3, sleep: ->(n) { 4**n }, on: Retryable::TIMEOUT_CLASSES) do purge_cache(env_sym, purge_everything: true) end end |
#purge_url(*urls, environment: Rails.env.to_sym) ⇒ Array<String>
Purge cache for specific URLs
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'app/services/cache/edge_cache_utility.rb', line 26 def purge_url(*urls, environment: Rails.env.to_sym) return unless urls.present? 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| Retryable.retryable(tries: 3, sleep: ->(n) { 4**n }, on: Retryable::TIMEOUT_CLASSES) do purge_cache(environment, files: url_batch) end end flat_urls end |
#zone_id(environment = Rails.env.to_sym) ⇒ String
Get zone ID for an environment
73 74 75 |
# File 'app/services/cache/edge_cache_utility.rb', line 73 def zone_id(environment = Rails.env.to_sym) ZONE_MAP[environment.to_sym] end |