Class: EdgeCacheWorker

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Job
Defined in:
app/workers/edge_cache_worker.rb

Instance Method Summary collapse

Instance Method Details

#perform(options = {}) ⇒ Object

Purge Cloudflare edge cache using either URLs, tags, or everything

Examples:
EdgeCacheWorker.perform_async('urls' => ['https://example.com/page1', 'https://example.com/page2'])
EdgeCacheWorker.perform_async('tags' => ['post', 'product'])
EdgeCacheWorker.perform_async('purge_everything' => true)
EdgeCacheWorker.perform_async('purge_everything' => true, 'environment' => :staging)

Parameters:

  • options (Hash) (defaults to: {})

    Options hash containing 'urls', 'tags', or 'purge_everything'

Options Hash (options):

  • :urls (Array<String>)

    Array of URLs to purge from cache

  • :tags (Array<String>)

    Array of cache tags to purge from cache

  • :purge_everything (Boolean)

    If true, purges all cached content

  • :environment (Symbol)

    Environment to purge (defaults to current Rails.env)



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/workers/edge_cache_worker.rb', line 21

def perform(options = {})
  options = options.with_indifferent_access
  urls = options[:urls] || []
  tags = options[:tags] || []
  purge_everything = options[:purge_everything]
  environment = options[:environment]&.to_sym

  if purge_everything
    logger.debug(" ** Edge Cache Worker execution starting, purging everything")
    res = Cache::EdgeCacheUtility.instance.purge_everything(environment)
    logger.info ' ** EdgeCacheUtility purge everything completed.'
  elsif urls.present?
    logger.debug(" ** Edge Cache Worker execution starting", url_count: urls&.size)
    res = Cache::EdgeCacheUtility.instance.purge_url(urls)
    logger.info ' ** EdgeCacheUtility URL purge completed.'
  elsif tags.present?
    logger.debug(" ** Edge Cache Worker execution starting", tag_count: tags&.size)
    res = Cache::EdgeCacheUtility.instance.purge_by_tags(tags)
    logger.info ' ** EdgeCacheUtility tag purge completed.'
  else
    logger.error("Edge Cache worker, missing urls, tags, or purge_everything parameter, args were #{options.inspect}")
    return
  end
  res
end