]> git.ipfire.org Git - thirdparty/paperless-ngx.git/commitdiff
Invalidate llm suggestion cache on doc save
authorshamoon <4887959+shamoon@users.noreply.github.com>
Mon, 21 Apr 2025 20:04:01 +0000 (13:04 -0700)
committershamoon <4887959+shamoon@users.noreply.github.com>
Wed, 2 Jul 2025 18:01:49 +0000 (11:01 -0700)
src/documents/caching.py
src/documents/signals/handlers.py

index bde21fd9267e3e04593494341362a9e2764ab81d..48ff9ebe7eefd1245049f9e77bdce881aa39645a 100644 (file)
@@ -122,7 +122,7 @@ def get_llm_suggestion_cache(
     doc_key = get_suggestion_cache_key(document_id)
     data: SuggestionCacheData = cache.get(doc_key)
 
-    if data and data.classifier_version == 1000 and data.classifier_hash == backend:
+    if data and data.classifier_hash == backend:
         return data
 
     return None
@@ -152,6 +152,19 @@ def set_llm_suggestions_cache(
     )
 
 
+def invalidate_llm_suggestions_cache(
+    document_id: int,
+) -> None:
+    """
+    Invalidate the LLM suggestions cache for a specific document and backend.
+    """
+    doc_key = get_suggestion_cache_key(document_id)
+    data: SuggestionCacheData = cache.get(doc_key)
+
+    if data:
+        cache.delete(doc_key)
+
+
 def get_metadata_cache_key(document_id: int) -> str:
     """
     Returns the basic key for a document's metadata
index 0c20de95a539c894fde60a5ddb65a1748c6a71dc..dd0f34f9277ae3f0f443996a0dd2f22c96a734f0 100644 (file)
@@ -26,6 +26,7 @@ from guardian.shortcuts import remove_perm
 
 from documents import matching
 from documents.caching import clear_document_caches
+from documents.caching import invalidate_llm_suggestions_cache
 from documents.file_handling import create_source_path_directory
 from documents.file_handling import delete_empty_directories
 from documents.file_handling import generate_unique_filename
@@ -525,6 +526,15 @@ def update_filename_and_move_files(
             )
 
 
+@receiver(models.signals.post_save, sender=Document)
+def update_llm_suggestions_cache(sender, instance, **kwargs):
+    """
+    Invalidate the LLM suggestions cache when a document is saved.
+    """
+    # Invalidate the cache for the document
+    invalidate_llm_suggestions_cache(instance.pk)
+
+
 # should be disabled in /src/documents/management/commands/document_importer.py handle
 @receiver(models.signals.post_save, sender=CustomField)
 def check_paths_and_prune_custom_fields(sender, instance: CustomField, **kwargs):