I tried looking around but I couldn't find any answers so I wanted to ask: is caching the results of reverse("url_name_here") (or equivalent {% url "url_name_here" %}) safe / won't cause any issues? Here's some example code
from django.urls import reverse
PATH_CACHE = {}
def cached_url(value):
cached_path = PATH_CACHE.get(value, None)
if cached_path is None:
cached_path = reverse(value)
PATH_CACHE[value] = cached_path
return cached_path
With this example code, I'd replace simple reverse() calls (ones without arguments) with cached_url(). Would also like to know the same for static('path/to/file') and {% static 'path/to/file' %}, especially with the ManifestStaticFilesStorage storage backend.
As I intend to run my application with Gunicorn's Gevent workers, it'd be also great to know if this would cause any issues with greenlets / async. While I'm not particularly experienced with async, if it's OK running synchronously it doesn't seem like it'd be an issue with async as if 2 separate greenlet(s? thread-likes? Not exactly sure how to call them) reverse the same URL name and set it on the dictionary, the only thing lost would be some performance as (according to https://stackoverflow.com/a/6955678) Python's dictionaries are thread-safe for single operations.
P.S. I initially posted this as a regular message but realized a bit too late that perhaps the question is best framed as a thread.