From c02358758c0f7f11870131104b3210ea7e5345d5 Mon Sep 17 00:00:00 2001 From: Victor Quinn Date: Mon, 7 Oct 2024 11:28:28 -0400 Subject: [PATCH] Fix bug where cache is never checked --- package.json | 2 +- src/weatherService.ts | 11 ++++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index a05d635..bf970f9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "weather-plus", - "version": "0.0.17", + "version": "0.0.18", "description": "Weather Plus is a powerful wrapper around various Weather APIs that simplifies adding weather data to your application", "main": "./dist/cjs/index.js", "module": "./dist/esm/index.js", diff --git a/src/weatherService.ts b/src/weatherService.ts index 52ae16f..9962775 100644 --- a/src/weatherService.ts +++ b/src/weatherService.ts @@ -50,8 +50,13 @@ export class WeatherService { log(`Getting weather for (${lat}, ${lng})`); const geohash = getGeohash(lat, lng, 6); - const weather = await this.providers[this.provider].getWeather(lat, lng); - await this.cache.set(geohash, JSON.stringify(weather), 300); // Cache for 5 mins - return weather; + const cachedWeather = await this.cache.get(geohash); + if (cachedWeather) { + return JSON.parse(cachedWeather); + } else { + const weather = await this.providers[this.provider].getWeather(lat, lng); + await this.cache.set(geohash, JSON.stringify(weather), 300); // Cache for 5 mins + return weather; + } } }