|
7 | 7 | import os |
8 | 8 | from datetime import datetime |
9 | 9 | from ai_service import generate_book_note, get_ai_recommendations, get_book_mood_tags_safe, generate_chat_response, llm_service |
10 | | -from models import db, User, Book, ShelfItem, register_user, login_user |
| 10 | +from models import db, User, Book, ShelfItem, BookNote, register_user, login_user |
11 | 11 | from collections import defaultdict, deque |
12 | 12 | from math import ceil |
13 | 13 | from time import time |
@@ -260,7 +260,24 @@ def handle_generate_note(): |
260 | 260 | title = data.get('title', '') |
261 | 261 | author = data.get('author', '') |
262 | 262 |
|
| 263 | + # Check cache |
| 264 | + cached_note = BookNote.query.filter_by(book_title=title, book_author=author).first() |
| 265 | + if cached_note: |
| 266 | + print(f"Cache hit for {title} by {author}") |
| 267 | + return jsonify({"vibe": cached_note.content}) |
| 268 | + |
263 | 269 | vibe = generate_book_note(description, title, author) |
| 270 | + |
| 271 | + # Save to cache |
| 272 | + try: |
| 273 | + if vibe and title and author: # Ensure we have valid data to cache |
| 274 | + new_note = BookNote(book_title=title, book_author=author, content=vibe) |
| 275 | + db.session.add(new_note) |
| 276 | + db.session.commit() |
| 277 | + except Exception as e: |
| 278 | + print(f"Failed to cache note: {e}") |
| 279 | + db.session.rollback() |
| 280 | + |
264 | 281 | return jsonify({"vibe": vibe}) |
265 | 282 |
|
266 | 283 | except Exception as e: |
|
0 commit comments