This commit is contained in:
2026-03-26 15:04:59 +08:00
commit e0af97ac7f
65 changed files with 7366 additions and 0 deletions

0
app/db/__init__.py Normal file
View File

15
app/db/models.py Normal file
View File

@@ -0,0 +1,15 @@
from sqlalchemy import Column, Integer, String, Text, DateTime
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class News(Base):
__tablename__ = 'news'
id = Column(Integer, primary_key=True, autoincrement=True)
title = Column(String(255), nullable=False)
content = Column(Text, nullable=True)
url = Column(String(255), nullable=False, unique=True)
source = Column(String(50), nullable=True)
publish_time = Column(DateTime, nullable=True)
created_at = Column(DateTime, nullable=False)

60
app/db/mysql.py Normal file
View File

@@ -0,0 +1,60 @@
from datetime import datetime
from typing import Optional, List, Dict, Any
from .models import Base, News
# 移除对 SQLAlchemy 的依赖
# from app.core.db import Base
# 定义一个简单的数据类来替代 SQLAlchemy 模型
class News:
"""新闻数据模型"""
def __init__(self,
title: str = "",
content: str = "",
url: str = "",
source: str = "",
publish_time: Optional[datetime] = None):
self.id: Optional[int] = None
self.title = title
self.content = content
self.url = url
self.source = source
self.publish_time = publish_time or datetime.now()
self.created_at = datetime.now()
@classmethod
def from_dict(cls, data: Dict[str, Any]) -> 'News':
"""从字典创建新闻对象"""
news = cls(
title=data.get('title', ''),
content=data.get('content', ''),
url=data.get('url', ''),
source=data.get('source', ''),
publish_time=data.get('publish_time')
)
if 'id' in data:
news.id = data['id']
if 'created_at' in data:
news.created_at = data['created_at']
return news
def to_dict(self) -> Dict[str, Any]:
"""转换为字典"""
return {
'id': self.id,
'title': self.title,
'content': self.content,
'url': self.url,
'source': self.source,
'publish_time': self.publish_time,
'created_at': self.created_at
}
def insert_news(news_list):
"""将新闻列表插入数据库"""
from app.core import db
# 如果传入的是 News 对象列表,转换为字典列表
if news_list and isinstance(news_list[0], News):
news_list = [news.to_dict() for news in news_list]
return db.insert_news(news_list)

45
app/db/redis.py Normal file
View File

@@ -0,0 +1,45 @@
import redis
from redis import Redis
from typing import Optional
from pydantic import BaseModel
from app.core import cache
from app.core.config import get_redis_config
REDIS_CONFIG = {
"host": "localhost",
"port": 6379,
"db": 0,
"decode_responses": False,
"socket_timeout": 5,
"socket_connect_timeout": 5,
"health_check_interval": 30,
}
_redis_pool = None
def get_redis_pool() -> redis.ConnectionPool:
global _redis_pool
if _redis_pool is None:
redis_config = get_redis_config()
_redis_pool = redis.ConnectionPool(
host=redis_config.host,
port=redis_config.port,
db=redis_config.db,
password=redis_config.password,
decode_responses=redis_config.decode_responses,
socket_timeout=redis_config.socket_timeout,
socket_connect_timeout=redis_config.socket_connect_timeout,
health_check_interval=redis_config.health_check_interval
)
return _redis_pool
def get_redis_client() -> Redis:
pool = get_redis_pool()
return redis.Redis(connection_pool=pool)
class CacheNews(BaseModel):
title: str
url: str
score: int
desc: str