Maison  >  Article  >  interface Web  >  Méthode script.createCachedData() dans Node.js

Méthode script.createCachedData() dans Node.js

王林
王林avant
2023-09-04 21:21:081148parcourir

La méthode

Node.js 中的 script.createCachedData() 方法

script.createCachedData() permet de créer un cache de code qui sera utilisé avec l'option cachedData du constructeur de script. Ce cachedData peut être appelé plusieurs fois sans délai. Cette méthode est l'interface de programmation intégrée au module "Script".

Syntaxe

script.createCachedData()

Paramètres

car il met uniquement en cache les données. Il ne nécessite aucune intervention spécifique de la part de l'utilisateur. Il renvoie uniquement le tampon mis en cache.

Exemple

Créez un fichier appelé createCachedData.js et copiez l'extrait de code suivant. Après avoir créé le fichier, exécutez ce code à l'aide de la commande suivante, comme indiqué dans l'exemple ci-dessous -

node createCachedData.js

createCachedData.js

// Node.js program to demonstrate the flow of script.createCachedData() method

// Importing the vm module
const vm = require("vm");

// Defining the script as constant and usng the below function
// to create the cached data for the difference of two numbers.
const script = new vm.Script(`
   function add(a, b) {
      return a - b;
   }
   const x = add(2, 1);
`);

//Creating the cached data without caching the variable
const cacheWithoutx = script.createCachedData();
console.log(cacheWithoutx);

Output

C:\homeode>> node createCachedData.js
<Buffer b5 03 de c0 8a f4 d4 f4 3d 00 00 00 ff 03 00 00 d5 a2 f5 b7 06 00 00
00 00 00 00 00 28 02 00 00 8f 87 4d e3 59 55 98 f9 00 00 00 80 20 00 00 80 00
03 ... >

Example

Voyons un autre exemple.

// Node.js program to demonstrate the flow of script.createCachedData() method

// Importing the vm module
const vm = require("vm");

// Defining the script as constant and usng the below function
// to create the cached data for the difference of two numbers.
const script = new vm.Script(`
   function add(a, b) {
      return a - b;
   }
   const x = add(2, 1);
`);

// Calling the runInThisContext from script module
script.runInThisContext();

//Creating the cached data along with caching the variable
const cacheWithx = script.createCachedData();
console.log(cacheWithx);

sortie

C:\homeode>> node createCachedData.js
<Buffer b5 03 de c0 8a f4 d4 f4 3d 00 00 00 ff 03 00 00 d5 a2 f5 b7 06 00 00
00 00 00 00 00 00 03 00 00 15 80 fd 5d 69 21 3a a9 00 00 00 80 20 00 00 80 38
04 ... >

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Cet article est reproduit dans:. en cas de violation, veuillez contacter admin@php.cn Supprimer