Add clear() and size to SearchBackend

This commit is contained in:
jeremystretch 2022-10-19 15:54:36 -04:00
parent 50088ef393
commit e07806930f
2 changed files with 42 additions and 15 deletions

View File

@ -1,7 +1,6 @@
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.core.management.base import BaseCommand, CommandError from django.core.management.base import BaseCommand, CommandError
from extras.models import CachedValue
from extras.registry import registry from extras.registry import registry
from netbox.search.backends import search_backend from netbox.search.backends import search_backend
@ -53,14 +52,11 @@ class Command(BaseCommand):
# Clear all cached values for the specified models # Clear all cached values for the specified models
self.stdout.write('Clearing cached values... ', ending='') self.stdout.write('Clearing cached values... ', ending='')
if model_labels: content_types = [
content_types = [ ContentType.objects.get_for_model(model) for model in indexers.keys()
ContentType.objects.get_for_model(model) for model in indexers.keys() ]
] deleted_count = search_backend.clear(content_types)
del_count, _ = CachedValue.objects.filter(object_type__in=content_types).delete() self.stdout.write(f'{deleted_count} entries deleted.')
else:
del_count, _ = CachedValue.objects.all().delete()
self.stdout.write(f'{del_count} deleted.')
# Index models # Index models
for model, idx in indexers.items(): for model, idx in indexers.items():
@ -68,10 +64,12 @@ class Command(BaseCommand):
model_name = model._meta.model_name model_name = model._meta.model_name
self.stdout.write(f'Reindexing {app_label}.{model_name}... ', ending='') self.stdout.write(f'Reindexing {app_label}.{model_name}... ', ending='')
i = 0 i = 0
for i, instance in enumerate(model.objects.all()): for instance in model.objects.all():
search_backend.caching_handler(model, instance) i += search_backend.cache(instance)
if i: if i:
self.stdout.write(f'{i} created.') self.stdout.write(f'{i} entries cached.')
cache_size = CachedValue.objects.count() msg = f'Completed.'
self.stdout.write(f'Done. Finished with {cache_size} cached values') if total_count := search_backend.size:
msg += f' Total entries: {total_count}'
self.stdout.write(msg)

View File

@ -84,6 +84,21 @@ class SearchBackend:
""" """
raise NotImplementedError raise NotImplementedError
@classmethod
def clear(cls, instance):
"""
Delete *all* cached data.
"""
raise NotImplementedError
@property
def size(self):
"""
Return a total number of cached entries. The meaning of this value will be
backend-dependent.
"""
return None
class CachedValueSearchBackend(SearchBackend): class CachedValueSearchBackend(SearchBackend):
@ -155,7 +170,9 @@ class CachedValueSearchBackend(SearchBackend):
value=field.value value=field.value
) )
) )
CachedValue.objects.bulk_create(cached_values) ret = CachedValue.objects.bulk_create(cached_values)
return len(ret)
@classmethod @classmethod
def remove(cls, instance): def remove(cls, instance):
@ -168,6 +185,18 @@ class CachedValueSearchBackend(SearchBackend):
ct = ContentType.objects.get_for_model(instance) ct = ContentType.objects.get_for_model(instance)
CachedValue.objects.filter(object_type=ct, object_id=instance.pk).delete() CachedValue.objects.filter(object_type=ct, object_id=instance.pk).delete()
@classmethod
def clear(cls, object_types=None):
if object_types:
del_count, _ = CachedValue.objects.filter(object_type__in=object_types).delete()
else:
del_count, _ = CachedValue.objects.all().delete()
return del_count
@property
def size(self):
return CachedValue.objects.count()
def get_backend(): def get_backend():
"""Initializes and returns the configured search backend.""" """Initializes and returns the configured search backend."""