You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
starmap/client_basicrw.go

97 lines
1.8 KiB
Go

4 years ago
package starmap
import (
"b612.me/notify"
4 years ago
)
func (kv *RemoteKv) clientOnline() error {
return kv.reconnect()
4 years ago
}
4 years ago
func (kv *RemoteKv) MustGet(key string) interface{} {
result, _ := kv.Get(key)
return result
}
func (kv *RemoteKv) Get(key interface{}) (interface{}, error) {
4 years ago
if err := kv.clientOnline(); err != nil {
return nil, err
}
keyCode, err := notify.ToMsgVal(key)
4 years ago
if err != nil {
return nil, err
}
data, err := kv.client.SendWait("get", keyCode, kv.readTimeout)
4 years ago
if err != nil {
return nil, err
}
rp, err := data.Value.ToInterface()
if err != nil {
return nil, err
4 years ago
}
reply := rp.(kvMsg)
return reply.Value, reply.Err
4 years ago
}
func (kv *RemoteKv) Store(key interface{}, value interface{}) error {
4 years ago
if err := kv.clientOnline(); err != nil {
return err
}
encodeData, err := notify.ToMsgVal(kvMsg{
Key: key,
Value: value,
Err: nil,
})
4 years ago
if err != nil {
return err
}
data, err := kv.client.SendWait("store", encodeData, kv.readTimeout)
4 years ago
if err != nil {
return err
}
rp, err := data.Value.ToInterface()
if err != nil {
return err
4 years ago
}
return rp.(kvMsg).Err
4 years ago
}
func (kv *RemoteKv) Delete(key interface{}) error {
4 years ago
if err := kv.clientOnline(); err != nil {
return err
}
keyCode, err := notify.ToMsgVal(key)
4 years ago
if err != nil {
return err
}
data, err := kv.client.SendWait("delete", keyCode, kv.readTimeout)
if err != nil {
return err
4 years ago
}
rp, err := data.Value.ToInterface()
if err != nil {
return err
}
return rp.(kvMsg).Err
4 years ago
}
func (kv *RemoteKv) Exists(key interface{}) bool {
if err := kv.clientOnline(); err != nil {
return false
}
keyCode, err := notify.ToMsgVal(key)
if err != nil {
return false
}
data, err := kv.client.SendWait("exists", keyCode, kv.readTimeout)
if err != nil {
return false
}
rp, err := data.Value.ToInterface()
if err != nil {
return false
4 years ago
}
reply := rp.(kvMsg)
return reply.Value.(bool)
4 years ago
}