Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: lib/matcher.js

Issue 30007559: Issue 7285 - Abstract caching logic (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Add tests and change API and behavior slightly Created Feb. 16, 2019, 12:14 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« lib/caching.js ('K') | « lib/elemHide.js ('k') | test/caching.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/matcher.js
===================================================================
--- a/lib/matcher.js
+++ b/lib/matcher.js
@@ -19,16 +19,17 @@
/**
* @fileOverview Matcher class implementing matching addresses against
* a list of filters.
*/
const {RegExpFilter, WhitelistFilter} = require("./filterClasses");
const {normalizeHostname, suffixes} = require("./domain");
+const {Cache} = require("./caching");
/**
* Regular expression for matching a keyword in a filter.
* @type {RegExp}
*/
const keywordRegExp = /[^a-z0-9%*][a-z0-9%]{3,}(?=[^a-z0-9%*])/;
/**
@@ -619,41 +620,35 @@
* Combines a matcher for blocking and exception rules, automatically sorts
* rules into two {@link Matcher} instances.
*/
class CombinedMatcher
{
constructor()
{
/**
- * Maximal number of matching cache entries to be kept
- * @type {number}
- */
- this.maxCacheEntries = 10000;
-
- /**
* Matcher for blocking rules.
* @type {Matcher}
* @private
*/
this._blacklist = new Matcher();
/**
* Matcher for exception rules.
* @type {Matcher}
* @private
*/
this._whitelist = new Matcher();
/**
* Lookup table of previous {@link Matcher#matchesAny} results
- * @type {Map.<string,Filter>}
+ * @type {Cache.<string, ?Filter>}
* @private
*/
- this._resultCache = new Map();
+ this._resultCache = new Cache(10000);
}
/**
* @see Matcher#clear
*/
clear()
{
this._blacklist.clear();
@@ -803,28 +798,25 @@
/**
* @see Matcher#matchesAny
* @inheritdoc
*/
matchesAny(location, typeMask, docDomain, thirdParty, sitekey, specificOnly)
{
let key = location + " " + typeMask + " " + docDomain + " " + thirdParty +
- " " + sitekey + " " + specificOnly;
+ " " + sitekey + " " + specificOnly;
let result = this._resultCache.get(key);
if (typeof result != "undefined")
return result;
result = this._matchesAnyInternal(location, typeMask, docDomain,
thirdParty, sitekey, specificOnly);
- if (this._resultCache.size >= this.maxCacheEntries)
- this._resultCache.clear();
-
this._resultCache.set(key, result);
return result;
}
/**
* @typedef {object} MatcherSearchResults
* @property {Array.<BlockingFilter>} [blocking] List of blocking filters
@@ -858,19 +850,16 @@
let result = this._resultCache.get(key);
if (typeof result != "undefined")
return result;
result = this._searchInternal(location, typeMask, docDomain, thirdParty,
sitekey, specificOnly, filterType);
- if (this._resultCache.size >= this.maxCacheEntries)
- this._resultCache.clear();
-
this._resultCache.set(key, result);
return result;
}
/**
* Tests whether the URL is whitelisted
* @see Matcher#matchesAny
« lib/caching.js ('K') | « lib/elemHide.js ('k') | test/caching.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld