最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

一文詳解GO如何實現(xiàn)Redis的AOF持久化

 更新時間:2023年03月27日 09:38:11   作者:CSGOPHER  
這篇文章主要為大家詳細介紹了GO如何實現(xiàn)Redis的AOF持久化的,文中的示例代碼講解詳細,具有一定的借鑒價值,感興趣的小伙伴可以了解一下

GO實現(xiàn)Redis的AOF持久化

將用戶發(fā)來的指令以RESP協(xié)議的形式存儲在本地的AOF文件,重啟Redis后執(zhí)行此文件恢復數(shù)據(jù)

https://github.com/csgopher/go-redis

本文涉及以下文件:redis.conf:配置文件

aof:實現(xiàn)aof

redis.conf

appendonly yes
appendfilename appendonly.aof

aof/aof.go

type CmdLine = [][]byte

const (
   aofQueueSize = 1 << 16
)

type payload struct {
   cmdLine CmdLine
   dbIndex int
}

type AofHandler struct {
   db          databaseface.Database
   aofChan     chan *payload
   aofFile     *os.File
   aofFilename string
   currentDB   int
}

func NewAOFHandler(db databaseface.Database) (*AofHandler, error) {
   handler := &AofHandler{}
   handler.aofFilename = config.Properties.AppendFilename
   handler.db = db
   handler.LoadAof()
   aofFile, err := os.OpenFile(handler.aofFilename, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0600)
   if err != nil {
      return nil, err
   }
   handler.aofFile = aofFile
   handler.aofChan = make(chan *payload, aofQueueSize)
   go func() {
      handler.handleAof()
   }()
   return handler, nil
}

func (handler *AofHandler) AddAof(dbIndex int, cmdLine CmdLine) {
   if config.Properties.AppendOnly && handler.aofChan != nil {
      handler.aofChan <- &payload{
         cmdLine: cmdLine,
         dbIndex: dbIndex,
      }
   }
}

func (handler *AofHandler) handleAof() {
   handler.currentDB = 0
   for p := range handler.aofChan {
      if p.dbIndex != handler.currentDB {
         // select db
         data := reply.MakeMultiBulkReply(utils.ToCmdLine("SELECT", strconv.Itoa(p.dbIndex))).ToBytes()
         _, err := handler.aofFile.Write(data)
         if err != nil {
            logger.Warn(err)
            continue
         }
         handler.currentDB = p.dbIndex
      }
      data := reply.MakeMultiBulkReply(p.cmdLine).ToBytes()
      _, err := handler.aofFile.Write(data)
      if err != nil {
         logger.Warn(err)
      }
   }
}

func (handler *AofHandler) LoadAof() {
   file, err := os.Open(handler.aofFilename)
   if err != nil {
      logger.Warn(err)
      return
   }
   defer file.Close()
   ch := parser.ParseStream(file)
   fakeConn := &connection.Connection{}
   for p := range ch {
      if p.Err != nil {
         if p.Err == io.EOF {
            break
         }
         logger.Error("parse error: " + p.Err.Error())
         continue
      }
      if p.Data == nil {
         logger.Error("empty payload")
         continue
      }
      r, ok := p.Data.(*reply.MultiBulkReply)
      if !ok {
         logger.Error("require multi bulk reply")
         continue
      }
      ret := handler.db.Exec(fakeConn, r.Args)
      if reply.IsErrorReply(ret) {
         logger.Error("exec err", err)
      }
   }
}
  • AofHandler:1.從管道中接收數(shù)據(jù) 2.寫入AOF文件
  • AddAof:用戶的指令包裝成payload放入管道
  • handleAof:將管道中的payload寫入磁盤
  • LoadAof:重啟Redis后加載aof文件

database/database.go

type Database struct {
   dbSet []*DB
   aofHandler *aof.AofHandler
}

func NewDatabase() *Database {
   mdb := &Database{}
   if config.Properties.Databases == 0 {
      config.Properties.Databases = 16
   }
   mdb.dbSet = make([]*DB, config.Properties.Databases)
   for i := range mdb.dbSet {
      singleDB := makeDB()
      singleDB.index = i
      mdb.dbSet[i] = singleDB
   }
   if config.Properties.AppendOnly {
      aofHandler, err := aof.NewAOFHandler(mdb)
      if err != nil {
         panic(err)
      }
      mdb.aofHandler = aofHandler
      for _, db := range mdb.dbSet {
         singleDB := db
         singleDB.addAof = func(line CmdLine) {
            mdb.aofHandler.AddAof(singleDB.index, line)
         }
      }
   }
   return mdb
}

將AOF加入到database里

使用singleDB的原因:因為在循環(huán)中獲取返回變量的地址都完全相同,因此當我們想要訪問數(shù)組中元素所在的地址時,不應該直接獲取 range 返回的變量地址 db,而應該使用 singleDB := db

database/db.go

type DB struct {
   index int
   data   dict.Dict
   addAof func(CmdLine)
}

func makeDB() *DB {
	db := &DB{
		data:   dict.MakeSyncDict(),
		addAof: func(line CmdLine) {},
	}
	return db
}

由于分數(shù)據(jù)庫db引用不到aof,所以添加一個addAof匿名函數(shù),在NewDatabase中用這個匿名函數(shù)調(diào)用AddAof

database/keys.go

func execDel(db *DB, args [][]byte) resp.Reply {
   ......
   if deleted > 0 {
      db.addAof(utils.ToCmdLine2("del", args...))
   }
   return reply.MakeIntReply(int64(deleted))
}

func execFlushDB(db *DB, args [][]byte) resp.Reply {
	db.Flush()
	db.addAof(utils.ToCmdLine2("flushdb", args...))
	return &reply.OkReply{}
}

func execRename(db *DB, args [][]byte) resp.Reply {
	......
	db.addAof(utils.ToCmdLine2("rename", args...))
	return &reply.OkReply{}
}

func execRenameNx(db *DB, args [][]byte) resp.Reply {
	......
	db.addAof(utils.ToCmdLine2("renamenx", args...))
	return reply.MakeIntReply(1)
}

database/string.go

func execSet(db *DB, args [][]byte) resp.Reply {
   ......
   db.addAof(utils.ToCmdLine2("set", args...))
   return &reply.OkReply{}
}

func execSetNX(db *DB, args [][]byte) resp.Reply {
   ......
   db.addAof(utils.ToCmdLine2("setnx", args...))
   return reply.MakeIntReply(int64(result))
}

func execGetSet(db *DB, args [][]byte) resp.Reply {
   key := string(args[0])
   value := args[1]

   entity, exists := db.GetEntity(key)
   db.PutEntity(key, &database.DataEntity{Data: value})
   db.addAof(utils.ToCmdLine2("getset", args...))
   ......
}

添加addAof方法

測試命令

*3\r\n$3\r\nSET\r\n$3\r\nkey\r\n$5\r\nvalue\r\n
*2\r\n$3\r\nGET\r\n$3\r\nkey\r\n
*2\r\n$6\r\nSELECT\r\n$1\r\n1\r\n

到此這篇關(guān)于一文詳解GO如何實現(xiàn)Redis的AOF持久化的文章就介紹到這了,更多相關(guān)GO實現(xiàn)Redis AOF持久化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Go語言os包用法詳解

    Go語言os包用法詳解

    本文主要介紹了Go語言os包用法詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-04-04
  • Go string轉(zhuǎn)int,int64,int32及注意事項說明

    Go string轉(zhuǎn)int,int64,int32及注意事項說明

    這篇文章主要介紹了Go string轉(zhuǎn)int,int64,int32及注意事項說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • Go語言流程控制語句

    Go語言流程控制語句

    這篇文章介紹了Go語言流程控制語句的用法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-07-07
  • Go中阻塞以及非阻塞操作實現(xiàn)(Goroutine和main Goroutine)

    Go中阻塞以及非阻塞操作實現(xiàn)(Goroutine和main Goroutine)

    本文主要介紹了Go中阻塞以及非阻塞操作實現(xiàn)(Goroutine和main Goroutine),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-05-05
  • Golang實現(xiàn)將視頻按照時間維度剪切的工具

    Golang實現(xiàn)將視頻按照時間維度剪切的工具

    這篇文章主要為大家詳細介紹了如何利用Golang實現(xiàn)將視頻按照時間維度進行剪切,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起了解一下
    2022-12-12
  • Golang實現(xiàn)常見的限流算法的示例代碼

    Golang實現(xiàn)常見的限流算法的示例代碼

    限流是項目中經(jīng)常需要使用到的一種工具,一般用于限制用戶的請求的頻率,也可以避免瞬間流量過大導致系統(tǒng)崩潰,或者穩(wěn)定消息處理速率,本文主要介紹了使用Go實現(xiàn)常見的限流算法,希望對大家有所幫助
    2023-04-04
  • golang壓縮與解壓縮文件的示例代碼

    golang壓縮與解壓縮文件的示例代碼

    這篇文章主要給大家介紹了golang壓縮與解壓縮文件,文中通過代碼示例給大家介紹的非常詳細,對大家的學習或工作有一定的幫助,需要的朋友可以參考下
    2024-02-02
  • Golang共享變量如何解決問題

    Golang共享變量如何解決問題

    協(xié)程之間的通信只能夠通過通道。但是我們習慣于共享變量,而且很多時候使用共享變量能讓代碼更簡潔。那么Golang共享變量如何解決問題,感興趣的可以了解一下
    2021-12-12
  • golang?四則運算計算器yacc歸約手寫實現(xiàn)

    golang?四則運算計算器yacc歸約手寫實現(xiàn)

    這篇文章主要為大家介紹了golang?四則運算?計算器?yacc?歸約的手寫實現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-07-07
  • GO語言并發(fā)編程之互斥鎖、讀寫鎖詳解

    GO語言并發(fā)編程之互斥鎖、讀寫鎖詳解

    這篇文章主要介紹了GO語言并發(fā)編程之互斥鎖、讀寫鎖詳解,本文是GO并發(fā)編程實戰(zhàn)一書的樣章,詳細講解了互斥鎖、讀寫鎖,然后給出了一個完整示例,需要的朋友可以參考下
    2014-11-11

最新評論

凭祥市| 蚌埠市| 旬邑县| 佛坪县| 道孚县| 林西县| 武强县| 大港区| 海伦市| 双桥区| 辽阳市| 三原县| 云梦县| 盘锦市| 泸州市| 肥乡县| 松溪县| 武山县| 北票市| 玉溪市| 建德市| 天门市| 拉孜县| 英超| 嘉善县| 山东省| 安达市| 拜城县| 东源县| 舟曲县| 项城市| 九龙县| 元朗区| 柳州市| 巴林左旗| 鄂伦春自治旗| 铜陵市| 伊川县| 佳木斯市| 西乌珠穆沁旗| 林甸县|