Skip to content

Commit

Permalink
Fixed issue with deleting old rates from rates cache
Browse files Browse the repository at this point in the history
  • Loading branch information
olarotseyi committed Aug 2, 2022
1 parent e121673 commit cc1387a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
36 changes: 25 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,9 @@ class CurrencyConverter {
this.isRatesCaching = ratesCacheOptions.isRatesCaching;

if (ratesCacheOptions.ratesCacheDuration === undefined)
this.ratesCacheDuration = 3600000; // Defaults to 3600 seconds (1 hour)
this.ratesCacheDuration = 3600; // Defaults to 3600 seconds (1 hour)
else
this.ratesCacheDuration = ratesCacheOptions.ratesCacheDuration * 1000;
this.ratesCacheDuration = ratesCacheOptions.ratesCacheDuration;

return this
}
Expand All @@ -261,9 +261,10 @@ class CurrencyConverter {
return new Promise((resolve, _) => {resolve(1) })
} else {
let currencyPair = this.currencyFrom.toUpperCase() + this.currencyTo.toUpperCase();
if (currencyPair in this.ratesCache) {
let now = new Date();
if (currencyPair in this.ratesCache && now < this.ratesCache[currencyPair].expiryDate) {
return new Promise((resolve, _) => {
resolve(this.ratesCache[currencyPair]);
resolve(this.ratesCache[currencyPair].rate);
});
} else {
return got(`https://www.google.co.in/search?q=${this.currencyFrom}+to+${this.currencyTo}+&hl=en`)
Expand All @@ -282,8 +283,7 @@ class CurrencyConverter {
rates = this.replaceAll(rates, ",", "")
}
if (this.isRatesCaching) {
this.ratesCache[currencyPair] = parseFloat(rates);
this.removeCurrencyPairFromRatesCache(currencyPair);
this.addRateToRatesCache(currencyPair, parseFloat(rates));
}
return parseFloat(rates)
})
Expand Down Expand Up @@ -321,11 +321,25 @@ class CurrencyConverter {
return this.currencies[currencyCode_]
}

removeCurrencyPairFromRatesCache(currencyPair) {
// Deletes cached currencyPair rate an hour later
setTimeout(function() {
delete this.ratesCache[currencyPair];
}, this.ratesCacheDuration);
addRateToRatesCache(currencyPair, rate_) {
let now = new Date();
if (currencyPair in this.ratesCache) {
if (now > this.ratesCache[currencyPair].expiryDate) {
let newExpiry = new Date();
newExpiry.setSeconds(newExpiry.getSeconds() + this.ratesCacheDuration);
this.ratesCache[currencyPair] = {
rate: rate_,
expiryDate: newExpiry
};
}
} else {
let newExpiry = new Date();
newExpiry.setSeconds(newExpiry.getSeconds() + this.ratesCacheDuration);
this.ratesCache[currencyPair] = {
rate: rate_,
expiryDate: newExpiry
};
}
}
}

Expand Down
6 changes: 4 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ currencyConverter.rates().then((response) => {
})
```

Rates can be cached. To implement rate caching, instantiate an object of CurrencyConverter only once in your project, in a CurrencyConverter file, and setup rates caching then import the instance of CurrencyConverter from the CurrencyConverter file in your project across the rest of your project. Use chaining to convert currencies when caching is implemented. Below is an example of a CurrencyConverter file.
Rates can be cached for currency pairs. To implement rate caching, instantiate an object of CurrencyConverter only once in your project, in a CurrencyConverter file, and setup rates caching then import the instance of CurrencyConverter from the CurrencyConverter file in your project across the rest of your project. Use chaining to convert currencies when caching is implemented. Below is an example of a CurrencyConverter file.

Note: Rates are not actually deleted after the ratesCacheDuration. The rate remains in the rates cache of the CurrencyConverter object until a request is made for the same currency pair at which point, the old rate is overwritten.

```javascript
const CC = require('currency-converter-lt')
Expand All @@ -84,7 +86,7 @@ let currencyConverter = new CC()

let ratesCacheOptions = {
isRatesCaching: true, // Set this boolean to true to implement rate caching
ratesCacheDuration: 3600 // Set this to a positive number to set the number of seconds you want your rates to be cached. Defaults to 3600 seconds (1 hour)
ratesCacheDuration: 3600 // Set this to a positive number to set the number of seconds you want the rates to be cached. Defaults to 3600 seconds (1 hour)
}

currencyConverter = currencyConverter.setupRatesCache(ratesCacheOptions)
Expand Down

0 comments on commit cc1387a

Please sign in to comment.