Class: Cache::EdgeCacheUtility

Inherits:
Object
  • Object
show all
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

Class Method Details

.edge_cache_enabled?Boolean

Returns:

  • (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

Parameters:

  • tags (Array<String>)

    Array of cache tags to purge

  • environment (Symbol) (defaults to: Rails.env.to_sym)

    Target environment (defaults to current Rails.env)

Returns:

  • (Array<String>)

    The tags that were purged



46
47
48
49
50
51
52
53
54
55
56
# File 'app/services/cache/edge_cache_utility.rb', line 46

def purge_by_tags(*tags, environment: Rails.env.to_sym)
  return unless tags.present?

  flat_tags = tags.flatten.compact.uniq
  return if flat_tags.empty?

  Retryable.retryable(tries: 3, sleep: ->(n) { 4**n }, on: Retryable::TIMEOUT_CLASSES) do
    purge_cache(environment, tags: flat_tags)
  end
  flat_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

Parameters:

  • environment (Symbol) (defaults to: Rails.env.to_sym)

    The environment to purge (defaults to current Rails.env)

Returns:

  • (Hash)

    The purge result from Cloudflare API



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

Parameters:

  • urls (Array<String>)

    Array of URLs to purge

  • environment (Symbol) (defaults to: Rails.env.to_sym)

    Target environment (defaults to current Rails.env)

Returns:

  • (Array<String>)

    The URLs that were purged



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

Parameters:

  • environment (Symbol) (defaults to: Rails.env.to_sym)

    The environment

Returns:

  • (String)

    The Cloudflare zone ID



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