Redis命令参考我个人比较喜欢这个,可以解决很多问题。下面开始讨论为什么我们喜欢这个ReJSONini解决方案场景平台或者项目,都会有类似的数据:配置信息,用户基本信息,数据库中存储了很多经常不变的数据,但是我们的项目需要有这些数据被不断的查询并验证。为了减少数据库查询,我们可以使用rejson将数据库中的这些基本信息放到redis中。在进行校验等操作时,可以直接进行数据校验。解决数据库压力,部分信息存储。有了rejosn,可以很好的避免redis,不方便对象解析等操作。更新后处理起来很麻烦。支持rejson的第三方包https://github.com/search?l=J...我用的go语言,rejson的支持包https://github.com/nitishm/go...学习recordstartredisredis-server//Redis默认端口为6379,也可以更改端口号启动,redis-server--port6380//Redis会使用配置文件启动;url为配置文件路径redis-serverurl比如我的mac电脑redis配置路径为redis-server/usr/local/etc/redis.conf2。停止redis服务redis-clishutdown3。我的电脑已经安装了ReJSON,直接运行rejson命令packagemainimport("encoding/json""flag""fmt""log"//"github.com/garyburd/redigo/redis""github.com/gomodule/redigo/redis"rejson"github.com/nitishm/go-rejson")varaddr=标志。String("Server","localhost:6379","Redisserveraddress")typeNamestruct{第一个字符串`json:"first,omitempty"`中间字符串`json:"middle,omitempty"`最后一个字符串`json:"last,omitempty"`}typeStudentstruct{NameName`json:"name,omitempty"`Rankint`json:"rank,omitempty"`}funcmain(){testMain()}typeStudentInfostruct{信息*StudentDetails`json:"info,omitempty"`Rankint`json:”rank,omitempty”`}typeStudentDetailsstruct{FirstNamestringLastNamestringMajorstring}functestMain(){flag.Parse()conn,err:=redis.Dial("tcp",*addr)iferr!=nil{log.Fatalf("无法连接到redis-server@%s",*addr)}deferfunc(){//conn.Do("FLUSHALL")conn.Close()}()studentJD:=StudentInfo{Info:&StudentDetails{FirstName:"John",LastName:"Doe",Major:"CSE",},Rank:1,}b,err:=json.Marshal(&studentJD)iferr!=nil{返回}//_,err=conn.Do(“SET”,“JohnDoe”,string(b))//如果err!=nil{//返回//}fmt.Println(string(b))_,err=rejson.JSONSet(conn,"JohnDoeJSON",".",studentJD,false,false)如果错误!=nil{return}outJSON,err:=rejson.JSONGet(conn,"JohnDoeJSON","")如果错误!=无{返回}outStudent:=&StudentInfo{}err=json.Unmarshal(outJSON.([]byte),outStudent)如果错误!=nil{return}v,_:=json.Marshal(outStudent)fmt.Println(string(v))fmt.Println(fmt.Sprintf("排名:%d",outStudent.Rank))fmt.Println(fmt.Sprintf("信息:%s",outStudent.Info))fmt.Println(fmt.Sprintf("Info.FirstName:%s",outStudent.Info.FirstName))fmt.Println(fmt.Sprintf("Info.LastName:%s",outStudent.Info.LastName))fmt.Println(fmt.Sprintf("Info.Major:%s",outStudent.Info.Major))//=======测试json.set=====log.Println("======测试json.set=========")_,err=rejson.JSONSet(conn,"JohnDoeJSON",".Info.Major","EE",false,false)iferr!=nil{return}outJSON,err=rejson.JSONGet(conn,"JohnDoeJSON","")iferr!=nil{return}outStudent=&StudentInfo{}err=json.Unmarshal(outJSON.([]byte),outStudent)iferr!=nil{return}v,_=json.Marshal(outStudent)fmt.Println(string(v))fmt.Println(fmt.Sprintf("Rank:%d",outStudent.Rank))fmt.Println(fmt.Sprintf("Info:%s",outStudent.Info))fmt.Println(fmt.Sprintf("Info.FirstName:%s",outStudent.Info.FirstName))fmt.Println(fmt.Sprintf("Info.LastName:%s",outStudent.Info.LastName))fmt.Println(fmt.Sprintf("Info.Major:%s",outStudent.Info.Major))}结果输出:
