flat: 性能优化,招聘会时间筛选性能优化、精选企业性能优化,封装缓存request、indexDb方法,主要用于不常更新的接口,以达到毫秒级性能
This commit is contained in:
91
utils/db.js
Normal file
91
utils/db.js
Normal file
@@ -0,0 +1,91 @@
|
||||
// BaseDBStore.js
|
||||
import IndexedDBHelper from '@/common/IndexedDBHelper.js'
|
||||
// import UniStorageHelper from '../common/UniStorageHelper'
|
||||
import useChatGroupDBStore from '@/stores/userChatGroupStore'
|
||||
import config from '@/config'
|
||||
|
||||
class BaseStore {
|
||||
db = null
|
||||
isDBReady = false
|
||||
dbName = 'BrowsingHistory' // 'AppMainDB'
|
||||
initPromise = null
|
||||
|
||||
constructor() {
|
||||
this.initPromise = this.checkAndInitDB()
|
||||
}
|
||||
|
||||
async getDB() {
|
||||
if (!this.initPromise) {
|
||||
this.initPromise = this.checkAndInitDB();
|
||||
}
|
||||
await this.initPromise; // 等待初始化完成
|
||||
return this.db;
|
||||
}
|
||||
|
||||
async checkAndInitDB() {
|
||||
if (config.OnlyUseCachedDB) {
|
||||
return this.initDB()
|
||||
}
|
||||
const localVersion = uni.getStorageSync('indexedDBVersion') || 1
|
||||
console.log('DBVersion: ', localVersion, config.DBversion)
|
||||
if (localVersion === config.DBversion) {
|
||||
return this.initDB() // 🟢 记得加 return
|
||||
} else {
|
||||
console.log('清空本地数据库')
|
||||
await this.clearDB() // 🟢 建议用 await
|
||||
uni.setStorageSync('indexedDBVersion', config.DBversion);
|
||||
return this.initDB(); // 🟢 记得加 return
|
||||
}
|
||||
}
|
||||
|
||||
initDB() {
|
||||
// // #ifdef H5
|
||||
this.db = new IndexedDBHelper(this.dbName, config.DBversion);
|
||||
// // #endif
|
||||
|
||||
return this.db.openDB([{
|
||||
name: 'record',
|
||||
keyPath: "id",
|
||||
autoIncrement: true,
|
||||
},
|
||||
{
|
||||
name: 'messageGroup',
|
||||
keyPath: "id",
|
||||
autoIncrement: true,
|
||||
},
|
||||
{
|
||||
name: 'messages',
|
||||
keyPath: "id",
|
||||
autoIncrement: true,
|
||||
indexes: [{
|
||||
name: 'parentGroupId',
|
||||
key: 'parentGroupId',
|
||||
unique: false
|
||||
}]
|
||||
},
|
||||
{
|
||||
name: 'api_cache',
|
||||
keyPath: "cacheKey", // 使用 URL+参数 作为主键
|
||||
indexes: []
|
||||
}
|
||||
]).then(async () => {
|
||||
// 这里原来的逻辑保留
|
||||
if (useChatGroupDBStore) {
|
||||
useChatGroupDBStore().init()
|
||||
}
|
||||
this.isDBReady = true
|
||||
return this.db;
|
||||
});
|
||||
}
|
||||
|
||||
async clearDB() {
|
||||
return new Promise((resolve, rejetc) => {
|
||||
new IndexedDBHelper().deleteDB(this.dbName).then(() => {
|
||||
resolve()
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const baseDB = new BaseStore()
|
||||
export default baseDB
|
||||
@@ -1,14 +1,15 @@
|
||||
import config from "@/config.js"
|
||||
import {
|
||||
sm2_Decrypt,
|
||||
sm2_Encrypt
|
||||
} from '@/common/globalFunction';
|
||||
import useUserStore from '@/stores/useUserStore';
|
||||
import {
|
||||
sm2_Encrypt,
|
||||
sm4Decrypt,
|
||||
sm4Encrypt
|
||||
} from '../common/globalFunction';
|
||||
} from '@/common/globalFunction';
|
||||
import IndexedDBHelper from '@/common/IndexedDBHelper';
|
||||
import useUserStore from '@/stores/useUserStore';
|
||||
import baseDB from '@/utils/db.js';
|
||||
|
||||
const CACHE_STORE_NAME = 'api_cache';
|
||||
|
||||
const needToEncrypt = [
|
||||
["post", "/app/login"],
|
||||
@@ -20,6 +21,47 @@ const needToEncrypt = [
|
||||
["get", "/app/user/experience/list"]
|
||||
]
|
||||
|
||||
/**
|
||||
* 带缓存的请求方法
|
||||
*/
|
||||
export async function createRequestWithCache(url, data = {}, method = 'GET', loading = false, headers = {},
|
||||
onCacheLoad = null) {
|
||||
const cacheKey = `${method.toUpperCase()}:${url}:${JSON.stringify(data)}`;
|
||||
|
||||
baseDB.getDB().then(async (dbHelper) => {
|
||||
try {
|
||||
const cachedRecord = await dbHelper.get(CACHE_STORE_NAME, cacheKey);
|
||||
|
||||
if (cachedRecord && cachedRecord.response && typeof onCacheLoad === 'function') {
|
||||
onCacheLoad(cachedRecord.response);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('读取缓存失败', e);
|
||||
}
|
||||
});
|
||||
|
||||
// 3. 发起网络请求
|
||||
try {
|
||||
const networkResponse = await createRequest(url, data, method, loading, headers);
|
||||
baseDB.getDB().then(async (dbHelper) => {
|
||||
try {
|
||||
await dbHelper.update(CACHE_STORE_NAME, {
|
||||
cacheKey: cacheKey,
|
||||
response: networkResponse,
|
||||
timestamp: Date.now()
|
||||
});
|
||||
console.log('💾 [BaseDB] 缓存更新:', url);
|
||||
} catch (e) {
|
||||
console.error('更新缓存失败', e);
|
||||
}
|
||||
});
|
||||
|
||||
return networkResponse;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param url String,请求的地址,默认:none
|
||||
* @param data Object,请求的参数,默认:{}
|
||||
|
||||
Reference in New Issue
Block a user