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

首頁 > 語言 > JavaScript > 正文

在Node.js應(yīng)用中讀寫Redis數(shù)據(jù)庫的簡單方法

2024-05-06 16:22:31
字體:
供稿:網(wǎng)友

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

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

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

 

 
  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 后會調(diào)用 runSample 函數(shù)并設(shè)置一個值,緊接著便讀出該值,運行的結(jié)果如下:

 

 
  1. OK 
  2. Hello World 

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

 

 
  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 項目中謹慎使用定時器。

運行上述程序的輸出結(jié)果是:

 

 
  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); 

運行結(jié)果:

 

 
  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 
 

 

 


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

圖片精選

主站蜘蛛池模板: 绵阳市| 观塘区| 澎湖县| 林西县| 宁陵县| 黑山县| 乐清市| 奉新县| 西华县| 木兰县| 长子县| 霍州市| 烟台市| 兴仁县| 浦东新区| 松江区| 广宁县| 铜陵市| 平潭县| 香格里拉县| 林州市| 金阳县| 天等县| 佛山市| 红安县| 满城县| 潮州市| 舟山市| 都匀市| 明星| 云浮市| 鄯善县| 宁远县| 大竹县| 河池市| 德保县| 浑源县| 垦利县| 孝义市| 安溪县| 金阳县|