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: Created Feb. 14, 2019, 5:36 a.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') | no next file » | 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,29 +798,26 @@
/**
* @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);
+ let result = this._resultCache.read(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);
+ this._resultCache.save(key, result);
return result;
}
/**
* @typedef {object} MatcherSearchResults
* @property {Array.<BlockingFilter>} [blocking] List of blocking filters
* found.
@@ -851,27 +843,24 @@
*/
search(location, typeMask, docDomain, thirdParty, sitekey, specificOnly,
filterType = "all")
{
let key = "* " + location + " " + typeMask + " " + docDomain + " " +
thirdParty + " " + sitekey + " " + specificOnly + " " +
filterType;
- let result = this._resultCache.get(key);
+ let result = this._resultCache.read(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);
+ this._resultCache.save(key, result);
return result;
}
/**
* Tests whether the URL is whitelisted
* @see Matcher#matchesAny
* @inheritdoc
« lib/caching.js ('K') | « lib/elemHide.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld