Improve caching and invalidation for URI templates on the root resource.

Review Request #12746 — Created Nov. 28, 2022 and submitted

Information

Djblets
release-3.x

Reviewers

The root resource attempts to cache the generation of URI templates, to
make access to the resource as speedy as possible. However, there are
two problems, which some installs may encounter depending on their use:

  • The URI template cache can grow unbounded. This is only an issue if
    the root resource is namespaced to, for instance, a team/organization
    name.

  • Template caching/clearing/generation is not thread-safe. The cache is
    shared across all threads, but is written to/cleared with no regard to
    other threads, which could in theory pose problems.

This addresses both issues through a rewrite of the caching. There's now
a maximum of 50 cached entries. This is a LRU cache. When hitting the
limit, the least-recently used item will be removed from the cache.

It also locks on any cache manipulation. This includes building of URI
templates. When looking up an item not found in the cache, the URI
templates will be built within that same lock. This should avoid more
than one thread ever building the URI templates at a time, and should
ensure every other thread benefits from the cached entries.

Subclasses that need to augment the list of URI templates are now
encouraged to override the new build_uri_templates() method instead of
get_uri_templates() in order to take advantage of the caching behavior
(though legacy code and behavior will not break).

Unit tests passed in Djblets and Review Board.

Summary ID
Improve caching and invalidation for URI templates on the root resource.
The root resource attempts to cache the generation of URI templates, to make access to the resource as speedy as possible. However, there are two problems, which some installs may encounter depending on their use: * The URI template cache can grow unbounded. This is only an issue if the root resource is namespaced to, for instance, a team/organization name. * Template caching/clearing/generation is not thread-safe. The cache is shared across all threads, but is written to/cleared with no regard to other threads, which could *in theory* pose problems. This addresses both issues through a rewrite of the caching. There's now a maximum of 50 cached entries. This is a LRU cache. When hitting the limit, the least-recently used item will be removed from the cache. It also locks on any cache manipulation. This includes building of URI templates. When looking up an item not found in the cache, the URI templates will be built within that same lock. This should avoid more than one thread ever building the URI templates at a time, and should ensure every other thread benefits from the cached entries. Subclasses that need to augment the list of URI templates are now encouraged to override the new `build_uri_templates()` method instead of `get_uri_templates()` in order to take advantage of the caching behavior (though legacy code and behavior will not break).
ace51b3e5e333d8d7a25ce71f099bc059af3023f
david
  1. Ship It!
  2. 
      
maubin
  1. Ship It!
  2. 
      
chipx86
Review request changed
Status:
Completed
Change Summary:
Pushed to release-4.x (c7054a4)