国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 網(wǎng)站 > 建站經(jīng)驗(yàn) > 正文

在Node.js應(yīng)用中使用Redis-的方法簡(jiǎn)介

2019-11-02 14:52:10
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

   在開(kāi)始本文之前請(qǐng)確保安裝好 Redis 和 Node.js 以及 Node.js 的 Redis 擴(kuò)展 —— node_redis

  首先創(chuàng)建一個(gè)新文件夾并新建文本文件 app.js 文件內(nèi)容如下:

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 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é)果如下:

  ?

1 2 OK Hello World

  我們也可以使用 EXPIRE 命令來(lái)設(shè)置對(duì)象的失效時(shí)間,代碼如下:

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 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); }
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 宿松县| 平邑县| 诸暨市| 大英县| 北辰区| 闵行区| 沙湾县| 乌拉特后旗| 清流县| 云阳县| 福海县| 烟台市| 新郑市| 松滋市| 合水县| 汉川市| 乌拉特中旗| 上虞市| 临潭县| 唐河县| 通化县| 新密市| 阆中市| 门源| 汤原县| 那曲县| 高碑店市| 郓城县| 方正县| 临朐县| 罗江县| 河南省| 黑河市| 鄯善县| 肃北| 揭阳市| 城市| 和林格尔县| 白银市| 昭通市| 成都市|