function get_document_v7
Retrieves a document by its ID from an in-memory cache or loads it from persistent storage if not cached.
/tf/active/vicechatdev/vice_ai/complex_app.py
578 - 592
moderate
Purpose
This function implements a two-tier document retrieval system with thread-safe access. It first checks an in-memory cache (app_state['documents']) for the requested document. If not found in memory, it attempts to load the document from persistent storage using load_document_from_file(), caches it in memory for future access, and returns it. Returns None if the document is not found in either location. The function uses a lock to ensure thread-safe concurrent access to the document cache.
Source Code
def get_document(doc_id):
"""Get a document by ID"""
with app_state['locks']['documents']:
# Try memory first
document = app_state['documents'].get(doc_id)
if document:
return document
# If not in memory, try loading from file
document = load_document_from_file(doc_id)
if document:
app_state['documents'][doc_id] = document
return document
return None
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
doc_id |
- | - | positional_or_keyword |
Parameter Details
doc_id: A unique identifier for the document to retrieve. Expected to be a string or UUID that uniquely identifies a document in the system. This ID is used to look up the document in the cache and as a parameter to load_document_from_file() if the document needs to be loaded from storage.
Return Value
Returns a document object if found (type depends on the document structure used by the application, likely a dictionary or custom object containing document data). Returns None if the document with the specified doc_id is not found in either the in-memory cache or persistent storage.
Dependencies
threading
Required Imports
from threading import Lock
Usage Example
import threading
from threading import Lock
# Initialize app_state
app_state = {
'locks': {
'documents': Lock()
},
'documents': {}
}
# Define load_document_from_file function (stub)
def load_document_from_file(doc_id):
# Implementation to load from file system or database
return {'id': doc_id, 'content': 'Document content'}
# Use get_document
doc_id = 'doc_12345'
document = get_document(doc_id)
if document:
print(f"Document found: {document}")
else:
print("Document not found")
Best Practices
- Ensure app_state is properly initialized before calling this function
- The lock mechanism prevents race conditions when multiple threads access documents simultaneously
- Documents are cached in memory after first load to improve performance on subsequent accesses
- Always check if the returned value is None before using the document
- Consider implementing cache eviction strategies if memory usage becomes a concern
- Ensure load_document_from_file() handles errors gracefully and returns None for missing documents
- The function assumes load_document_from_file() is thread-safe or handles its own locking
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function get_uploaded_document 69.1% similar
-
function get_uploaded_document_v1 68.4% similar
-
function save_document 62.5% similar
-
function get_user_documents 61.6% similar
-
function load_document_from_file 61.5% similar