* Serializes this instance into the provided serializer context. * @param {ObjectSerializerContext & { logger: Logger, profile: boolean | undefined }} context context
({ write, snapshot, rollback, logger, profile })
| 719 | * @param {ObjectSerializerContext & { logger: Logger, profile: boolean | undefined }} context context |
| 720 | */ |
| 721 | serialize({ write, snapshot, rollback, logger, profile }) { |
| 722 | if (profile) { |
| 723 | write(false); |
| 724 | for (const [key, value] of this.map) { |
| 725 | const s = snapshot(); |
| 726 | try { |
| 727 | write(key); |
| 728 | const start = process.hrtime(); |
| 729 | write(value); |
| 730 | const durationHr = process.hrtime(start); |
| 731 | const duration = durationHr[0] * 1000 + durationHr[1] / 1e6; |
| 732 | if (duration > 1) { |
| 733 | if (duration > 500) { |
| 734 | logger.error(`Serialization of '${key}': ${duration} ms`); |
| 735 | } else if (duration > 50) { |
| 736 | logger.warn(`Serialization of '${key}': ${duration} ms`); |
| 737 | } else if (duration > 10) { |
| 738 | logger.info(`Serialization of '${key}': ${duration} ms`); |
| 739 | } else if (duration > 5) { |
| 740 | logger.log(`Serialization of '${key}': ${duration} ms`); |
| 741 | } else { |
| 742 | logger.debug(`Serialization of '${key}': ${duration} ms`); |
| 743 | } |
| 744 | } |
| 745 | } catch (err) { |
| 746 | rollback(s); |
| 747 | if (err === NOT_SERIALIZABLE) continue; |
| 748 | const msg = "Skipped not serializable cache item"; |
| 749 | const notSerializableErr = /** @type {Error} */ (err); |
| 750 | if (notSerializableErr.message.includes("ModuleBuildError")) { |
| 751 | logger.log( |
| 752 | `${msg} (in build error): ${notSerializableErr.message}` |
| 753 | ); |
| 754 | logger.debug( |
| 755 | `${msg} '${key}' (in build error): ${notSerializableErr.stack}` |
| 756 | ); |
| 757 | } else { |
| 758 | logger.warn(`${msg}: ${notSerializableErr.message}`); |
| 759 | logger.debug(`${msg} '${key}': ${notSerializableErr.stack}`); |
| 760 | } |
| 761 | } |
| 762 | } |
| 763 | write(null); |
| 764 | return; |
| 765 | } |
| 766 | // Try to serialize all at once |
| 767 | const s = snapshot(); |
| 768 | try { |
| 769 | write(true); |
| 770 | write(this.map); |
| 771 | } catch (_err) { |
| 772 | rollback(s); |
| 773 | |
| 774 | // Try to serialize each item on it's own |
| 775 | write(false); |
| 776 | for (const [key, value] of this.map) { |
| 777 | const s = snapshot(); |
| 778 | try { |