@plantinformatics/vcf-genotype-brapi
Version:
Client and server functions to access genotype data from VCF via a custom web API and BrAPI
72 lines (62 loc) • 2.26 kB
JavaScript
// old API is available via require()
// let flatCache = require('flat-cache');
import { /*FlatCache,*/ create } from 'flat-cache';
/* global require */
/* global exports */
/*----------------------------------------------------------------------------*/
/** function API signature generated by ChatGPT, see ./cache-browser.js */
/** Implementation copied from pretzel/lb4app/lb3app/common/utilities/results-cache.js */
/**
* File-based persistent cache for Node.js, implemented using flat-cache.
* @class CacheWrapper
*/
export class CacheWrapper {
/** If cache is not initialised, initialise it, calling .load().
* The constructor signature provides the same interface as
* cache-browser.js.
*
* Default cacheDir is .cache in server cwd, i.e. lb4app/lb3app/.cache/
* (previously : /app/node_modules/flat-cache/.cache)
* Default cacheId is cache1 (i.e. file .cache/cache1)
* Cache format is json.
* @param {string} dbName
* @param {string} storeName
*/
constructor(dbName, storeName) {
{
const
cacheId = storeName,
cacheDir = './Cache/' + dbName;
/* old API : cache = flatCache.load(cacheName);
* cache = new FlatCache(); cache.load(cacheName);
* Unlike create(), .load() does not set the given name as cacheId
* (although the function header comment says : If specified `cacheDir`
* will be used as the directory to persist the data to. refn :
* node_modules/flat-cache/dist/index.js )
*/
this.cache = create({ cacheId, cacheDir });
}
}
/**
* Caches data in a file using flat-cache.
* @param {string} key - The key to store the data under.
* @param {any} value - The value to store.
*/
set(key, value) {
this.cache.setKey(key, value);
/** https://github.com/royriojas/flat-cache#readme : "Non visited
* keys are removed when cache.save() is called" if noPrune is not
* true
*/
this.cache.save(/*noPrune*/ true);
}
/**
* Retrieves cached data from flat-cache.
* @param {string} key - The key to retrieve the data for.
* @returns {any|null} The cached value or null if not found.
*/
get(key) {
let cacheContent = this.cache.getKey(key);
return cacheContent;
}
}