54 lines
1.8 KiB
Python
54 lines
1.8 KiB
Python
"""
|
|
Base model with common fields
|
|
"""
|
|
from sqlalchemy import Column, DateTime, String, event
|
|
from sqlalchemy.sql import func
|
|
from app.database.base import Base
|
|
|
|
|
|
class TimestampMixin:
|
|
"""Mixin for created_at and updated_at timestamps"""
|
|
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
|
updated_at = Column(DateTime(timezone=True), onupdate=func.now())
|
|
|
|
|
|
class BaseModel(Base, TimestampMixin):
|
|
"""Base model class"""
|
|
__abstract__ = True
|
|
|
|
|
|
# Event listeners for adaptive cache integration
|
|
@event.listens_for(BaseModel, 'after_update', propagate=True)
|
|
def record_update(mapper, connection, target):
|
|
"""Record data updates for adaptive cache TTL calculation"""
|
|
try:
|
|
from app.services.adaptive_cache import record_data_update
|
|
table_name = target.__tablename__
|
|
record_data_update(table_name)
|
|
except Exception:
|
|
# Don't fail database operations if cache tracking fails
|
|
pass
|
|
|
|
|
|
@event.listens_for(BaseModel, 'after_insert', propagate=True)
|
|
def record_insert(mapper, connection, target):
|
|
"""Record data inserts for adaptive cache TTL calculation"""
|
|
try:
|
|
from app.services.adaptive_cache import record_data_update
|
|
table_name = target.__tablename__
|
|
record_data_update(table_name)
|
|
except Exception:
|
|
# Don't fail database operations if cache tracking fails
|
|
pass
|
|
|
|
|
|
@event.listens_for(BaseModel, 'after_delete', propagate=True)
|
|
def record_delete(mapper, connection, target):
|
|
"""Record data deletions for adaptive cache TTL calculation"""
|
|
try:
|
|
from app.services.adaptive_cache import record_data_update
|
|
table_name = target.__tablename__
|
|
record_data_update(table_name)
|
|
except Exception:
|
|
# Don't fail database operations if cache tracking fails
|
|
pass |