代码拉取完成,页面将自动刷新
package entitystorageredis
import (
"io"
"github.com/garyburd/redigo/redis"
"github.com/pkg/errors"
"github.com/xiaonanln/goworld/engine/common"
"github.com/xiaonanln/goworld/engine/netutil"
"github.com/xiaonanln/goworld/engine/storage/storage_common"
)
var (
dataPacker = netutil.MessagePackMsgPacker{}
)
type redisEntityStorage struct {
c redis.Conn
}
// OpenRedis opens redis as entity storage
func OpenRedis(url string, dbindex int) (storagecommon.EntityStorage, error) {
c, err := redis.DialURL(url)
if err != nil {
return nil, errors.Wrap(err, "redis dail failed")
}
if dbindex >= 0 {
if _, err := c.Do("SELECT", dbindex); err != nil {
return nil, errors.Wrap(err, "redis select db failed")
}
}
es := &redisEntityStorage{
c: c,
}
return es, nil
}
func entityKey(typeName string, eid common.EntityID) string {
return typeName + "$" + string(eid)
}
func packData(data interface{}) (b []byte, err error) {
b, err = dataPacker.PackMsg(data, b)
return
}
func (es *redisEntityStorage) List(typeName string) ([]common.EntityID, error) {
keyMatch := typeName + "$*"
r, err := redis.Values(es.c.Do("SCAN", "0", "MATCH", keyMatch, "COUNT", 10000))
if err != nil {
return nil, err
}
var eids []common.EntityID
prefixLen := len(typeName) + 1
for {
nextCursor := r[0]
keys, err := redis.Strings(r[1], nil)
if err != nil {
return nil, err
}
for _, key := range keys {
println("key", key)
eids = append(eids, common.EntityID(key[prefixLen:]))
}
if isZeroCursor(nextCursor) {
break
}
r, err = redis.Values(es.c.Do("SCAN", nextCursor, "MATCH", keyMatch, "COUNT", 10000))
if err != nil {
return nil, err
}
}
return eids, nil
}
func isZeroCursor(c interface{}) bool {
return string(c.([]byte)) == "0"
}
func (es *redisEntityStorage) Write(typeName string, entityID common.EntityID, data interface{}) error {
b, err := packData(data)
if err != nil {
return err
}
_, err = es.c.Do("SET", entityKey(typeName, entityID), b)
return err
}
func (es *redisEntityStorage) Read(typeName string, entityID common.EntityID) (interface{}, error) {
b, err := redis.Bytes(es.c.Do("GET", entityKey(typeName, entityID)))
if err != nil {
return nil, err
}
var data map[string]interface{}
if err = dataPacker.UnpackMsg(b, &data); err != nil {
return nil, err
}
return data, nil
}
func (es *redisEntityStorage) Exists(typeName string, entityID common.EntityID) (bool, error) {
key := entityKey(typeName, entityID)
exists, err := redis.Bool(es.c.Do("EXISTS", key))
return exists, err
}
func (es *redisEntityStorage) Close() {
es.c.Close()
}
func (es *redisEntityStorage) IsEOF(err error) bool {
return err == io.EOF || err == io.ErrUnexpectedEOF
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。