Improve caching and invalidation for URI templates on the root resource.
Review Request #12746 — Created Nov. 28, 2022 and submitted — Latest diff uploaded
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.