Add maximum link cache size

This commit is contained in:
benji7425 2017-08-23 01:27:06 +01:00
parent f96b1a3585
commit bf9fbdcdae
3 changed files with 11 additions and 5 deletions

View File

@ -1,6 +1,7 @@
{
"saveIntervalSec": 60,
"feedCheckIntervalSec": 30,
"maxCacheSize": 10,
"commands": {
"version": "version",
"addFeed": "add-feed"

View File

@ -53,7 +53,7 @@ const HandleMessage = {
message.reply("v" + require("../package.json").version);
break;
case config.commands.addFeed:
addFeed(client, guildsData, message);
addFeed(client, guildsData, message, config.maxCacheSize);
break;
}
}
@ -66,7 +66,7 @@ const HandleMessage = {
}
};
function addFeed(client, guildsData, message) {
function addFeed(client, guildsData, message, maxCacheSize) {
const parameters = message.content.split(" "); //expect !addfeed <url> <channelName> <roleName>
const feedUrl = [...GetUrls(message.content)][0];
@ -80,7 +80,8 @@ function addFeed(client, guildsData, message) {
const feedData = new FeedData({
url: feedUrl,
channelName: channel.name,
roleName: role ? role.name : null
roleName: role ? role.name : null,
maxCacheSize: maxCacheSize
});
//ask the user if they're happy with the details they set up, save if yes, don't if no

View File

@ -8,17 +8,21 @@ const FeedRead = require("feed-read"); //for extracing new links from RSS feeds
const GetUrls = require("get-urls"); //for extracting urls from messages
module.exports = class FeedData {
constructor({ url, channelName, roleName, cachedLinks }) {
constructor({ url, channelName, roleName, cachedLinks, maxCacheSize }) {
this.url = url;
this.channelName = channelName;
this.roleName = roleName;
this.cachedLinks = cachedLinks || [];
this.maxCacheSize = maxCacheSize || 10;
this.cachedLinks.push = (...elements) => {
const unique = elements
.map(el => normaliseUrl(el)) //normalise all the urls
.filter(el => !this.cachedLinks.includes(el)); //filter out any already cached
Array.prototype.push.apply(this.cachedLinks, unique);
Array.prototype.push.apply(this.cachedLinks, unique);
if(this.cachedLinks.length > this.maxCacheSize)
this.cachedLinks.splice(0, this.cachedLinks.length - this.maxCacheSize); //remove the # of elements above the max from the beginning
};
}