From 14b2f01ef1570871338dd72079c83b1073c0fa8b Mon Sep 17 00:00:00 2001 From: Sirttas Date: Sat, 23 Sep 2023 10:56:17 +0200 Subject: [PATCH] cahce apraisal --- src/market/appraisal.ts | 40 ++++++++++++++++++++++++++++++++------ src/pages/market/Scan.vue | 7 ++++--- src/pages/market/Track.vue | 6 ++++-- 3 files changed, 42 insertions(+), 11 deletions(-) diff --git a/src/market/appraisal.ts b/src/market/appraisal.ts index e277501..a1b085a 100644 --- a/src/market/appraisal.ts +++ b/src/market/appraisal.ts @@ -1,4 +1,6 @@ import { evepraisalAxiosInstance } from '@/service'; +import { defineStore } from 'pinia'; +import { ref } from 'vue'; import { MarketType } from "./type"; export type MarketTypePrice = { @@ -7,9 +9,35 @@ export type MarketTypePrice = { sell: number } -export const getPrice = async (type: MarketType): Promise => (await getPrices([type]))[0]; -export const getPrices = async (types: MarketType[]): Promise => (await evepraisalAxiosInstance.post(`/appraisal.json?market=jita&persist=no&raw_textarea=${types.map(t => t.name).join("%0A")}`)).data.appraisal.items.map((item: any) => ({ - type: types.find(t => t.name === item.typeName), - buy: item.prices.buy.max, - sell: item.prices.sell.min -})); \ No newline at end of file +type MarketTypePriceCache = { + price: MarketTypePrice, + date: Date +} + +const cacheDuration = 1000 * 60 * 5; // 5 minutes + +export const useApraisalStore = defineStore('appraisal', () => { + const cache = ref>({}); + + const getPricesUncached = async (types: MarketType[]): Promise => (await evepraisalAxiosInstance.post(`/appraisal.json?market=jita&persist=no&raw_textarea=${types.map(t => t.name).join("%0A")}`)).data.appraisal.items.map((item: any) => ({ + type: types.find(t => t.name === item.typeName), + buy: item.prices.buy.max, + sell: item.prices.sell.min + })); + + const getPrice = async (type: MarketType): Promise => (await getPrices([type]))[0]; + const getPrices = async (types: MarketType[]): Promise => { + const now = new Date().getTime(); + const cached = types.map(t => cache.value[t.id]).filter(c => c && now - c.date.getTime() < cacheDuration); + const uncached = types.filter(t => !cached.find(c => c.price.type.id === t.id)); + + if (uncached.length > 0) { + const prices = await getPricesUncached(uncached); + + prices.forEach(p => cache.value[p.type.id] = { price: p, date: new Date() }); + return [...cached.map(c => c.price), ...prices]; + } + return cached.map(c => c.price); + }; + return { getPrice, getPrices }; +}); \ No newline at end of file diff --git a/src/pages/market/Scan.vue b/src/pages/market/Scan.vue index 6f60204..49622a0 100644 --- a/src/pages/market/Scan.vue +++ b/src/pages/market/Scan.vue @@ -1,5 +1,5 @@