增加搜索缓存

This commit is contained in:
2025-08-30 13:29:18 +08:00
parent 2a980c83d2
commit aa04f9d03f
5 changed files with 68 additions and 11 deletions
+30 -2
View File
@@ -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,