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 =

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

Class Method Details

.edge_cache_enabled?Boolean

Returns:

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

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
# File 'app/services/cache/edge_cache_utility.rb', line 46

def purge_by_tags(*tags, environment: Rails.env.to_sym)
  return if tags.blank?

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

  purge_cache(environment, tags: flat_tags)
  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



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

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



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

Parameters:

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

    The environment

Returns:

  • (String)

    The Cloudflare zone ID



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