vsm-dictionary-cacher
Summary
vsm-dictionary-cacher
augments a given VSM-dictionary with a layer of
caching functionality.
This speeds up requests for string-matches, refTerms, and dictInfos;
and fixedTerms-preloading.
Use in Node.js
Install like (after also installing a vsm-dictionary-...
of choice) :
npm install vsm-dictionary-cacher
Then use like:
const Dictionary = require('vsm-dictionary-local');
const cacher = require('vsm-dictionary-cacher');
const CachedDictionary = cacher(Dictionary);
var dict = new CachedDictionary();
dict.getMatchesForString('abc', {filter: {dictID: ['Foo']}}, (err, res) => {
console.dir(res);
dict.getMatchesForString('abc', {filter: {dictID: ['Foo']}}, (err, res) => {
console.dir(res);
});
dict.getMatchesForString('abc', {filter: {dictID: ['BAR']}}, (err, res) => {});
dict.getMatchesForString('QQQ', {filter: {dictID: ['Foo']}}, (err, res) => {});
});
Specify options like:
const CachedDictionary = cacher(Dictionary, { maxItems: 1000 });
Use in the browser
<script src="https://unpkg.com/vsm-dictionary-cacher@^1.0.0/dist/vsm-dictionary-cacher.min.js"></script>
after which it is accessible as the global variable VsmDictionaryCacher
.
Then it can be wrapped around a VsmDictionary, e.g. a VsmDictionaryLocal
, like:
....
<script src="https://unpkg.com/vsm-dictionary-local@^2.0.0/dist/vsm-dictionary-local.min.js"></script>
<script>
var dict = new (VsmDictionaryCacher(VsmDictionaryLocal)) (options);
dict.getMatchesForString(....
</script>
Details
This package provides a factory function that accepts any VsmDictionary
(sub)class, and returns a (further) subclass of it,
which inserts cache handling code into several functions:
Options
An options object can be given as second argument to the factory function
(see example above), with these optional properties:
maxItems
: {Number}:
This limits the amount of items kept in the string-match cache (only).
One item equals the result of one getMatchesForString()
query (which is
often a list of match-objects).
When adding a new item to a full cache, the least recently added or accessed
item gets removed first.
Default is 0, which means unlimited storage.
- Note: this pertains to string-match-objects only;
so not to refTerms (they are small and few), not to dictInfos (same reason,
and the result of one query is spread out over multiple cache-items, which
is hard to manage), and not to fixedTerms (same).
predictEmpties
: {Boolean}:
If true
, then it keeps a list of strings (per options-object) for which
getEntryMatchesForString()
returned no results (i.e.: { items: [] }
).
Then for any subsequent query (with same options) for a string that
starts with any such empty-returning-string, we can assume that no results
will be returned either.
E.g. if a search for 'ab' returned no matching entries, then neither will
'abc'. So it can avoid running that query and immediately return the
empty { items: [] }
for 'abc'.
Default is true
.
- Note:
maxItems
does not apply to this collection of strings either.
But the collection gets cleared, like everything else, by a call
to clearCache()
(see below). - Note: this is handled in
getEntryMatchesForString()
, not in
getMatchesForString()
, because the latter may still add 'extra' matches
(refTerm/number/fixedTerm), other than the 'entry'-type matches.
- Example 1: after a call for "i" would give no entry-matches (and "i"
ends up in the 'cacheEmpties'), a subsequent call for "it" should still
return "it" as a refTerm-match.
(Note that a refTerm only matches for a full, not partial, string match). - Example 2: after a call for "1e" gave no results, a subsequent call
for the valid number-string "1e5" should still return it as a result.
Functions
An extra function is added to the VsmDictionary subclass:
clearCache()
:
This removes all data from the cache layer, including e.g. the list used by
predictEmpties
.
License
This project is licensed under the AGPL license - see LICENSE.md.