增加搜索缓存
This commit is contained in:
@@ -230,6 +230,8 @@ backend/
|
|||||||
|
|
||||||
### 8. API缓存管理
|
### 8. API缓存管理
|
||||||
- 作者相关API请求缓存功能
|
- 作者相关API请求缓存功能
|
||||||
|
- 作品搜索API请求缓存功能
|
||||||
|
- 作品详情API请求缓存功能
|
||||||
- 自动缓存过期清理(默认5分钟)
|
- 自动缓存过期清理(默认5分钟)
|
||||||
- 缓存大小限制管理(默认50MB)
|
- 缓存大小限制管理(默认50MB)
|
||||||
- 缓存统计信息查看
|
- 缓存统计信息查看
|
||||||
|
|||||||
@@ -58,7 +58,7 @@ class CacheConfigManager {
|
|||||||
const configDir = path.dirname(this.configPath);
|
const configDir = path.dirname(this.configPath);
|
||||||
if (!require('fs').existsSync(configDir)) {
|
if (!require('fs').existsSync(configDir)) {
|
||||||
require('fs').mkdirSync(configDir, { recursive: true });
|
require('fs').mkdirSync(configDir, { recursive: true });
|
||||||
console.log('缓存配置目录创建成功:', configDir);
|
// console.log('缓存配置目录创建成功:', configDir);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('创建缓存配置目录失败:', error);
|
console.error('创建缓存配置目录失败:', error);
|
||||||
@@ -72,7 +72,7 @@ class CacheConfigManager {
|
|||||||
try {
|
try {
|
||||||
// 检查配置文件是否存在
|
// 检查配置文件是否存在
|
||||||
await fs.access(this.configPath);
|
await fs.access(this.configPath);
|
||||||
console.log('缓存配置文件已存在');
|
// console.log('缓存配置文件已存在');
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// 配置文件不存在,创建默认配置
|
// 配置文件不存在,创建默认配置
|
||||||
console.log('创建默认缓存配置文件...');
|
console.log('创建默认缓存配置文件...');
|
||||||
@@ -87,7 +87,7 @@ class CacheConfigManager {
|
|||||||
try {
|
try {
|
||||||
const configContent = JSON.stringify(this.defaultConfig, null, 2);
|
const configContent = JSON.stringify(this.defaultConfig, null, 2);
|
||||||
await fs.writeFile(this.configPath, configContent, 'utf8');
|
await fs.writeFile(this.configPath, configContent, 'utf8');
|
||||||
console.log('默认缓存配置文件创建成功:', this.configPath);
|
// console.log('默认缓存配置文件创建成功:', this.configPath);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('创建默认缓存配置文件失败:', error);
|
console.error('创建默认缓存配置文件失败:', error);
|
||||||
throw error;
|
throw error;
|
||||||
|
|||||||
@@ -37,7 +37,13 @@ class ApiCacheService {
|
|||||||
'/v1/user/illusts',
|
'/v1/user/illusts',
|
||||||
'/v1/user/following',
|
'/v1/user/following',
|
||||||
'/v1/user/follower',
|
'/v1/user/follower',
|
||||||
'/v1/search/user'
|
'/v1/search/user',
|
||||||
|
'/v1/search/illust',
|
||||||
|
'/v1/illust/detail',
|
||||||
|
'/v1/illust/recommended',
|
||||||
|
'/v1/illust/ranking',
|
||||||
|
'/v1/user/bookmarks/illust',
|
||||||
|
'/v1/user/bookmarks/novel'
|
||||||
],
|
],
|
||||||
// 缓存键生成策略
|
// 缓存键生成策略
|
||||||
keyStrategy: {
|
keyStrategy: {
|
||||||
@@ -66,7 +72,7 @@ class ApiCacheService {
|
|||||||
// 启动定期清理任务
|
// 启动定期清理任务
|
||||||
this.startCleanupTask();
|
this.startCleanupTask();
|
||||||
|
|
||||||
console.log('API缓存服务初始化完成');
|
// console.log('API缓存服务初始化完成');
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('API缓存服务初始化失败:', error);
|
console.error('API缓存服务初始化失败:', error);
|
||||||
}
|
}
|
||||||
@@ -78,7 +84,7 @@ class ApiCacheService {
|
|||||||
async ensureCacheDir() {
|
async ensureCacheDir() {
|
||||||
try {
|
try {
|
||||||
await fs.mkdir(this.cacheDir, { recursive: true });
|
await fs.mkdir(this.cacheDir, { recursive: true });
|
||||||
console.log('API缓存目录创建成功:', this.cacheDir);
|
// console.log('API缓存目录创建成功:', this.cacheDir);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('创建API缓存目录失败:', error);
|
console.error('创建API缓存目录失败:', error);
|
||||||
}
|
}
|
||||||
@@ -98,9 +104,30 @@ class ApiCacheService {
|
|||||||
// 确保params是对象
|
// 确保params是对象
|
||||||
const safeParams = params || {};
|
const safeParams = params || {};
|
||||||
|
|
||||||
// 如果endpoint已经包含查询参数(包含?),则直接使用endpoint作为键
|
// 如果endpoint已经包含查询参数(包含?),则解析endpoint中的参数
|
||||||
if (endpoint.includes('?')) {
|
if (endpoint.includes('?')) {
|
||||||
keyBase = `${method.toUpperCase()}:${endpoint}`;
|
// 分离endpoint和查询参数
|
||||||
|
const [baseEndpoint, queryString] = endpoint.split('?');
|
||||||
|
keyBase = `${method.toUpperCase()}:${baseEndpoint}`;
|
||||||
|
|
||||||
|
// 解析查询字符串
|
||||||
|
const urlParams = new URLSearchParams(queryString);
|
||||||
|
const endpointParams = {};
|
||||||
|
for (const [key, value] of urlParams) {
|
||||||
|
endpointParams[key] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 合并endpoint中的参数和传入的params
|
||||||
|
const allParams = { ...endpointParams, ...safeParams };
|
||||||
|
|
||||||
|
// 如果有参数,添加到键中
|
||||||
|
if (Object.keys(allParams).length > 0) {
|
||||||
|
const sortedParams = Object.keys(allParams)
|
||||||
|
.sort()
|
||||||
|
.map(key => `${key}=${allParams[key]}`)
|
||||||
|
.join('&');
|
||||||
|
keyBase += `?${sortedParams}`;
|
||||||
|
}
|
||||||
} else if (this.config.keyStrategy.includeQueryParams && Object.keys(safeParams).length > 0) {
|
} else if (this.config.keyStrategy.includeQueryParams && Object.keys(safeParams).length > 0) {
|
||||||
// 如果endpoint不包含查询参数,且params不为空,则添加查询参数
|
// 如果endpoint不包含查询参数,且params不为空,则添加查询参数
|
||||||
const sortedParams = Object.keys(safeParams)
|
const sortedParams = Object.keys(safeParams)
|
||||||
|
|||||||
@@ -335,7 +335,7 @@ class ArtistService {
|
|||||||
try {
|
try {
|
||||||
const cachedData = await this.apiCache.get(method, endpoint, data || {});
|
const cachedData = await this.apiCache.get(method, endpoint, data || {});
|
||||||
if (cachedData) {
|
if (cachedData) {
|
||||||
console.log(`API缓存命中: ${method} ${endpoint}`);
|
// console.log(`API缓存命中: ${method} ${endpoint}`);
|
||||||
return cachedData;
|
return cachedData;
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -1,10 +1,14 @@
|
|||||||
const axios = require('axios');
|
const axios = require('axios');
|
||||||
const { stringify } = require('qs');
|
const { stringify } = require('qs');
|
||||||
|
const ApiCacheService = require('./api-cache');
|
||||||
|
|
||||||
class ArtworkService {
|
class ArtworkService {
|
||||||
constructor(auth) {
|
constructor(auth) {
|
||||||
this.auth = auth;
|
this.auth = auth;
|
||||||
this.baseURL = 'https://app-api.pixiv.net';
|
this.baseURL = 'https://app-api.pixiv.net';
|
||||||
|
|
||||||
|
// 创建API缓存服务实例
|
||||||
|
this.apiCache = new ApiCacheService();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -361,6 +365,19 @@ class ArtworkService {
|
|||||||
* 发送API请求
|
* 发送API请求
|
||||||
*/
|
*/
|
||||||
async makeRequest(method, endpoint, data = null) {
|
async makeRequest(method, endpoint, data = null) {
|
||||||
|
// 对于GET请求,尝试从缓存获取
|
||||||
|
if (method === 'GET') {
|
||||||
|
try {
|
||||||
|
const cachedData = await this.apiCache.get(method, endpoint, data || {});
|
||||||
|
if (cachedData) {
|
||||||
|
// console.log(`API缓存命中: ${method} ${endpoint}`);
|
||||||
|
return cachedData;
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('读取API缓存失败:', error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (!this.auth || !this.auth.accessToken) {
|
if (!this.auth || !this.auth.accessToken) {
|
||||||
throw new Error('No access token available');
|
throw new Error('No access token available');
|
||||||
@@ -391,9 +408,20 @@ class ArtworkService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 发送API请求
|
// 发送API请求
|
||||||
|
|
||||||
const response = await axios(config);
|
const response = await axios(config);
|
||||||
return response.data;
|
const responseData = response.data;
|
||||||
|
|
||||||
|
// 对于GET请求,将响应数据缓存
|
||||||
|
if (method === 'GET') {
|
||||||
|
try {
|
||||||
|
await this.apiCache.set(method, endpoint, data || {}, responseData);
|
||||||
|
// console.log(`API缓存已保存: ${method} ${endpoint}`);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('保存API缓存失败:', error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return responseData;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('API request failed:', {
|
console.error('API request failed:', {
|
||||||
method,
|
method,
|
||||||
|
|||||||
Reference in New Issue
Block a user