parser_cache.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. 'use strict';
  2. const { createLRU } = require('lru.min');
  3. const parserCache = createLRU({
  4. max: 15000,
  5. });
  6. function keyFromFields(type, fields, options, config) {
  7. const res = [
  8. type,
  9. typeof options.nestTables,
  10. options.nestTables,
  11. Boolean(options.rowsAsArray),
  12. Boolean(options.supportBigNumbers || config.supportBigNumbers),
  13. Boolean(options.bigNumberStrings || config.bigNumberStrings),
  14. typeof options.typeCast,
  15. options.timezone || config.timezone,
  16. Boolean(options.decimalNumbers),
  17. options.dateStrings,
  18. ];
  19. for (let i = 0; i < fields.length; ++i) {
  20. const field = fields[i];
  21. res.push([
  22. field.name,
  23. field.columnType,
  24. field.length,
  25. field.schema,
  26. field.table,
  27. field.flags,
  28. field.characterSet,
  29. ]);
  30. }
  31. return JSON.stringify(res, null, 0);
  32. }
  33. function getParser(type, fields, options, config, compiler) {
  34. const key = keyFromFields(type, fields, options, config);
  35. let parser = parserCache.get(key);
  36. if (parser) {
  37. return parser;
  38. }
  39. parser = compiler(fields, options, config);
  40. parserCache.set(key, parser);
  41. return parser;
  42. }
  43. function setMaxCache(max) {
  44. parserCache.resize(max);
  45. }
  46. function clearCache() {
  47. parserCache.clear();
  48. }
  49. module.exports = {
  50. getParser: getParser,
  51. setMaxCache: setMaxCache,
  52. clearCache: clearCache,
  53. _keyFromFields: keyFromFields,
  54. };