parser_cache.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. 'use strict';
  2. const LRU = require('lru-cache');
  3. const parserCache = new LRU({
  4. max: 15000
  5. });
  6. function keyFromFields(type, fields, options, config) {
  7. let res =
  8. `${type}` +
  9. `/${typeof options.nestTables}` +
  10. `/${options.nestTables}` +
  11. `/${options.rowsAsArray}` +
  12. `/${options.supportBigNumbers || config.supportBigNumbers}` +
  13. `/${options.bigNumberStrings || config.bigNumberStrings}` +
  14. `/${typeof options.typeCast}` +
  15. `/${options.timezone || config.timezone}` +
  16. `/${options.decimalNumbers}` +
  17. `/${options.dateStrings}`;
  18. for (let i = 0; i < fields.length; ++i) {
  19. const field = fields[i];
  20. res += `/${field.name}:${field.columnType}:${field.length}:${field.schema}:${field.table}:${field.flags}:${field.characterSet}`;
  21. }
  22. return res;
  23. }
  24. function getParser(type, fields, options, config, compiler) {
  25. const key = keyFromFields(type, fields, options, config);
  26. let parser = parserCache.get(key);
  27. if (parser) {
  28. return parser;
  29. }
  30. parser = compiler(fields, options, config);
  31. parserCache.set(key, parser);
  32. return parser;
  33. }
  34. function setMaxCache(max) {
  35. parserCache.max = max;
  36. }
  37. function clearCache() {
  38. parserCache.reset();
  39. }
  40. module.exports = {
  41. getParser: getParser,
  42. setMaxCache: setMaxCache,
  43. clearCache: clearCache
  44. };