Genericize cache for use by other models
CI / build (20.x, 3.12) (push) Has been cancelled
CI / build (20.x, 3.13) (push) Has been cancelled
CI / build (20.x, 3.14) (push) Has been cancelled

This commit is contained in:
Jeremy Stretch
2026-01-26 10:39:10 -05:00
parent 75193ee5fc
commit 77102e9f7f
3 changed files with 10 additions and 9 deletions
+4 -4
View File
@@ -9,7 +9,7 @@ from django.db import connection, models
from django.db.models import Q
from django.utils.translation import gettext as _
from netbox.context import object_types_cache
from netbox.context import query_cache
from netbox.plugins import PluginConfig
from netbox.registry import registry
from utilities.string import title
@@ -72,9 +72,9 @@ class ObjectTypeManager(models.Manager):
from netbox.models.features import get_model_features, model_is_public
# Check the request cache before hitting the database
cache = object_types_cache.get()
cache = query_cache.get()
if cache is not None:
if ot := cache.get((model._meta.model, for_concrete_model)):
if ot := cache['object_types'].get((model._meta.model, for_concrete_model)):
return ot
# TODO: Remove this in NetBox v5.0
@@ -105,7 +105,7 @@ class ObjectTypeManager(models.Manager):
# Populate the request cache to avoid redundant lookups
if cache is not None:
cache[(model._meta.model, for_concrete_model)] = ot
cache['object_types'][(model._meta.model, for_concrete_model)] = ot
return ot
+2 -2
View File
@@ -3,10 +3,10 @@ from contextvars import ContextVar
__all__ = (
'current_request',
'events_queue',
'object_types_cache',
'query_cache',
)
current_request = ContextVar('current_request', default=None)
events_queue = ContextVar('events_queue', default=dict())
object_types_cache = ContextVar('object_types_cache', default=None)
query_cache = ContextVar('query_cache', default=None)
+4 -3
View File
@@ -1,6 +1,7 @@
from collections import defaultdict
from contextlib import contextmanager
from netbox.context import current_request, events_queue, object_types_cache
from netbox.context import current_request, events_queue, query_cache
from netbox.utils import register_request_processor
from extras.events import flush_events
@@ -16,7 +17,7 @@ def event_tracking(request):
"""
current_request.set(request)
events_queue.set({})
object_types_cache.set({})
query_cache.set(defaultdict(dict))
yield
@@ -27,4 +28,4 @@ def event_tracking(request):
# Clear context vars
current_request.set(None)
events_queue.set({})
object_types_cache.set(None)
query_cache.set(None)