This commit is contained in:
2024-05-24 21:22:28 +02:00
parent a7b1fb902c
commit 9f2627faf8
8 changed files with 115 additions and 29 deletions
+11 -15
View File
@@ -1,15 +1,11 @@
import { defineStore } from 'pinia';
import { ref } from 'vue';
import { RegionalMarketCache } from '../RegionalMarketCache';
import { jitaId } from '../market';
import { MarketType } from "../type";
import { MarketTypePrice } from './MarketTypePrice';
import { getEvepraisalPrices } from './evepraisal';
import { getfuzzworkPrices } from './fuzzwork';
type MarketTypePriceCache = {
price: MarketTypePrice,
date: Date
}
const cacheDuration = 1000 * 60 * 5; // 5 minutes
const priceGetters = {
evepraisal: getEvepraisalPrices,
@@ -17,21 +13,21 @@ const priceGetters = {
}
export const useApraisalStore = defineStore('appraisal', () => {
const cache = ref<Record<number, MarketTypePriceCache>>({});
const cache: RegionalMarketCache<MarketTypePrice> = new RegionalMarketCache(cacheDuration);
const getPricesUncached = priceGetters.fuzzwork;
const getPrice = async (type: MarketType): Promise<MarketTypePrice> => (await getPrices([type]))[0];
const getPrices = async (types: MarketType[]): Promise<MarketTypePrice[]> => {
const now = new Date();
const getPrice = async (type: MarketType, regionId?: number): Promise<MarketTypePrice> => (await getPrices([type], regionId))[0];
const getPrices = async (types: MarketType[], regionId?: number): Promise<MarketTypePrice[]> => {
const cached: MarketTypePrice[] = [];
const uncached: MarketType[] = [];
const rId = regionId ?? jitaId;
types.forEach(t => {
const cachedPrice = cache.value[t.id];
const cachedPrice = cache.get(rId, t.id);
if (cachedPrice && now.getTime() - cachedPrice.date.getTime() < cacheDuration) {
cached.push(cachedPrice.price);
if (cachedPrice) {
cached.push(cachedPrice);
} else {
uncached.push(t);
}
@@ -40,8 +36,8 @@ export const useApraisalStore = defineStore('appraisal', () => {
if (uncached.length > 0) {
const prices = await getPricesUncached(uncached);
prices.forEach(p => cache.value[p.type.id] = { price: p, date: now });
return [...cached, ...prices];
prices.forEach(p => cache.set(rId, p.type.id, p));
return [ ...cached, ...prices ];
}
return cached;
};