詳解redis在nodejs中的應用
redis是一個性能非常好的內(nèi)存數(shù)據(jù)庫,部署在應用程序和mysql數(shù)據(jù)中間做緩存數(shù)據(jù)庫,可以極大的提升應用程序的性能,這里簡單介紹nodejs客戶端操作redis的demo程序
redis里面總共可以存儲5種數(shù)據(jù)類型,分別是字符串,列表、集合、三列、有序集合;這里將會對這5種數(shù)據(jù)類型的增刪查改一一處理;
1、redis在mac上的安裝:
https://redis.io/download,當前我用的版本穩(wěn)定版本是4.0.9,解壓之后,進入redis-4.0.9目錄,執(zhí)行make && sudo make install,稍等幾分鐘就可以安裝好;
2、redis啟動:
命令行執(zhí)行 redis-server即可啟動,默認端口是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,即可在終端上響應式的執(zhí)行nodejs代碼,用做測試,下面開始demo程序
首先要創(chuàng)建客戶端,并連接redis服務器,在執(zhí)行以下連接客戶端代碼之前,請確保已經(jīng)運行了redis服務器:終端商執(zhí)行redis-server即可,默認端口6379;
const redis = require('redis');
const client = redis.createClient(); //默認連接localhost:6379,具體配置參數(shù)可以參考文檔https://github.com/NodeRedis/node_redis
如果一切順利,我們就已經(jīng)創(chuàng)建好了連接redis服務器的客戶端,后續(xù)操作都是在client對象上進行。
一、字符串類型
雖然說是字符串類型,但是可以存儲的數(shù)據(jù)包括字符串、整數(shù)以及浮點數(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
為了簡單起見,我們定義一個回調(diào)函數(shù),用于輸出數(shù)據(jù):
const cb = (err, data) => {
console.log('err: ', err, ' data: ', data, ' data type: ', typeof data);
}
下面再針對整數(shù)做一個測試:
client.set('age', 20, cb); //err: null data: OK data type: string
client.get('age', cb); //err: null data: 20 data type: string
可以看出,雖然設置的是整數(shù),輸出來的時候,其實還是字符串,所以如果要進行計算,需要自己在回調(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
//對列表進行裁剪
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推入一個或者多個數(shù)據(jù),也可以從左邊lpush推入一個或多個數(shù)據(jù);另外,取值的時候,需要指明需要起止位置,如果要獲取整個,可以把結(jié)束位置寫成-1。
三、集合數(shù)據(jù)類型
//往集合ids中加幾個元素
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
//看看集合有多少個元素
client.scard('ids', cb); //err: null data: 1 data type: number
//再加幾個元素進去
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
//查看多個鍵的值
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
//刪掉其中一個鍵值對
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
//再加幾個屬性
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
五、有序集合數(shù)據(jù)類型
有序集合會復雜一點,但是可以完成很好的應用程序效果,源碼地址:https://github.com/abczhijia/redis-node
相關文章
node vue項目開發(fā)之前后端分離實戰(zhàn)記錄
其實基于vue.js+node.js構(gòu)建的開源博客系統(tǒng)有很多,下面這篇文章主要給大家介紹了關于node vue項目開發(fā)之前后端分離的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友下面來一起看看吧。2017-12-12
nodejs抓取notion?emoji?svg資源的腳本示例
這篇文章主要為大家介紹了nodejs抓取notion?emoji?svg資源腳本實現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02

