Class: Alchemy::Page::SitemapCacheKey

Inherits:
Object
  • Object
show all
Defined in:
app/models/alchemy/page/sitemap_cache_key.rb

Overview

Builds the cache key for the XML sitemap.

The key combines three independent sources of change:

  1. The contents of the Alchemy::Page.sitemap scope, as page count and latest updated_at.
  2. The requested base url (protocol, host and port), because show_alchemy_page_url builds absolute urls from request.base_url and a site can be served under several of them (see Alchemy::Site.find_for_host).
  3. A coarse time bucket of max_age seconds.

The time bucket is what makes scheduled publishing work. Pages enter and leave the sitemap when public_on / public_until pass, without any record being touched, so no content based key can notice that transition. Rotating the key every max_age seconds bounds how long such a page can be missing. It also keeps the ETag from freezing: without it, a revalidating client would receive 304 Not Modified forever.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site:, base_url:, max_age: Alchemy.config.sitemap.max_age) ⇒ SitemapCacheKey

Returns a new instance of SitemapCacheKey.

Parameters:

  • site (Alchemy::Site) —

    The site the sitemap is rendered for.

  • base_url (String) —

    The requested base url (e.g. request.base_url).

  • max_age (Integer) (defaults to: Alchemy.config.sitemap.max_age) —

    Length of the time bucket in seconds.



27
28
29
30
31
# File 'app/models/alchemy/page/sitemap_cache_key.rb', line 27

def initialize(site:, base_url:, max_age: Alchemy.config.sitemap.max_age)
  @site = site
  @base_url = base_url
  @max_age = max_age
end

Instance Attribute Details

#base_url ⇒ Object (readonly)

Returns the value of attribute base_url.



22
23
24
# File 'app/models/alchemy/page/sitemap_cache_key.rb', line 22

def base_url
  @base_url
end

#max_age ⇒ Object (readonly)

Returns the value of attribute max_age.



22
23
24
# File 'app/models/alchemy/page/sitemap_cache_key.rb', line 22

def max_age
  @max_age
end

#site ⇒ Object (readonly)

Returns the value of attribute site.



22
23
24
# File 'app/models/alchemy/page/sitemap_cache_key.rb', line 22

def site
  @site
end

Instance Method Details

#call ⇒ Array<Object>

Returns:

  • (Array<Object>)


34
35
36
# File 'app/models/alchemy/page/sitemap_cache_key.rb', line 34

def call
  [site&.id, base_url, content_version, time_bucket]
end