大家好,我是彬哥,本节给大家讲下go语言服务器游戏缓存处理相关,抛砖引玉了,主要是针对Go语言游戏服务器开发缓存如何处理。
首选我们来看下LollipopGo v1.0.20190104 版本的处理,缓存使用cache2go;
github地址
官方代码例子如下:
package main
import (
"github.com/muesli/cache2go"
"fmt"
"time"
)
// Keys & values in cache2go can be of arbitrary types, e.g. a struct.
type myStruct struct {
text string
moreData []byte
}
func main() {
// Accessing a new cache table for the first time will create it.
cache := cache2go.Cache("myCache")
// We will put a new item in the cache. It will expire after
// not being accessed via Value(key) for more than 5 seconds.
val := myStruct{"This is a test!", []byte{}}
cache.Add("someKey", 5*time.Second, &val)
// Let's retrieve the item from the cache.
res, err := cache.Value("someKey")
if err == nil {
fmt.Println("Found value in cache:", res.Data().(*myStruct).text)
} else {
fmt.Println("Error retrieving value from cache:", err)
}
// Wait for the item to expire in cache.
time.Sleep(6 * time.Second)
res, err = cache.Value("someKey")
if err != nil {
fmt.Println("Item is not cached (anymore).")
}
// Add another item that never expires.
cache.Add("someKey", 0, &val)
// cache2go supports a few handy callbacks and loading mechanisms.
cache.SetAboutToDeleteItemCallback(func(e *cache2go.CacheItem) {
fmt.Println("Deleting:", e.Key(), e.Data().(*myStruct).text, e.CreatedOn())
})
// Remove the item from the cache.
cache.Delete("someKey")
// And wipe the entire cache table.
cache.Flush()
}
社区测试修改缓存例子,代码如下:
package main
import (
"cache2go"
"fmt"
)
var cache *cache2go.CacheTable
var SaveChessData map[int]*GolangLtd
type GolangLtd struct {
RoomUID int
PlayerA string
PlayerB string
Default [4][4]int
ChessData [4][4]int
}
func init() {
cache = cache2go.Cache("myCache")
SaveChessData = make(map[int]*GolangLtd)
return
}
//------------------------------------------------------------------------------
func main() {
data11 := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
data1 := [4][4]int{{17, 17, 17, 17}, {17, 17, 17, 17}, {17, 17, 17, 17}, {17, 17, 17, 17}}
re := InitDSQ(data11)
data := &GolangLtd{
RoomUID: 1,
PlayerB: "987654321",
Default: data1,
ChessData: re,
}
SaveChessData[1] = data
// 保存数据
cache.Add(111, 0, data)
// 获取数据
res, err1 := cache.Value(111)
if err1 != nil {
fmt.Println(err1)
return
}
//--------------------------------------------------------------------------
fmt.Println("result:", res.Data().(*GolangLtd).RoomUID)
res.Data().(*GolangLtd).RoomUID = 2
fmt.Println("result:", res.Data().(*GolangLtd).RoomUID)
//--------------------------------------------------------------------------
fmt.Println("result:", res.Data().(*GolangLtd).Default)
fmt.Println("result:", res.Data().(*GolangLtd).Default[1][2])
res.Data().(*GolangLtd).Default[1][2] = 18
fmt.Println("result:", res.Data().(*GolangLtd).Default)
fmt.Println("result:", res.Data().(*GolangLtd).PlayerA)
//--------------------------------------------------------------------------
}
运行结果
LollipopGo 子游戏服务器使用,代码如下:
定义变量:
var cacheDSQ *cache2go.CacheTable
初始化:
func initDSQNetRPC() {
client, err := jsonrpc.Dial("tcp", service)
if err != nil {
log.Debug("dial error:", err)
}
ConnDSQRPC = client
cacheDSQ = cache2go.Cache("LollipopGo_DSQ")
}
测试逻辑,代码如下:
//------------------------------------------------------------------------------
func CacheSaveRoomData(iRoomID int, data *RoomPlayerDSQ, openid string) {
cacheDSQ.Add(iRoomID, 0, data)
CacheSavePlayerUID(iRoomID, openid)
}
func CacheGetPlayerUID(iRoomID int, player string) string {
res, err1 := cacheDSQ.Value(iRoomID)
if err1 != nil {
panic("没有对应数据")
return ""
}
if res.Data().(*RoomPlayerDSQ).OpenIDA == player {
return res.Data().(*RoomPlayerDSQ).OpenIDB
} else {
return res.Data().(*RoomPlayerDSQ).OpenIDA
}
return ""
}
func CacheSavePlayerUID(iRoomID int, player string) {
res, err1 := cacheDSQ.Value(iRoomID)
if err1 != nil {
panic("没有对应数据")
return
}
fmt.Println("result:", res.Data().(*RoomPlayerDSQ).OpenIDA)
fmt.Println("result:", res.Data().(*RoomPlayerDSQ).OpenIDB)
if len(res.Data().(*RoomPlayerDSQ).OpenIDA) == 0 {
res.Data().(*RoomPlayerDSQ).OpenIDA = player
} else {
res.Data().(*RoomPlayerDSQ).OpenIDB = player
}
fmt.Println("result:", res.Data().(*RoomPlayerDSQ).OpenIDA)
fmt.Println("result:", res.Data().(*RoomPlayerDSQ).OpenIDB)
return
}
func CacheUpdateRoomData(iRoomID int, Update_pos string, value int) {
res, err1 := cacheDSQ.Value(iRoomID)
if err1 != nil {
panic("棋盘数据更新失败!")
return
}
ipos_x := 0
ipos_y := 0
strsplit := Strings_Split(Update_pos, ",")
if len(strsplit) != 2 {
panic("棋盘数据更新失败!")
return
}
for i := 0; i < len(strsplit); i++ {
if i == 0 {
ipos_x = util.Str2int_LollipopGo(strsplit[i])
} else {
ipos_y = util.Str2int_LollipopGo(strsplit[i])
}
}
fmt.Println("修改的棋盘的坐标", ipos_x, ipos_y)
// 测试数据
fmt.Println("result:", res.Data().(*RoomPlayerDSQ).ChessData[ipos_x][ipos_y])
res.Data().(*RoomPlayerDSQ).ChessData[ipos_x][ipos_y] = value
fmt.Println("result:", res.Data().(*RoomPlayerDSQ).ChessData[ipos_x][ipos_y])
return
}
// 获取默认棋牌数据是否翻开
// true 表示翻开了
// itype ==1 查询是否翻开
// itype ==2 修改数据
func CacheGetChessDefaultData(iRoomID int, Update_pos string, itype int, valve int) (bool, int) {
res, err1 := cacheDSQ.Value(iRoomID)
if err1 != nil {
panic("棋盘数据获取数据失败!")
return false, -1
}
ipos_x := 0
ipos_y := 0
strsplit := Strings_Split(Update_pos, ",")
if len(strsplit) != 2 {
panic("棋盘数据获取数据失败!")
return false, -1
}
for i := 0; i < len(strsplit); i++ {
if i == 0 {
ipos_x = util.Str2int_LollipopGo(strsplit[i])
} else {
ipos_y = util.Str2int_LollipopGo(strsplit[i])
}
}
fmt.Println("修改的棋盘的坐标", ipos_x, ipos_y)
if itype == 1 {
// 获取
fmt.Println("result:", res.Data().(*RoomPlayerDSQ).Default[ipos_x][ipos_y])
idata := res.Data().(*RoomPlayerDSQ).Default[ipos_x][ipos_y]
if idata == (2*Proto2.Mouse + 1) {
return false, -1
} else {
return true, -1
}
} else if itype == 2 {
// 修改翻盘结构
fmt.Println("result:", res.Data().(*RoomPlayerDSQ).Default[ipos_x][ipos_y])
res.Data().(*RoomPlayerDSQ).Default[ipos_x][ipos_y] = valve
return true, res.Data().(*RoomPlayerDSQ).ChessData[ipos_x][ipos_y]
}
return false, -1
}
//------------------------------------------------------------------------------
社区交流群:221273219
Golang语言社区论坛 :
www.Golang.Ltd
LollipopGo游戏服务器地址:
https://github.com/Golangltd/LollipopGo
社区视频课程课件GIT地址:
https://github.com/Golangltd/codeclass
Golang语言社区
网友评论