增加搜索缓存
This commit is contained in:
@@ -37,7 +37,13 @@ class ApiCacheService {
|
||||
'/v1/user/illusts',
|
||||
'/v1/user/following',
|
||||
'/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: {
|
||||
@@ -66,7 +72,7 @@ class ApiCacheService {
|
||||
// 启动定期清理任务
|
||||
this.startCleanupTask();
|
||||
|
||||
console.log('API缓存服务初始化完成');
|
||||
// console.log('API缓存服务初始化完成');
|
||||
} catch (error) {
|
||||
console.error('API缓存服务初始化失败:', error);
|
||||
}
|
||||
@@ -78,7 +84,7 @@ class ApiCacheService {
|
||||
async ensureCacheDir() {
|
||||
try {
|
||||
await fs.mkdir(this.cacheDir, { recursive: true });
|
||||
console.log('API缓存目录创建成功:', this.cacheDir);
|
||||
// console.log('API缓存目录创建成功:', this.cacheDir);
|
||||
} catch (error) {
|
||||
console.error('创建API缓存目录失败:', error);
|
||||
}
|
||||
@@ -98,9 +104,30 @@ class ApiCacheService {
|
||||
// 确保params是对象
|
||||
const safeParams = params || {};
|
||||
|
||||
// 如果endpoint已经包含查询参数(包含?),则直接使用endpoint作为键
|
||||
// 如果endpoint已经包含查询参数(包含?),则解析endpoint中的参数
|
||||
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) {
|
||||
// 如果endpoint不包含查询参数,且params不为空,则添加查询参数
|
||||
const sortedParams = Object.keys(safeParams)
|
||||
|
||||
@@ -335,7 +335,7 @@ class ArtistService {
|
||||
try {
|
||||
const cachedData = await this.apiCache.get(method, endpoint, data || {});
|
||||
if (cachedData) {
|
||||
console.log(`API缓存命中: ${method} ${endpoint}`);
|
||||
// console.log(`API缓存命中: ${method} ${endpoint}`);
|
||||
return cachedData;
|
||||
}
|
||||
} catch (error) {
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
const axios = require('axios');
|
||||
const { stringify } = require('qs');
|
||||
const ApiCacheService = require('./api-cache');
|
||||
|
||||
class ArtworkService {
|
||||
constructor(auth) {
|
||||
this.auth = auth;
|
||||
this.baseURL = 'https://app-api.pixiv.net';
|
||||
|
||||
// 创建API缓存服务实例
|
||||
this.apiCache = new ApiCacheService();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -361,6 +365,19 @@ class ArtworkService {
|
||||
* 发送API请求
|
||||
*/
|
||||
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 {
|
||||
if (!this.auth || !this.auth.accessToken) {
|
||||
throw new Error('No access token available');
|
||||
@@ -391,9 +408,20 @@ class ArtworkService {
|
||||
}
|
||||
|
||||
// 发送API请求
|
||||
|
||||
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) {
|
||||
console.error('API request failed:', {
|
||||
method,
|
||||
|
||||
Reference in New Issue
Block a user