在Node.js應(yīng)用中讀寫Redis數(shù)據(jù)庫的簡單方法
在開始本文之前請(qǐng)確保安裝好 Redis 和 Node.js 以及 Node.js 的 Redis 擴(kuò)展 —— node_redis
首先創(chuàng)建一個(gè)新文件夾并新建文本文件 app.js 文件內(nèi)容如下:
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()); }); }
當(dāng)連接到 Redis 后會(huì)調(diào)用 runSample 函數(shù)并設(shè)置一個(gè)值,緊接著便讀出該值,運(yùn)行的結(jié)果如下:
OK Hello World
我們也可以使用 EXPIRE 命令來設(shè)置對(duì)象的失效時(shí)間,代碼如下:
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); }
注意: 上述使用的定時(shí)器只是為了演示 EXPIRE 命令,你必須在 Node.js 項(xiàng)目中謹(jǐn)慎使用定時(shí)器。
運(yùn)行上述程序的輸出結(jié)果是:
Reply: OK I live: Hello World I live: Hello World I live: Hello World I expired
接下來我們檢查一個(gè)值在失效之前存留了多長時(shí)間:
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); }
運(yùn)行結(jié)果:
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
相關(guān)文章
基于 Docker 開發(fā) NodeJS 應(yīng)用
這是兩篇文章的第一篇。本文涵蓋了有關(guān)在使用 Express 框架開發(fā)一個(gè)Node應(yīng)用時(shí),用Docker 替代 Vagrant 的比較詳細(xì)的教程, 應(yīng)用將使用 connect-redis 中間件將會(huì)話信息持久化到Redis中. 第二篇文章將介紹到將這個(gè)開發(fā)的設(shè)置產(chǎn)品化.2014-07-07node.js WEB開發(fā)中圖片驗(yàn)證碼的實(shí)現(xiàn)方法
這篇文章主要介紹了node.js WEB開發(fā)中圖片驗(yàn)證碼的實(shí)現(xiàn)方法,使用ccap模塊實(shí)現(xiàn),需要的朋友可以參考下2014-06-06nodejs連接mongodb數(shù)據(jù)庫實(shí)現(xiàn)增刪改查
本篇文章主要結(jié)合了nodejs操作mongodb數(shù)據(jù)庫實(shí)現(xiàn)增刪改查,包括對(duì)數(shù)據(jù)庫的增加,刪除,查找和更新,有興趣的可以了解一下。2016-12-12詳解如何使用node.js的開發(fā)框架express創(chuàng)建一個(gè)web應(yīng)用
這篇文章主要介紹了詳解如何使用node.js的開發(fā)框架express創(chuàng)建一個(gè)web應(yīng)用,網(wǎng)上各種搜索后,整理了下快速搭建express框架的步驟。非常具有實(shí)用價(jià)值,需要的朋友可以參考下2018-12-12基于NodeJS開發(fā)釘釘回調(diào)接口實(shí)現(xiàn)AES-CBC加解密
這篇文章主要介紹了基于NodeJS開發(fā)釘釘回調(diào)接口 實(shí)現(xiàn)AES-CBC加解密,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08