# Redis ## 1. 本地安裝Redis ### 1.1、安裝地址 - Redis 官網:**[redis.io/download](http://redis.io/download)** (用cmd做) - Redis github :**[github.com/MSOpenTech/…](https://github.com/MSOpenTech/redis/tags)** ### 1.2、安装流程 < 用github做示範 > - 下載[win-3.2.100版本],[Redis-x64] ![](https://i.imgur.com/Ht2cQ2u.png) - 安裝完成後,進入此資料夾內,打開redis-server.exe,開啟server ![](https://i.imgur.com/SHoM4qt.png) - 打開此資料夾內 redis-cli.exe,出現連線,輸入ping,跑出pong,表示連接成功 ![](https://i.imgur.com/8av7CzK.png) - 若有密碼 `> auth 密碼` ## 2. VSCode 連線 ```javascript= const redis = require("redis"); var client = redis.createClient(6379, '127.0.0.1'); // 若有密碼(port,IP, { auth_pass: redisPassword }) //若在本地做,可以不放參數 client.on("error", function(error) { console.error(error); }); ``` ## 3. 操作 - 取值方式:自動帶兩個參數 client.get("key1",(err,value)=>console.log(value)) 可只寫 client.get("key1",console.log) 會照順序印出err、value的值 - 只能存string或JSON格式 - 所以存進需要JSON.stringify({key:'val'}) - 取得JSON.parse(val) - 刪除全部:client.flushall() ### 3-1 SET and GET 最簡易可放進資料的方式 ```javascript= client.set("key1",123,redis.print); client.get("key1",redis.print); ``` ``` output: Reply: OK Reply:123 ``` ### 3-2 DEL 刪除 ```javascript= client.del("key1",redis.print); ``` key1這個鍵的資料被刪除 ``` output: Reply: null ``` ### 3-3 INCR and DECR 做資料的加減 (以下都做redis.print) ```javascript= client.set("counter",50); client.incr("counter"); client.decr("counter"); ``` ``` output: Reply: 50 Reply: 51 Reply: 50 ``` ### 3-4 Set 過期時間 ```javascript= client.set("test","val","EX",5); //5秒後資料刪除 client.get("test"); //5秒後跑get會Reply:null ``` ### 3-5 Lists 有順序性的資料 ```javascript= client.rpush("list1","t1","t2","t3"); //向右邊push資料進去 client.lrange("list1",0,-1); //由左邊拿資料,0代表資料開始,-1代表end of this list client.rpop("list1") //從右邊把資料取出後丟掉 client.lrange("list1",0,-1); ``` ``` output: Reply: 3 Reply: t1,t2,t3 Reply: t3 Reply:t1,t2 ``` - 可用來實現Queues、Stacks - Queues:lpush+rpop - Stacks:rpush+rpop ### 3-6 Hashmaps 可回傳一個物件 如同一個主key可對應多個key,value ```javascript= client.hset("formname-1","name","Jack","type","student","age",20); //第一個此hashmap名字,後面以key,value依序寫入 client.hgetall("formname-1"); //回傳物件{ // "name" : "Jack", // "type" : "student", // "age" : 20 //} client.hget("formname-1","name"); //可依照key名字單獨取出值 ``` ``` output: Reply: 0 Reply: [object,object] Reply: Jack ``` ### 3-7 Sorted Set 可做排序 ```javascript= client.zadd("score",100,"palyer1"); client.zadd("score",120,"palyer2"); client.zadd("score",90,"palyer3"); ``` ``` output: Reply: 0 Reply: 0 Reply: 0 ``` - 排序 ```javascript= client.zrange("score",0,-1); //低-->高 client.zrevrange("score",0,-1); //高-->低 ``` ``` output: Reply: palyer3,palyer1,palyer2 Reply: palyer2,palyer1,palyer3 ``` ## 4. Linux 雲端上安裝 - Redis 官網: [redis.io/download](http://redis.io/download) ```cmake= $ wget https://download.redis.io/releases/redis-6.0.9.tar.gz ``` 在官網找最新版替代 wget後面的網址 ```cmake= $ tar xzf redis-6.0.9.tar.gz $ cd redis-6.0.9 ``` 解壓縮下載下來的這個檔案後,進入解壓縮完的資料夾裡 ```cmake= $ cd src $ make ``` 進到src資料夾,下make指令後會出現redis-server、redis-cli - 失敗紀錄: 無法跑make指令,出現 ``` Command 'make' not found, but can be installed with: apt install make apt install make-guile Ask your administrator to install one of them. ``` 無論加sudo 跑哪一個指令,都出現 ``` E: Could not open lock file /var/lib/dpkg/lock-front E: Unable to acquire the dpkg frontend lock (/var/li ``` 網路上有不同解法: [安裝包下載有誤](https://www.cnblogs.com/zdd-java/p/10288734.html) [安裝gcc環境](https://zhuanlan.zhihu.com/p/149133500) [分配器allocator沒有libc](https://www.cnblogs.com/liu2-/p/6914159.html) 這個指令的問題是,make這個指令未下載,要install時又沒有權限 因此開全部權限,先將make install 即可續跑 ![](https://i.imgur.com/ur1bq48.png) 最後執行 ```cmake= $ src/redis-server ``` ![](https://i.imgur.com/DaMKmAl.png) 6. 安裝h top [Linux htop 系統程序監控工具簡介](https://matthung0807.blogspot.com/2019/06/linux-htop.html) [htop背景執行](https://medium.com/10coding/node-js-%E4%BD%BF%E7%94%A8-redis-%E5%85%A7%E5%AD%98%E4%BE%86%E5%AD%98%E5%8F%96%E6%9C%AC%E5%9C%B0%E8%B3%87%E6%96%99-9557660196f4) 背景執行 ``` $ src/redis-server --daemonize yes ``` 停止 ``` $ pkill -u $USER redis-server ``` ## 參考資料: - [Node.js 系列:Redis 的使用](https://juejin.cn/post/6844903806266785805) - [Use Redis with NodeJS (REDIS CRASH COURSE PART 3 OF 3)](https://www.youtube.com/watch?v=u0_w7McIzFE) - [Redis 教程](https://www.runoob.com/redis/redis-tutorial.html) - [redis客户端连接(error) NOAUTH Authentication required](https://blog.csdn.net/leisure_life/article/details/78460733)