ホームページ  >  記事  >  ウェブフロントエンド  >  Node.js で Redis データベースを読み書きする簡単な方法 application_node.js

Node.js で Redis データベースを読み書きする簡単な方法 application_node.js

WBOY
WBOYオリジナル
2016-05-16 15:52:192292ブラウズ

この記事を始める前に、Redis と Node.js、および Node.js 用の Redis 拡張機能 - node_redis

を必ずインストールしてください。

まず、新しいフォルダーを作成し、新しいテキスト ファイル app.js を作成します。ファイルの内容は次のとおりです。

var redis = require("redis")
  , client = redis.createClient();
 
client.on("error", function (err) {
  console.log("Error " + err);
});
 
client.on("connect", runSample);
 
function runSample() {
  // Set a value
  client.set("string key", "Hello World", function (err, reply) {
    console.log(reply.toString());
  });
  // Get a value
  client.get("string key", function (err, reply) {
    console.log(reply.toString());
  });
}

Redis に接続すると、runSample 関数が呼び出され、値が設定され、その後、値が読み取られます。操作の結果は次のとおりです。

OK
Hello World

EXPIRE コマンドを使用してオブジェクトの有効期限を設定することもできます。コードは次のとおりです。

var redis = require('redis')
  , client = redis.createClient();
 
client.on('error', function (err) {
  console.log('Error ' + err);
});
 
client.on('connect', runSample);
 
function runSample() {
  // Set a value with an expiration
  client.set('string key', 'Hello World', redis.print);
  // Expire in 3 seconds
  client.expire('string key', 3);
 
  // This timer is only to demo the TTL
  // Runs every second until the timeout
  // occurs on the value
  var myTimer = setInterval(function() {
    client.get('string key', function (err, reply) {
      if(reply) {
        console.log('I live: ' + reply.toString());
      } else {
        clearTimeout(myTimer);
        console.log('I expired');
        client.quit();
      }
    });
  }, 1000);
}

注: 上記で使用されているタイマーは、EXPIRE コマンドを説明するためのものです。Node.js プロジェクトでは注意してタイマーを使用する必要があります。

上記のプログラムを実行すると出力は次のようになります:


Reply: OK
I live: Hello World
I live: Hello World
I live: Hello World
I expired

次に、値が期限切れになるまでの持続時間を確認します。

var redis = require('redis')
  , client = redis.createClient();
 
client.on('error', function (err) {
  console.log('Error ' + err);
});
 
client.on('connect', runSample);
 
function runSample() {
  // Set a value
  client.set('string key', 'Hello World', redis.print);
  // Expire in 3 seconds
  client.expire('string key', 3);
 
  // This timer is only to demo the TTL
  // Runs every second until the timeout
  // occurs on the value
  var myTimer = setInterval(function() {
    client.get('string key', function (err, reply) {
      if(reply) {
        console.log('I live: ' + reply.toString());
        client.ttl('string key', writeTTL);
      } else {
        clearTimeout(myTimer);
        console.log('I expired');
        client.quit();
      }
    });
  }, 1000);
}
 
function writeTTL(err, data) {
  console.log('I live for this long yet: ' + data);
}

実行結果:

Reply: OK
I live: Hello World
I live for this long yet: 2
I live: Hello World
I live for this long yet: 1
I live: Hello World
I live for this long yet: 0
I expired


声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。