node.js中 redis 的安裝和基本操作示例
本文實例講述了node.js中 redis 的安裝和基本操作。分享給大家供大家參考,具體如下:
一、win下安裝redis
https://github.com/MicrosoftArchive/redis/releases
下載Redis-x64-3.2.100.zip,然后解壓,放到自定義目錄。
然后打開命令行工具,進(jìn)入到該目錄下,運(yùn)行安裝redis服務(wù)。
redis-server.exe --service-install redis.windows-service.conf --loglevel verbose
然后就可以啟動redis服務(wù)了
redis-server --service-start
二、redis可視化工具RedisDesktopManager
https://github.com/uglide/RedisDesktopManager/releases
現(xiàn)在已經(jīng)不免費(fèi)了,可以下載早期版本。
三、redis的數(shù)據(jù)類型
1、字符串,最基本的類型,一個key對應(yīng)一個value。
//設(shè)置值 set name xiaoxu //獲取值 get name //獲取子字符串,包含開始和結(jié)束索引的字符 getrange name 0 -1 getrange name 1 3 //自增加1 set age 1 incr age //指定增加的數(shù)量 incrby age 5 //遞減1 decr age //指定遞減的數(shù)量 decrby age 3 //刪除指定的鍵 del age //判斷鍵是否存在 exists name //設(shè)置過期時間,單位秒 expire name 10 //查看剩余生存時間 ttl name //獲取鍵的值類型 type name
2、哈希值,是一個鍵值對的集合,一個字符串類型的field和value的映射表,適合存儲對象
//設(shè)置單個值 hset person name xiao //設(shè)置多個值 hmset person age 24 height 172 //獲取單個值 hget person name //獲取多個值 hmget person age height //獲取所有值 hgetall person //刪除鍵 hdel person name //獲取所有的鍵 hkeys person
3、列表,簡單的字符串列表,按插入順序排序。
//往列表左邊插入 lpush list 1 lpush list 2 //往列表右邊插入 rpush list 3 rpush list 4 //查看列表元素 lrange list 0 -1 //彈出元素 lpop list rpop list //通過索引獲取元素 lindex list 1 //獲取列表的長度 llen list //刪除列表的元素 //lrem key count value // count > 0時,從表頭開始搜索,刪除與value相等的元素,數(shù)量為count // count < 0時,從表尾開始搜索,刪除與value相等的元素,數(shù)量為count絕對值 // count = 0時,刪除列表中所有與value相等的元素 lrem list 1 1 lrem list -1 2
4、集合,是字符串類型的無序集合
//添加元素 sadd label 1 2 3 //查看集合 smembers label //獲取集合個數(shù) scard label //刪除元素 srem label 2 //交集 sadd a 1 2 3 sadd b 2 3 4 sinter a b //差集 sdiff a b //并集 sunion a b
5、有序集合,跟集合一樣也是字符串的集合,不過每個元素會關(guān)聯(lián)一個double類型的分?jǐn)?shù),redis通過該分?jǐn)?shù)給集合中的元素進(jìn)行從小到大的排序。
//添加有序成員 zadd xiaoxu 60 math 77 english 80 chinaese //獲取有序成員數(shù)量 zcard xiaoxu //查看有序集合 zrange xiaoxu 0 -1 //查看有序集合,顯示分?jǐn)?shù) zrange xiaoxu 0 -1 withscores //刪除有序集合中的成員 zrem xiaoxu math
四、node.js中使用redis
安裝redis庫
npm install redis --save
操作redis的方法與我們在命令行中輸入的命令基本一致
const redis = require('redis');
//創(chuàng)建一個redis客戶端
let client = redis.createClient(6379, '127.0.0.1');
//操作redis基本跟在命令行操作一致
client.set('name', 'xiaoxu', function (err, result) {
if (err) {
console.log(err);
}
console.log(result);
});
client.hmset('person', 'name', 'xiaoxu', 'age', '25', function (err, result) {
if (err) {
console.log(err);
}
console.log(result);
});
client.hmget('person', 'name', 'age', function (err, result) {
if (err) {
console.log(err);
}
console.log(result);
});
client.hkeys('person', function (err, result) {
if (err) {
console.log(err);
}
result.forEach(function (value) {
client.hget('person', value, function (err, result) {
console.log(value, result);
});
});
//退出
client.quit();
});
通過bluebird來包裝redis,讓它支持async,await的方式,解決多層嵌套問題。
const redis = require('redis');
const bluebird = require('bluebird');
//通過bluebird包裝
bluebird.promisifyAll(redis.RedisClient.prototype);
bluebird.promisifyAll(redis.Multi.prototype);
//創(chuàng)建一個redis客戶端
let client = redis.createClient(6379, '127.0.0.1');
(async function () {
//方法名后面都加上Async
let result = await client.setAsync('name', 'hehe');
console.log(result);
result = await client.hmsetAsync('person', 'name', 'xiaoxu', 'age', '25');
console.log(result);
result = await client.hkeysAsync('person');
console.log(result);
result.forEach(async function (value) {
let v = await client.hgetAsync('person', value);
console.log(value, v);
});
client.quit();
})();
五、redis發(fā)布與訂閱
redis發(fā)布訂閱是一種消息通信模式,發(fā)送者發(fā)送消息,訂閱者接收消息。
const redis = require('redis');
let clientA = redis.createClient(6379, '127.0.0.1');
let clientB = redis.createClient(6379, '127.0.0.1');
//客戶端A訂閱頻道
clientA.subscribe('news');
clientA.subscribe('sports');
//客戶端A監(jiān)聽消息
clientA.on('message', function (channel, message) {
console.log('客戶端A收到', channel, message);
//客戶端A在10秒后取消訂閱
setTimeout(function () {
clientA.unsubscribe('news');
}, 10000);
});
setInterval(function () {
clientB.publish('news', '這是一條新聞' + new Date().toLocaleString());
clientB.publish('sports', '這是一條體育' + new Date().toLocaleString());
}, 1000);
六、redis事務(wù)
redis事務(wù)可以一次性執(zhí)行多個命令,multi 命令之后,exec命令之前,命令都會放到隊列中,直到執(zhí)行exec,將會執(zhí)行隊列中的命令。
discard可以取消事務(wù),放棄執(zhí)行事務(wù)塊內(nèi)的所有命令。
const redis = require('redis');
let client = redis.createClient(6379, '127.0.0.1');
client.multi()
.hset('person', 'name', 'haohao')
.hset('person', 'age', '34')
.exec(function (err, result) {
if (err) {
console.log(err);
}
console.log(result);
client.quit();
});
注意redis中的事務(wù)跟mysql中的事務(wù)是有區(qū)別的。
希望本文所述對大家node.js程序設(shè)計有所幫助。
相關(guān)文章
node.js中使用ejs渲染數(shù)據(jù)的代碼實現(xiàn)
這篇文章主要介紹了node.js中使用ejs渲染數(shù)據(jù),本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2023-11-11
Dapr+NestJs編寫Pub及Sub裝飾器實戰(zhàn)示例
這篇文章主要為大家介紹了Dapr+NestJs編寫Pub及Sub裝飾器的實戰(zhàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
使用PM2實現(xiàn)高效的應(yīng)用監(jiān)控與管理功能
PM2?是一個流行的進(jìn)程管理器,用于?Node.js?應(yīng)用程序,它支持應(yīng)用程序的負(fù)載均衡、自動重啟、日志管理、監(jiān)控以及多環(huán)境管理等功能,本文給大家介紹了如何使用PM2實現(xiàn)高效的應(yīng)用監(jiān)控與管理功能,需要的朋友可以參考下2024-02-02
npm安裝windows-build-tools卡在Successfully?installed?Python2.7
這篇文章主要介紹了npm安裝windows-build-tools卡在Successfully?installed?Python2.7的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10
node.js開發(fā)中使用Node Supervisor實現(xiàn)監(jiān)測文件修改并自動重啟應(yīng)用
這篇文章主要介紹了node.js開發(fā)中使用Node Supervisor實現(xiàn)監(jiān)測文件修改并自動重啟應(yīng)用的功能,從而避免大量重復(fù)的CTRL+C終止程序動作,需要的朋友可以參考下2014-11-11

