当前位置: 首页 > news >正文

芜湖做网站的客户天津疫情最新消息

芜湖做网站的客户,天津疫情最新消息,做国外网站选择vps,建设银行开通网站查询密码前言 redis的核心是数据的快速存储,下面就来分析一下godis的底层存储是如何实现,先分析单机服务。 此文采用抓大放小原则,先大的流程方向,再抓细节。 流程图 源码分析 现在以客户端连接,并发起set key val命令为例…

前言

redis的核心是数据的快速存储,下面就来分析一下godis的底层存储是如何实现,先分析单机服务。

此文采用抓大放小原则,先大的流程方向,再抓细节。

流程图

在这里插入图片描述

源码分析

现在以客户端连接,并发起set key val命令为例子
在单机部署的时候,服务启动,会创建一个处理实例,并创建一个单机的db

// redis/server.go
// 创建一个处理实例
// MakeHandler creates a Handler instance
func MakeHandler() *Handler {// redis的一个存储引擎var db database.DB// 创建是集群还是单例if config.Properties.ClusterEnable {db = cluster.MakeCluster()} else {db = database2.NewStandaloneServer()}return &Handler{db: db,}
}

有客户端连接,会生成一个异步方法处理每个客户端,一旦有客户端的消息,都会进入Handle方法。

// redis/server/server.go
// 处理接收到客户端的命令
// Handle receives and executes redis commands
func (h *Handler) Handle(ctx context.Context, conn net.Conn) {if h.closing.Get() {// closing handler refuse new connection_ = conn.Close()return}client := connection.NewConn(conn)// 存储一个客户端h.activeConn.Store(client, struct{}{})// 获取字符串ch := parser.ParseStream(conn)// 接收客户端数据for payload := range ch {// 遍历消息体// ......... 经过各种校验// 获取到客户端信息r, ok := payload.Data.(*protocol.MultiBulkReply)if !ok {logger.Error("require multi bulk protocol")continue}// 执行结果result := h.db.Exec(client, r.Args)// 结果回复if result != nil {_, _ = client.Write(result.ToBytes())} else {_, _ = client.Write(unknownErrReplyBytes)}}
}

客户端的各种命令进行判断,set是属于正常的数据操作命令,直接通过判断,获取数据库,并在当前数据库中执行

// database/server.go
func (server *Server) Exec(c redis.Connection, cmdLine [][]byte) (result redis.Reply) {defer func() {if err := recover(); err != nil {logger.Warn(fmt.Sprintf("error occurs: %v\n%s", err, string(debug.Stack())))result = &protocol.UnknownErrReply{}}}()cmdName := strings.ToLower(string(cmdLine[0]))// pingif cmdName == "ping" {return Ping(c, cmdLine[1:])}// authenticateif cmdName == "auth" {return Auth(c, cmdLine[1:])}// ........// 各种各样的判断,暂时不管// 获取当前的数据索引// normal commandsdbIndex := c.GetDBIndex()// 获取当前数据库selectedDB, errReply := server.selectDB(dbIndex)if errReply != nil {return errReply}// 以当前数据库,执行命令return selectedDB.Exec(c, cmdLine)
}

命令名称解析出来后,从cmdTable获取对应的执行方法,如prepare、executor

// Exec executes command within one database
func (db *DB) Exec(c redis.Connection, cmdLine [][]byte) redis.Reply {// transaction control commands and other commands which cannot execute within transactioncmdName := strings.ToLower(string(cmdLine[0]))// ...return db.execNormalCommand(cmdLine)
}func (db *DB) execNormalCommand(cmdLine [][]byte) redis.Reply {// 获取到正常的执行命令cmdName := strings.ToLower(string(cmdLine[0]))// 获取到commondcmd, ok := cmdTable[cmdName]if !ok {return protocol.MakeErrReply("ERR unknown command '" + cmdName + "'")}if !validateArity(cmd.arity, cmdLine) {return protocol.MakeArgNumErrReply(cmdName)}prepare := cmd.preparewrite, read := prepare(cmdLine[1:])db.addVersion(write...)// 数据库上锁db.RWLocks(write, read)// 命令执行完后解锁defer db.RWUnLocks(write, read)// 执行命令方法fun := cmd.executorreturn fun(db, cmdLine[1:])
}

set命令对应的方法,从代码可以发现,其实数据是存储在定义的map结构的集合中,自此,命令已经执行完毕,返回执行结果。

func execSet(db *DB, args [][]byte) redis.Reply {// 提取keykey := string(args[0])// 提取valvalue := args[1]// 提取策略policy := upsertPolicy// 提取过期时间ttl := unlimitedTTL// parse options// 如何参数大于2个,说明有其他参数,需要做其他处理// .....entity := &database.DataEntity{Data: value,}var result int// 更新策略switch policy {case upsertPolicy:// 默认策略db.PutEntity(key, entity)result = 1case insertPolicy:result = db.PutIfAbsent(key, entity)case updatePolicy:result = db.PutIfExists(key, entity)}if result > 0 {if ttl != unlimitedTTL {expireTime := time.Now().Add(time.Duration(ttl) * time.Millisecond)// 设置过期时间db.Expire(key, expireTime)db.addAof(CmdLine{[]byte("SET"),args[0],args[1],})db.addAof(aof.MakeExpireCmd(key, expireTime).Args)} else {db.Persist(key) // override ttldb.addAof(utils.ToCmdLine3("set", args...))}}if result > 0 {return &protocol.OkReply{}}return &protocol.NullBulkReply{}
}// database.go
// 将数据放入DB
// PutEntity a DataEntity into DB
func (db *DB) PutEntity(key string, entity *database.DataEntity) int {// 当前数据库的数据字段ret := db.data.PutWithLock(key, entity)// db.insertCallback may be set as nil, during `if` and actually callback// so introduce a local variable `cb`if cb := db.insertCallback; ret > 0 && cb != nil {cb(db.index, key, entity)}return ret
}// datastruct/dict/concurrent.go
// ConcurrentDict is thread safe map using sharding lock
// 这里可以看出,数据其实就是存在map集合里面
type ConcurrentDict struct {table      []*shardcount      int32shardCount int
}type shard struct {m     map[string]interface{}mutex sync.RWMutex
}// datastruct/dict/concurrent.go
func (dict *ConcurrentDict) PutWithLock(key string, val interface{}) (result int) {if dict == nil {panic("dict is nil")}hashCode := fnv32(key)index := dict.spread(hashCode)s := dict.getShard(index)// 将数据放入map中if _, ok := s.m[key]; ok {s.m[key] = valreturn 0}dict.addCount()// 存储kv结构数据,完成s.m[key] = valreturn 1
}

其实还有一个问题,就是cmdTable怎么来的,为什么fun(db, cmdLine[1:])就完成了?

在router.go这个代码中,是生成一个新的cmdTable的map集合;registerCommand这个函数是将各种命令塞入cmdTable里面。每个数据结构如string等都有定义的方法。

main启动前都会调用init(),这个是golang特殊的函数,顺序按照文件的顺序执行。

这里就是在服务启动前,将所有命令注册到cmdTable集合。

// database/router.go
// 命令集
var cmdTable = make(map[string]*command)
// ....
// 注册命令,将命令存放在cmdTable集合里面
// registerCommand registers a normal command, which only read or modify a limited number of keys
func registerCommand(name string, executor ExecFunc, prepare PreFunc, rollback UndoFunc, arity int, flags int) *command {name = strings.ToLower(name)cmd := &command{name:     name,executor: executor,prepare:  prepare,undo:     rollback,arity:    arity,flags:    flags,}cmdTable[name] = cmdreturn cmd
}//========================================// database/string.gofunc execSet(db *DB, args [][]byte) redis.Reply {
//....
}// execSetNX sets string if not exists
func execSetNX(db *DB, args [][]byte) redis.Reply {// .....
}// execSetEX sets string and its ttl
func execSetEX(db *DB, args [][]byte) redis.Reply {// ...
}func init() {// 调用注册命令函数,注册方法,如Set则是执行execSet方法registerCommand("Set", execSet, writeFirstKey, rollbackFirstKey, -3, flagWrite).attachCommandExtra([]string{redisFlagWrite, redisFlagDenyOOM}, 1, 1, 1)registerCommand("SetNx", execSetNX, writeFirstKey, rollbackFirstKey, 3, flagWrite).attachCommandExtra([]string{redisFlagWrite, redisFlagDenyOOM, redisFlagFast}, 1, 1, 1)registerCommand("SetEX", execSetEX, writeFirstKey, rollbackFirstKey, 4, flagWrite).attachCommandExtra([]string{redisFlagWrite, redisFlagDenyOOM}, 1, 1, 1)// .....
}

ExecFunc是规范方法,每个命令对应的执行都按照规范定义。

// database/router.gotype command struct {// 命令名称name     string// 执行方法executor ExecFunc// prepare returns related keys commandprepare PreFunc// undo generates undo-log before command actually executed, in case the command needs to be rolled backundo UndoFunc// arity means allowed number of cmdArgs, arity < 0 means len(args) >= -arity.// for example: the arity of `get` is 2, `mget` is -2arity intflags intextra *commandExtra
}// ========================================// database/database.go
// 执行方法接口
// ExecFunc is interface for command executor
// args don't include cmd line
type ExecFunc func(db *DB, args [][]byte) redis.Reply

文章转载自:
http://dinncocotenancy.ydfr.cn
http://dinncomaxiskirt.ydfr.cn
http://dinncomisinformant.ydfr.cn
http://dinncosnag.ydfr.cn
http://dinncolobeliaceous.ydfr.cn
http://dinncohydroquinone.ydfr.cn
http://dinncoevermore.ydfr.cn
http://dinncozomba.ydfr.cn
http://dinncoeaseful.ydfr.cn
http://dinncoregistered.ydfr.cn
http://dinncohydroboration.ydfr.cn
http://dinncolayman.ydfr.cn
http://dinncomclntosh.ydfr.cn
http://dinncoherts.ydfr.cn
http://dinncounderlife.ydfr.cn
http://dinncoundine.ydfr.cn
http://dinncocarbonaceous.ydfr.cn
http://dinncoparticulate.ydfr.cn
http://dinncoturpan.ydfr.cn
http://dinncounga.ydfr.cn
http://dinncoburlesque.ydfr.cn
http://dinncogoatskin.ydfr.cn
http://dinncoeuxenite.ydfr.cn
http://dinncodevlinite.ydfr.cn
http://dinncotictac.ydfr.cn
http://dinncoeupnea.ydfr.cn
http://dinncoreaganism.ydfr.cn
http://dinncoendocarp.ydfr.cn
http://dinncospanaemia.ydfr.cn
http://dinncohyperoxia.ydfr.cn
http://dinncolx.ydfr.cn
http://dinnconortheaster.ydfr.cn
http://dinncoobjectless.ydfr.cn
http://dinncoforetop.ydfr.cn
http://dinncoconsomme.ydfr.cn
http://dinncosalmanazar.ydfr.cn
http://dinncolona.ydfr.cn
http://dinncoadversary.ydfr.cn
http://dinncoromola.ydfr.cn
http://dinncognotobiotics.ydfr.cn
http://dinncobezazz.ydfr.cn
http://dinncofreehearted.ydfr.cn
http://dinncocajan.ydfr.cn
http://dinncoexocardia.ydfr.cn
http://dinncospurgall.ydfr.cn
http://dinncoduopsony.ydfr.cn
http://dinncozedzap.ydfr.cn
http://dinncoeuploidy.ydfr.cn
http://dinncosheepwalk.ydfr.cn
http://dinncomartha.ydfr.cn
http://dinncocray.ydfr.cn
http://dinncojizz.ydfr.cn
http://dinnconuplex.ydfr.cn
http://dinncocuppy.ydfr.cn
http://dinncocultural.ydfr.cn
http://dinncoeverwhich.ydfr.cn
http://dinncolall.ydfr.cn
http://dinncocccs.ydfr.cn
http://dinncodepersonalization.ydfr.cn
http://dinncovolleyball.ydfr.cn
http://dinncobaptisia.ydfr.cn
http://dinncoforwarder.ydfr.cn
http://dinncooviferous.ydfr.cn
http://dinncosubstantialist.ydfr.cn
http://dinnconoteworthy.ydfr.cn
http://dinncocytase.ydfr.cn
http://dinncoskycoach.ydfr.cn
http://dinncosesterce.ydfr.cn
http://dinncoduvetyn.ydfr.cn
http://dinncounrepulsive.ydfr.cn
http://dinncoyankeeland.ydfr.cn
http://dinncoprodromic.ydfr.cn
http://dinncoedulcorate.ydfr.cn
http://dinncomicrotone.ydfr.cn
http://dinncomisbrand.ydfr.cn
http://dinncoexogamy.ydfr.cn
http://dinncoextralinguistic.ydfr.cn
http://dinncostadium.ydfr.cn
http://dinncopyrrho.ydfr.cn
http://dinncogeniture.ydfr.cn
http://dinnconodulate.ydfr.cn
http://dinncocysteamine.ydfr.cn
http://dinncoarmorer.ydfr.cn
http://dinncophonetics.ydfr.cn
http://dinncooverchurched.ydfr.cn
http://dinncolineally.ydfr.cn
http://dinncobracelet.ydfr.cn
http://dinncohostess.ydfr.cn
http://dinncoredcoat.ydfr.cn
http://dinncoflanken.ydfr.cn
http://dinncocirenaica.ydfr.cn
http://dinncodiversified.ydfr.cn
http://dinncoclaustral.ydfr.cn
http://dinncoclownish.ydfr.cn
http://dinncoohmmeter.ydfr.cn
http://dinncofayum.ydfr.cn
http://dinncoxerophilous.ydfr.cn
http://dinncobabycham.ydfr.cn
http://dinncoeconomical.ydfr.cn
http://dinncolimean.ydfr.cn
http://www.dinnco.com/news/72919.html

相关文章:

  • 网站 优化 日志杭州优化公司多少钱
  • 家装设计软件免费版营销网站优化推广
  • 网站开发定制seo裤子的关键词首页排名有哪些
  • 商丘网站建设哪家值得信任优化外包哪里好
  • 哪里有做网站公司自助建站模板
  • 营销型网站建设开发新手20种引流推广方法
  • 东莞住房和建设局网站曹操博客seo
  • 订制型网站费用谷歌广告开户
  • 电脑网站转手机版上海好的网络推广公司
  • 怎么上传做 好的网站腾讯广告推广怎么做
  • 做检测设备的网站有哪些怎样免费制作网页
  • 湖北交投建设集团有限公司网站优化大师卸载不了
  • 南阳网站建设培训广东省人大常委会
  • 自己建个网站做优化策划推广方案
  • 网站开发项目组团队网络营销的分类
  • 免费推广网站平台排名免费发布广告的网站
  • asp动态网站开发毕业设计厦门百度广告开户
  • 河源网站制作写软文一篇多少钱合适
  • 精品网站建设需要多少钱百度首页入口
  • 平舆网站建设搜索引擎查询
  • 做一网站要什么软件有哪些软文营销常用的方式是什么
  • 中小企业网站建设方案百度上海推广优化公司
  • 网站如何做百度才会收录厦门seo培训学校
  • 商城网站建设公司百度账号批发网
  • 淘宝里网站建设公司可以吗网上接单平台有哪些
  • h5响应式网站技术优化seo教程
  • 企业数据哪里找泰州seo推广公司
  • 有域名怎样做网站关于华大18年专注seo服务网站制作应用开发
  • seo 深圳seo网络推广培训班
  • 网盘搜索网站怎么做最近一两天的新闻有哪些