Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

简化 MetaCmd casToken 实现 #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions meta_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ func buildMetaFlags(fs []metaFlag) string {
}

func obtainMetaFlagsResults(ss []string) (mr MetaResult, err error) {
// Always set the cas token as setted
// enforce the operation use this token always use the CasToken.value
// even if the token is not returned.
// To avoid unexpected non-cas opertion caused by the lack of "c" flag.
mr.CasToken.setted = true
for _, f := range ss {
k, v := f[0], f[1:]
switch k {
Expand All @@ -32,7 +27,9 @@ func obtainMetaFlagsResults(ss []string) (mr MetaResult, err error) {
case 'O':
mr.Opaque = v
case 'c':
mr.CasToken.value, err = strconv.ParseInt(v, 10, 64)
var cv int64
cv, err = strconv.ParseInt(v, 10, 64)
mr.CasToken = casToken(cv)
case 'f':
v, err := strconv.ParseUint(v, 10, 32)
if err != nil {
Expand Down
29 changes: 17 additions & 12 deletions meta_options.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package memcache

type casToken struct {
value int64
setted bool
type CasToken interface {
value() int64
}

type casToken int64

func (c casToken) value() int64 {
return int64(c)
}

type MetaGetOptions struct {
Expand Down Expand Up @@ -79,7 +84,7 @@ type MetaSetOptions struct {
Key string // the key of item
BinaryKey []byte // interpret key as base64 encoded binary value (see metaget)
Value []byte // the value of item
CasToken casToken // compare and swap token
CasToken CasToken // compare and swap token

GetCasToken bool // return CAS value if successfully stored.

Expand Down Expand Up @@ -109,16 +114,16 @@ func (o MetaSetOptions) marshal() (fs []metaFlag) {
if o.SetTTL != 0 {
fs = append(fs, withSetTTL(o.SetTTL))
}
if o.CasToken.setted {
fs = append(fs, withCompareCAS(o.CasToken.value))
if o.CasToken != nil {
fs = append(fs, withCompareCAS(o.CasToken.value()))
}
return
}

type MetaDeletOptions struct {
Key string // the key of item
BinaryKey []byte // interpret key as base64 encoded binary value (see metaget)
CasToken casToken // compare and swap token
CasToken CasToken // compare and swap token

SetTTL uint64 // updates TTL, only when paired with the SetInvalidate option
SetInvalidate bool // mark as stale, bumps CAS.
Expand All @@ -134,8 +139,8 @@ func (o MetaDeletOptions) marshal() (fs []metaFlag) {
if o.SetTTL != 0 {
fs = append(fs, withSetTTL(o.SetTTL))
}
if o.CasToken.setted {
fs = append(fs, withCompareCAS(o.CasToken.value))
if o.CasToken != nil {
fs = append(fs, withCompareCAS(o.CasToken.value()))
}
return
}
Expand All @@ -151,7 +156,7 @@ const (
type MetaArithmeticOptions struct {
Key string // the key of item
BinaryKey []byte // interpret key as base64 encoded binary value (see metaget)
CasToken casToken // compare and swap token
CasToken CasToken // compare and swap token

GetCasToken bool // return current CAS value if successful.
GetTTL bool // return current TTL
Expand Down Expand Up @@ -193,8 +198,8 @@ func (o MetaArithmeticOptions) marshal() (fs []metaFlag) {
if o.SetTTL != 0 {
fs = append(fs, withSetTTL(o.SetTTL))
}
if o.CasToken.setted {
fs = append(fs, withCompareCAS(o.CasToken.value))
if o.CasToken != nil {
fs = append(fs, withCompareCAS(o.CasToken.value()))
}
return
}
31 changes: 29 additions & 2 deletions meta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestMetaSetGet(t *testing.T) {
if err != nil {
t.Error(err)
}
if sr.CasToken.value == 0 {
if int(sr.CasToken) == 0 {
t.Error("CAS Incorrect")
}

Expand Down Expand Up @@ -127,7 +127,7 @@ func TestMetaSetCAS(t *testing.T) {
_, err = c.MetaSet(ctx, MetaSetOptions{
Key: k,
Value: v,
CasToken: casToken{0, true},
CasToken: casToken(0),
})
if err != ErrCASConflict {
t.Error("CAS Invalid")
Expand Down Expand Up @@ -279,3 +279,30 @@ func TestBinaryKey(t *testing.T) {
t.Error("Binary Key Error.", err)
}
}

func TestMetaCasMiss(t *testing.T) {
c, _ := New(os.Getenv("MC_ADDRESS"), 2, 100)

ctx := context.Background()
k := "MEI"
var ttl uint64 = 20

item, err := c.MetaGet(ctx, MetaGetOptions{
Key: k,
SetVivifyWithTTL: ttl,
GetValue: true,
})
if err != nil {
t.Error(err)
}

item, err = c.MetaSet(ctx, MetaSetOptions{
Key: k,
CasToken: item.CasToken,
Value: []byte("t"),
})
if err != ErrCASConflict {
t.Errorf("CAS Error")
}

}