设为首页 加入收藏

TOP

GO实现Redis:GO实现内存数据库(3)(四)
2023-07-23 13:31:29 】 浏览:74
Tags:实现 Redis
turn reply.MakeErrReply("no such key") } db.PutEntity(dest, entity) db.Remove(src) return &reply.OkReply{} } // execRenameNx:renamenx k1 k2 func execRenameNx(db *DB, args [][]byte) resp.Reply { src := string(args[0]) dest := string(args[1]) _, exist := db.GetEntity(dest) if exist { return reply.MakeIntReply(0) } entity, ok := db.GetEntity(src) if !ok { return reply.MakeErrReply("no such key") } db.Removes(src, dest) db.PutEntity(dest, entity) return reply.MakeIntReply(1) } // execKeys:keys func execKeys(db *DB, args [][]byte) resp.Reply { pattern := wildcard.CompilePattern(string(args[0])) result := make([][]byte, 0) db.data.ForEach(func(key string, val interface{}) bool { if pattern.IsMatch(key) { result = append(result, []byte(key)) } return true }) return reply.MakeMultiBulkReply(result) } func init() { RegisterCommand("Del", execDel, -2) RegisterCommand("Exists", execExists, -2) RegisterCommand("Keys", execKeys, 2) RegisterCommand("FlushDB", execFlushDB, -1) RegisterCommand("Type", execType, 2) RegisterCommand("Rename", execRename, 3) RegisterCommand("RenameNx", execRenameNx, 3) }

keys.go实现以下指令:
execDel:del k1 k2 k3 ...
execExists:exist k1 k2 k3 ...
execFlushDB:flushdb
execType:type k1
execRename:rename k1 k2
execRenameNx:renamenx k1 k2
execKeys:keys(依赖lib包的工具类wildcard.go)

database/string.go

// execGet:get k1
func execGet(db *DB, args [][]byte) resp.Reply {
   key := string(args[0])
   bytes, err := db.getAsString(key)
   if err != nil {
      return err
   }
   if bytes == nil {
      return &reply.NullBulkReply{}
   }
   return reply.MakeBulkReply(bytes)
}

// execSet:set k v
func (db *DB) getAsString(key string) ([]byte, reply.ErrorReply) {
   entity, ok := db.GetEntity(key)
   if !ok {
      return nil, nil
   }
   bytes, ok := entity.Data.([]byte)
   if !ok {
      return nil, &reply.WrongTypeErrReply{}
   }
   return bytes, nil
}

func execSet(db *DB, args [][]byte) resp.Reply {
   key := string(args[0])
   value := args[1]
   entity := &database.DataEntity{
      Data: value,
   }
   db.PutEntity(key, entity)
   return &reply.OkReply{}
}

// execSetNX:setnex k v
func execSetNX(db *DB, args [][]byte) resp.Reply {
   key := string(args[0])
   value := args[1]
   entity := &database.DataEntity{
      Data: value,
   }
   result := db.PutIfAbsent(key, entity)
   return reply.MakeIntReply(int64(result))
}

// execGetSet:getset k v 返回旧值
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})
   if !exists {
      return reply.MakeNullBulkReply()
   }
   old := entity.Data.([]byte)
   return reply.MakeBulkReply(old)
}

// execStrLen:strlen k
func execStrLen(db *DB, args [][]byte) resp.Reply {
   key := string(args[0])
   entity, exists := db.GetEntity(key)
   if !exists {
      return reply.MakeNullBulkReply()
   }
   old := entity.Data.([]byte)
   return reply.MakeIntReply(int64(len(old)))
}

func init() {
   RegisterCommand("Get", execGet, 2)
   RegisterCommand("Set", execSet, -3)
   RegisterCommand("SetNx", execSetNX, 3)
   RegisterCommand("GetSet", execGetSet, 3)
   RegisterCommand("StrLen", execStrLen, 2)
}

string.go实现以下指令:
execGet:get k1
execS

首页 上一页 1 2 3 4 下一页 尾页 4/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇「高频必考」Docker&K8S面试题和.. 下一篇Go语言入门2(流程控制,string)

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目