2 字符串String
生活随笔
收集整理的這篇文章主要介紹了
2 字符串String
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2.1 簡單動態字符串
Redis沒有直接使用C語言傳統的字符串表示,而是自己構建了一種名為簡單動態字符串(SDS)的抽象類型,并將SDS用作Redis的默認字符串表示。在Redis的數據庫里面,包含字符串的鍵值對在底層都是由SDS實現的。
例如創建命令:
> set msg “hello world” OKRedis將在數據庫中創建一個新的鍵值對,其中:
- 鍵值對的鍵是一個字符串對象,對象的底層實現是一個保存著字符串“msg”的SDS。
- 鍵值對的值也是一個字符串對象,對象的底層實現是一個保存著字符串“hello world”的SDS。
我們來看一下SDS的定義:
struct sdshdr{//記錄buf數組中已使用字節的數量 //等于SDS所保存字符串的長度 int len;//記錄buf數組未使用字節的數量 int free;//字節數組 char buf[];};? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
2.2 String常用命令
> set hello world //設置鍵為hello,值為world OK> get hello //獲取鍵為hello的值 "world"> getset hello xiaolin //獲取鍵為hello的值,并重新定義值為xiaolin "world" > get hello //重新獲取鍵為hello的值 "xiaolin" //此時已經變成了xiaolin> del hello //刪除 (integer) 1 > get hello (nil)>incr num //對一個值做加1操作,若該值之前沒有定義,則先初始化為0,之后加1 (integer) 1 >get num "1" >incr hello //若被增的值不是字符串,則會報錯 (error) ERR value is not an integer or out of range>decr num1 //減一操作,若先初始化為0,則會得到-1 (integer) -1>incrby num2 5 // 可以指定要增加的值 (integer) 5>decrby num2 3 // 可以指定要減小的值 (integer) 2>set hello xiaolin OK >append hello nihao //連接字符串操作 (integer) 12 >get hello "xiaolinnihao"?
轉載于:https://www.cnblogs.com/xlzfdddd/p/10343974.html
總結
以上是生活随笔為你收集整理的2 字符串String的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: redis雪崩解决方案
- 下一篇: 一张图搞定SDF的概念