Refactor AccountStorage.ts to add caching for database saves
This commit is contained in:
@@ -10,6 +10,7 @@ import { language } from "src/lang"
|
|||||||
|
|
||||||
export const AccountWarning = writable('')
|
export const AccountWarning = writable('')
|
||||||
const risuSession = Date.now().toFixed(0)
|
const risuSession = Date.now().toFixed(0)
|
||||||
|
const cachedForage = localforage.createInstance({name: "risuaiAccountCached"})
|
||||||
|
|
||||||
let seenWarnings:string[] = []
|
let seenWarnings:string[] = []
|
||||||
|
|
||||||
@@ -21,6 +22,7 @@ export class AccountStorage{
|
|||||||
this.checkAuth()
|
this.checkAuth()
|
||||||
let da:Response
|
let da:Response
|
||||||
while((!da) || da.status === 403){
|
while((!da) || da.status === 403){
|
||||||
|
const saveDate = Date.now().toFixed(0)
|
||||||
da = await fetch(hubURL + '/api/account/write', {
|
da = await fetch(hubURL + '/api/account/write', {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: value,
|
body: value,
|
||||||
@@ -29,9 +31,16 @@ export class AccountStorage{
|
|||||||
'x-risu-key': key,
|
'x-risu-key': key,
|
||||||
'x-risu-auth': this.auth,
|
'x-risu-auth': this.auth,
|
||||||
'X-Format': 'nocheck',
|
'X-Format': 'nocheck',
|
||||||
'x-risu-session': risuSession
|
'x-risu-session': risuSession,
|
||||||
|
'x-risu-save-date': saveDate
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
if(key === 'database/database.bin'){
|
||||||
|
cachedForage.setItem(key, value).then(() => {
|
||||||
|
cachedForage.setItem(key + '__date', saveDate)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
if(da.headers.get('Content-Type') === 'application/json'){
|
if(da.headers.get('Content-Type') === 'application/json'){
|
||||||
const json = (await da.json())
|
const json = (await da.json())
|
||||||
if(json?.warning){
|
if(json?.warning){
|
||||||
@@ -72,13 +81,16 @@ export class AccountStorage{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
let da:Response
|
let da:Response
|
||||||
|
const saveDate = await cachedForage.getItem(key + '__date') as number|undefined
|
||||||
|
const perf = performance.now()
|
||||||
while((!da) || da.status === 403){
|
while((!da) || da.status === 403){
|
||||||
da = await fetch(hubURL + '/api/account/read/' + Buffer.from(key ,'utf-8').toString('hex') +
|
da = await fetch(hubURL + '/api/account/read/' + Buffer.from(key ,'utf-8').toString('hex') +
|
||||||
(key.includes('database') ? ('|' + v4()) : ''), {
|
(key.includes('database') ? ('|' + v4()) : ''), {
|
||||||
method: "GET",
|
method: "GET",
|
||||||
headers: {
|
headers: {
|
||||||
'x-risu-key': key,
|
'x-risu-key': key,
|
||||||
'x-risu-auth': this.auth
|
'x-risu-auth': this.auth,
|
||||||
|
'x-risu-save-date': (saveDate || 0).toString()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
if(da.status === 403){
|
if(da.status === 403){
|
||||||
@@ -86,6 +98,15 @@ export class AccountStorage{
|
|||||||
this.checkAuth()
|
this.checkAuth()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if(da.status === 303){
|
||||||
|
console.log(performance.now() - perf)
|
||||||
|
const data = await da.json()
|
||||||
|
if(data.match){
|
||||||
|
const c = Buffer.from(await cachedForage.getItem(key))
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if(da.status < 200 || da.status >= 300){
|
if(da.status < 200 || da.status >= 300){
|
||||||
throw await da.text()
|
throw await da.text()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user