feat: mailbox 缓存清除策略

dev/ckeditor
Lei OT 10 months ago
parent f4f956fd5e
commit d881778d78

@ -1,5 +1,5 @@
import { getEmailDirAction, getRootMailboxDirAction } from '@/actions/EmailActions'
import { buildTree, isEmpty, readIndexDB, writeIndexDB, createIndexedDBStore } from '@/utils/commons'
import { buildTree, isEmpty, readIndexDB, writeIndexDB, createIndexedDBStore, clean7DaysMailboxLog } from '@/utils/commons'
/**
* Email
@ -125,7 +125,7 @@ const emailSlice = (set, get) => ({
setCurrentMailboxDEI(dei_sn)
getOPIEmailDir(userId, userIdStr)
// 登录后, 执行一下清除缓存
// clean7DaysMailboxLog();
clean7DaysMailboxLog();
},
})

@ -919,7 +919,7 @@ export const deleteIndexDBbyKey = (key, table, database) => {
}
};
function cleanOldData(database, storeName, dateKey = 'timestamp') {
function cleanOldData(database, storeNames=[], dateKey = 'timestamp') {
return function (daysToKeep = 7) {
return new Promise((resolve, reject) => {
let deletedCount = 0
@ -927,38 +927,42 @@ function cleanOldData(database, storeName, dateKey = 'timestamp') {
let openRequest = indexedDB.open(database, INDEXED_DB_VERSION)
openRequest.onupgradeneeded = function () {
var db = openRequest.result
// var db = openRequest.result
// 数据库是否存在
if (!db.objectStoreNames.contains(storeName)) {
var store = db.createObjectStore(storeName, { keyPath: 'id', autoIncrement: true })
store.createIndex('timestamp', 'timestamp', { unique: false })
} else {
const logStore = openRequest.transaction.objectStore(storeName)
if (!logStore.indexNames.contains('timestamp')) {
logStore.createIndex('timestamp', 'timestamp', { unique: false })
}
}
// if (!db.objectStoreNames.contains(storeName)) {
// var store = db.createObjectStore(storeName, { keyPath: 'id', autoIncrement: true })
// store.createIndex('timestamp', 'timestamp', { unique: false })
// } else {
// const logStore = openRequest.transaction.objectStore(storeName)
// if (!logStore.indexNames.contains('timestamp')) {
// logStore.createIndex('timestamp', 'timestamp', { unique: false })
// }
// }
}
openRequest.onsuccess = function (e) {
let db = e.target.result
// 数据库是否存在
if (!db.objectStoreNames.contains(storeName)) {
resolve('Database does not exist.')
return
}
// if (!db.objectStoreNames.contains(storeName)) {
// resolve('Database does not exist.')
// return
// }
// Calculate the cutoff timestamp for "X days ago"
const cutoffTimestamp = Date.now() - daysToKeep * 24 * 60 * 60 * 1000
const objectStoreNames = isEmpty(storeNames) ? db.objectStoreNames : storeNames
if (!isEmpty(objectStoreNames)) {
const objectStores = Array.from(objectStoreNames).map((storeName) => db.transaction([storeName], 'readwrite').objectStore(storeName))
for (const objectStore of objectStores) {
// Identify old data using the date index and primary key ID
const readTransaction = db.transaction([storeName], 'readwrite')
const objectStore = readTransaction.objectStore(storeName)
if (!objectStore.indexNames.contains(`${dateKey}`)) {
// Clear the store
let clearRequest = objectStore.clear()
console.log(`Cleanup complete. clear ${storeName} records.`)
resolve();
console.log(`Cleanup complete. clear ${objectStore.name} records.`)
resolve()
clearRequest.onerror = function (e) {}
clearRequest.onsuccess = function (e) {}
return
@ -975,12 +979,13 @@ function cleanOldData(database, storeName, dateKey = 'timestamp') {
recordsToDelete.add(cursor.primaryKey) // Add the primary key of the record to the set
cursor.continue()
} else {
const storeName = objectStore.name;
// Delete identified data in a new transaction
const deleteTransaction = db.transaction([storeName], 'readwrite')
const deleteObjectStore = deleteTransaction.objectStore(storeName)
deleteTransaction.oncomplete = () => {
console.log(`Cleanup complete. Deleted ${deletedCount} records.`)
console.log(`Cleanup complete. Deleted ${deletedCount} records in ${database}.${storeName}.`)
resolve(deletedCount)
}
@ -1007,6 +1012,8 @@ function cleanOldData(database, storeName, dateKey = 'timestamp') {
reject(event.target.error)
}
}
}
}
openRequest.onerror = function (e) {
reject('Error opening database:'+database, e)
}
@ -1014,4 +1021,5 @@ function cleanOldData(database, storeName, dateKey = 'timestamp') {
}
}
export const clean7DaysWebsocketLog = cleanOldData('LogWebsocketData', 'LogStore');
export const clean7DaysWebsocketLog = cleanOldData('LogWebsocketData', ['LogStore']);
export const clean7DaysMailboxLog = cleanOldData('mailbox');

Loading…
Cancel
Save