麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 語言 > JavaScript > 正文

在Node.js應用中讀寫Redis數據庫的簡單方法

2024-05-06 16:22:31
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了在Node.js應用中讀寫Redis數據庫的簡單方法,Redis是一個內存式高速數據庫,需要的朋友可以參考下

在開始本文之前請確保安裝好 Redis 和 Node.js 以及 Node.js 的 Redis 擴展 ——node_redis

首先創建一個新文件夾并新建文本文件 app.js 文件內容如下:

 

 
  1. var redis = require("redis"
  2. , client = redis.createClient(); 
  3.  
  4. client.on("error"function (err) { 
  5. console.log("Error " + err); 
  6. }); 
  7.  
  8. client.on("connect", runSample); 
  9.  
  10. function runSample() { 
  11. // Set a value 
  12. client.set("string key""Hello World"function (err, reply) { 
  13. console.log(reply.toString()); 
  14. }); 
  15. // Get a value 
  16. client.get("string key"function (err, reply) { 
  17. console.log(reply.toString()); 
  18. }); 

當連接到 Redis 后會調用 runSample 函數并設置一個值,緊接著便讀出該值,運行的結果如下:

 

 
  1. OK 
  2. Hello World 

我們也可以使用 EXPIRE 命令來設置對象的失效時間,代碼如下:

 

 
  1. var redis = require('redis'
  2. , client = redis.createClient(); 
  3.  
  4. client.on('error'function (err) { 
  5. console.log('Error ' + err); 
  6. }); 
  7.  
  8. client.on('connect', runSample); 
  9.  
  10. function runSample() { 
  11. // Set a value with an expiration 
  12. client.set('string key''Hello World', redis.print); 
  13. // Expire in 3 seconds 
  14. client.expire('string key', 3); 
  15.  
  16. // This timer is only to demo the TTL 
  17. // Runs every second until the timeout 
  18. // occurs on the value 
  19. var myTimer = setInterval(function() { 
  20. client.get('string key'function (err, reply) { 
  21. if(reply) { 
  22. console.log('I live: ' + reply.toString()); 
  23. else { 
  24. clearTimeout(myTimer); 
  25. console.log('I expired'); 
  26. client.quit(); 
  27. }); 
  28. }, 1000); 

注意: 上述使用的定時器只是為了演示 EXPIRE 命令,你必須在 Node.js 項目中謹慎使用定時器。

運行上述程序的輸出結果是:

 

 
  1. Reply: OK 
  2. I live: Hello World 
  3. I live: Hello World 
  4. I live: Hello World 
  5. I expired 

接下來我們檢查一個值在失效之前存留了多長時間:

 

 
  1. var redis = require('redis'
  2. , client = redis.createClient(); 
  3.  
  4. client.on('error'function (err) { 
  5. console.log('Error ' + err); 
  6. }); 
  7.  
  8. client.on('connect', runSample); 
  9.  
  10. function runSample() { 
  11. // Set a value 
  12. client.set('string key''Hello World', redis.print); 
  13. // Expire in 3 seconds 
  14. client.expire('string key', 3); 
  15.  
  16. // This timer is only to demo the TTL 
  17. // Runs every second until the timeout 
  18. // occurs on the value 
  19. var myTimer = setInterval(function() { 
  20. client.get('string key'function (err, reply) { 
  21. if(reply) { 
  22. console.log('I live: ' + reply.toString()); 
  23. client.ttl('string key', writeTTL); 
  24. else { 
  25. clearTimeout(myTimer); 
  26. console.log('I expired'); 
  27. client.quit(); 
  28. }); 
  29. }, 1000); 
  30.  
  31. function writeTTL(err, data) { 
  32. console.log('I live for this long yet: ' + data); 

運行結果:

 

 
  1. Reply: OK 
  2. I live: Hello World 
  3. I live for this long yet: 2 
  4. I live: Hello World 
  5. I live for this long yet: 1 
  6. I live: Hello World 
  7. I live for this long yet: 0 
  8. I expired 
 

 

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 欧美日韩夜夜 | 毛片视频在线免费观看 | 欧美一级免费视频 | 国av在线| 成人毛片视频在线播放 | 欧美伦交 | 中文字幕国产一区 | 精品国产一区二区三区久久久蜜月 | 永久免费毛片 | 久久人人人| 成人wxx视频免费 | 亚洲福利在线视频 | 操碰网| 亚州成人在线观看 | 成年人激情在线 | 手机av免费电影 | 久久99精品久久久久久小说 | 在线看国产视频 | 欧美黄色视屏 | 毛片视频播放 | 日本在线不卡一区二区三区 | www.成人在线视频 | a级在线 | 一本色道久久99精品综合蜜臀 | 精品国产乱码久久久久久久 | 成年毛片 | 成人在线视频精品 | 欧美成人午夜 | 久久精品视频亚洲 | 亚洲精品欧美在线 | 免费看黄色一级大片 | 久久久久电影网站 | 久久久久久久久久久久久国产精品 | 成人福利在线免费观看 | 男女隐私免费视频 | 一级大片久久 | 丁香天堂网 | 亚洲成人高清在线观看 | 久久久久久免费 | 操你逼| 成人444kkkk在线观看 |