溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么在nodejs中應(yīng)用redis數(shù)據(jù)庫

發(fā)布時(shí)間:2021-01-16 10:53:01 來源:億速云 閱讀:252 作者:Leah 欄目:web開發(fā)

怎么在nodejs中應(yīng)用redis數(shù)據(jù)庫?針對這個(gè)問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡單易行的方法。

redis是一個(gè)性能非常好的內(nèi)存數(shù)據(jù)庫,部署在應(yīng)用程序和mysql數(shù)據(jù)中間做緩存數(shù)據(jù)庫,可以極大的提升應(yīng)用程序的性能,這里簡單介紹nodejs客戶端操作redis的demo程序

redis里面總共可以存儲5種數(shù)據(jù)類型,分別是字符串,列表、集合、三列、有序集合;這里將會對這5種數(shù)據(jù)類型的增刪查改一一處理;

1、redis在mac上的安裝:

https://redis.io/download,當(dāng)前我用的版本穩(wěn)定版本是4.0.9,解壓之后,進(jìn)入redis-4.0.9目錄,執(zhí)行make && sudo make install,稍等幾分鐘就可以安裝好;

2、redis啟動:

命令行執(zhí)行 redis-server即可啟動,默認(rèn)端口是6379;

3、安裝nodejs客戶端:

創(chuàng)建redis-node目錄,在該目錄下yarn init -y之后,執(zhí)行命令:yarn add redis 即可安裝nodejs的redis客戶端,參考文檔:https://github.com/NodeRedis/node_redis

4、在redis-node目錄下,終端上執(zhí)行node,即可在終端上響應(yīng)式的執(zhí)行nodejs代碼,用做測試,下面開始demo程序

首先要創(chuàng)建客戶端,并連接redis服務(wù)器,在執(zhí)行以下連接客戶端代碼之前,請確保已經(jīng)運(yùn)行了redis服務(wù)器:終端商執(zhí)行redis-server即可,默認(rèn)端口6379;

const redis = require('redis');
const client = redis.createClient(); //默認(rèn)連接localhost:6379,具體配置參數(shù)可以參考文檔https://github.com/NodeRedis/node_redis

如果一切順利,我們就已經(jīng)創(chuàng)建好了連接redis服務(wù)器的客戶端,后續(xù)操作都是在client對象上進(jìn)行。

一、字符串類型

雖然說是字符串類型,但是可以存儲的數(shù)據(jù)包括字符串、整數(shù)以及浮點(diǎn)數(shù)。

var res = client.set('name', 'abczhijia', (err, data) => {
 console.log('err: ', err, ' data: ', data);
}); // err: null data: OK,res的值是true

client.get('name', (err, data) => {
 console.log('err: ', err, ' data: ', data);
}); // err: null data: abczhijia

為了簡單起見,我們定義一個(gè)回調(diào)函數(shù),用于輸出數(shù)據(jù):

const cb = (err, data) => {
 console.log('err: ', err, ' data: ', data, ' data type: ', typeof data);
}

下面再針對整數(shù)做一個(gè)測試:

client.set('age', 20, cb); //err: null data: OK data type: string
client.get('age', cb); //err: null data: 20 data type: string

可以看出,雖然設(shè)置的是整數(shù),輸出來的時(shí)候,其實(shí)還是字符串,所以如果要進(jìn)行計(jì)算,需要自己在回調(diào)函數(shù)里面做轉(zhuǎn)換

二、列表數(shù)據(jù)類型

//從右側(cè)推入
client.rpush('friends', 'mike', 'jhon', cb); //err: null data: 2 data type: number
client.lrange('friends', 0, -1, cb); //err: null data: [ 'mike', 'jhon' ] data type: object

//從左側(cè)推入
client.lpush('friends', 'sam', 'bob', cb); //err: null data: 4 data type: number
client.lrange('friends', 0, -1, cb); // err: null data: [ 'bob', 'sam', 'mike', 'jhon' ] data type: object

//從右側(cè)彈出
client.rpop('friends', cb); //err: null data: jhon data type: string
//從左側(cè)彈出
client.lpop('friends', cb); //err: null data: bob data type: string
//打印看看發(fā)生了啥
client.lrange('friends', 0, -1, cb); // err: null data: [ 'sam', 'mike' ] data type: object

//查看索引位置的值
client.lindex('friends', 0, cb); // err: null data: sam data type: string

//對列表進(jìn)行裁剪
client.rpush('friends', 'tom', 'bryant', cb)// err: null data: 4 data type: number
client.ltrim('friends', 1, 2, cb); //err: null data: OK data type: string
client.lrange('friends', 0, -1, cb); //err: null data: [ 'mike', 'tom' ] data type: object

這里注意,列表的操作可以從右邊rpush推入一個(gè)或者多個(gè)數(shù)據(jù),也可以從左邊lpush推入一個(gè)或多個(gè)數(shù)據(jù);另外,取值的時(shí)候,需要指明需要起止位置,如果要獲取整個(gè),可以把結(jié)束位置寫成-1。

三、集合數(shù)據(jù)類型

//往集合ids中加幾個(gè)元素
client.sadd('ids', 1, 2, cb); //err: null data: 2 data type: number
//查看集合元素
client.smembers('ids', cb); //err: null data: [ '1', '2' ] data type: object
//從集合中刪除元素
client.srem('ids', 2, cb); // err: null data: 1 data type: number
//看看發(fā)生了啥
client.smembers('ids', cb); //err: null data: [ '1' ] data type: object
//看看集合有多少個(gè)元素
client.scard('ids', cb); //err: null data: 1 data type: number
//再加幾個(gè)元素進(jìn)去
client.sadd('ids', 3, 5, 8, 9); //
//判斷元素是否在集合內(nèi)
client.sismember('ids', 8, cb); // err: null data: 1 data type: number
client.sismember('ids', 80, cb); //err: null data: 0 data type: number

四、散列數(shù)據(jù)類型

//往散列上添加多組鍵值對
client.hmset('phone', 'price', 5888, 'name', 'iphonex', cb); //err: null data: OK data type: string
//查看多個(gè)鍵的值
client.hmget('phone', 'price', 'name', cb); //err: null data: [ '5888', 'iphonex' ] data type: object
//查看鍵值對的數(shù)量
client.hlen('phone', cb); //err: null data: 2 data type: number
//刪掉其中一個(gè)鍵值對
client.hdel('phone', 'price', cb); //err: null data: 1 data type: number
//看看price是否還在?
client.hmget('phone', 'price', cb); //err: null data: [ null ] data type: object,原來只留下了null
//再加幾個(gè)屬性
client.hmset('phone', 'vendor', 'apple', 'madein', 'china', cb);
//取出所有的鍵值對
client.hgetall('phone', cb); //err: null data: { name: 'iphonex', vendor: 'apple', madein: 'china' } data type: object
//取出所有的鍵
client.hkeys('phone', cb); //err: null data: [ 'name', 'vendor', 'madein' ] data type: object
//取出所有的值
client.hvals('phone', cb); //err: null data: [ 'iphonex', 'apple', 'china' ] data type: object
//判斷鍵是否存在
client.hexists('phone', 'name', cb); //err: null data: 1 data type: number
client.hexists('phone', 'price', cb); //err: null data: 0 data type: number

關(guān)于怎么在nodejs中應(yīng)用redis數(shù)據(jù)庫問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI