parser_cache.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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.flags}:${
  21. field.characterSet
  22. }`;
  23. }
  24. return res;
  25. }
  26. function getParser(type, fields, options, config, compiler) {
  27. const key = keyFromFields(type, fields, options, config);
  28. let parser = parserCache.get(key);
  29. if (parser) {
  30. return parser;
  31. }
  32. parser = compiler(fields, options, config);
  33. parserCache.set(key, parser);
  34. return parser;
  35. }
  36. function setMaxCache(max) {
  37. parserCache.max = max;
  38. }
  39. function clearCache() {
  40. parserCache.reset();
  41. }
  42. module.exports = {
  43. getParser: getParser,
  44. setMaxCache: setMaxCache,
  45. clearCache: clearCache
  46. };