Skip to content

Commit 4d6d910

Browse files
authored
remove variable cache flags (#64)
1 parent 1bde9d0 commit 4d6d910

File tree

5 files changed

+42
-65
lines changed

5 files changed

+42
-65
lines changed

variable/api.go

+7-23
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func getByVariable(ctx context.Context, variable Variable) (interface{}, error)
7171
// 1.2 use variable.Getter() to get value
7272
getter := variable.Getter()
7373
if getter == nil {
74-
return "", errors.New(errGetterNotFound + variable.Name())
74+
return "", errors.New(errValueNotFound + variable.Name())
7575
}
7676
return getter.Get(ctx, nil, variable.Data())
7777
}
@@ -88,7 +88,7 @@ func getByName(ctx context.Context, name string) (interface{}, error) {
8888
if strings.HasPrefix(name, prefix) {
8989
getter := variable.Getter()
9090
if getter == nil {
91-
return "", errors.New(errGetterNotFound + name)
91+
return "", errors.New(errValueNotFound + name)
9292
}
9393
return getter.Get(ctx, nil, name)
9494
}
@@ -142,14 +142,9 @@ func getFlushedValue(ctx context.Context, index uint32) (interface{}, error) {
142142
if variables := ctx.Value(mosnctx.KeyVariables); variables != nil {
143143
if values, ok := variables.([]IndexedValue); ok {
144144
value := &values[index]
145-
if value.Valid || value.NotFound {
146-
if !value.noCacheable {
147-
return value.data, nil
148-
}
149-
150-
// clear flags
151-
//value.Valid = false
152-
//value.NotFound = false
145+
if value.Valid {
146+
return value.data, nil
147+
153148
}
154149

155150
return getIndexedValue(ctx, value, index)
@@ -162,25 +157,18 @@ func getFlushedValue(ctx context.Context, index uint32) (interface{}, error) {
162157
func getIndexedValue(ctx context.Context, value *IndexedValue, index uint32) (interface{}, error) {
163158
variable := indexedVariables[index]
164159

165-
//if value.NotFound || value.Valid {
166-
// return value.data, nil
167-
//}
168-
169160
getter := variable.Getter()
170161
if getter == nil {
171-
return "", errors.New(errGetterNotFound + variable.Name())
162+
return "", errors.New(errValueNotFound + variable.Name())
172163
}
173164
vdata, err := getter.Get(ctx, value, variable.Data())
174165
if err != nil {
175166
value.Valid = false
176-
value.NotFound = true
177167
return vdata, err
178168
}
179169

180170
value.data = vdata
181-
if (variable.Flags() & MOSN_VAR_FLAG_NOCACHEABLE) == MOSN_VAR_FLAG_NOCACHEABLE {
182-
value.noCacheable = true
183-
}
171+
value.Valid = true
184172
return value.data, nil
185173
}
186174

@@ -189,10 +177,6 @@ func setFlushedValue(ctx context.Context, index uint32, value interface{}) error
189177
if values, ok := variables.([]IndexedValue); ok {
190178
variable := indexedVariables[index]
191179
variableValue := &values[index]
192-
// should check variable.Flags
193-
if (variable.Flags() & MOSN_VAR_FLAG_NOCACHEABLE) == MOSN_VAR_FLAG_NOCACHEABLE {
194-
variableValue.noCacheable = true
195-
}
196180

197181
setter := variable.Setter()
198182
if setter == nil {

variable/api_test.go

+31-22
Original file line numberDiff line numberDiff line change
@@ -76,27 +76,6 @@ func TestGetVariableValue_normal(t *testing.T) {
7676
t.Error("Check unknown variable failed")
7777
}
7878

79-
//test variable noCacheable
80-
name = "nocache"
81-
value = "nocache Value"
82-
Register(NewStringVariable(name, nil, func(ctx context.Context, variableValue *IndexedValue, data interface{}) (s string, err error) {
83-
return value, nil
84-
}, DefaultStringSetter, MOSN_VAR_FLAG_NOCACHEABLE))
85-
ctx = NewVariableContext(context.Background())
86-
err = SetString(ctx, name, value)
87-
if err != nil {
88-
t.Error(err)
89-
}
90-
91-
vv, err = GetString(ctx, name)
92-
if err != nil {
93-
t.Error(err)
94-
}
95-
96-
if vv != value {
97-
t.Errorf("get/set nocache variable value not equal, expected: %s, acutal: %s", value, vv)
98-
}
99-
10079
}
10180

10281
func TestSetVariableValue_normal(t *testing.T) {
@@ -186,7 +165,37 @@ func TestVarNotGetterHint(t *testing.T) {
186165
ctx = NewVariableContext(ctx)
187166

188167
_, err := Get(ctx, name)
189-
assert.Equal(t, err.Error(), errGetterNotFound+name)
168+
assert.Equal(t, err.Error(), errValueNotFound+name)
169+
170+
_, err2 := Get(ctx, name)
171+
assert.Equal(t, err2.Error(), errValueNotFound+name)
172+
}
173+
174+
func TestVariableGetSetCached(t *testing.T) {
175+
name := "cache getter"
176+
cacheValue := "cached"
177+
getterCall := 0
178+
Register(NewStringVariable(name, nil, func(ctx context.Context, variableValue *IndexedValue, data interface{}) (s string, err error) {
179+
getterCall++
180+
return cacheValue, nil
181+
}, DefaultStringSetter, 0))
182+
ctx := NewVariableContext(context.Background())
183+
value, err := GetString(ctx, name)
184+
assert.Nil(t, err)
185+
assert.Equal(t, cacheValue, value)
186+
assert.Equal(t, 1, getterCall)
187+
// get from cache
188+
value, err = GetString(ctx, name)
189+
assert.Nil(t, err)
190+
assert.Equal(t, cacheValue, value)
191+
assert.Equal(t, 1, getterCall)
192+
// set will overwrite cache
193+
SetString(ctx, name, "overwrite")
194+
value, err = GetString(ctx, name)
195+
assert.Nil(t, err)
196+
assert.Equal(t, "overwrite", value)
197+
assert.Equal(t, 1, getterCall)
198+
190199
}
191200

192201
func BenchmarkGetVariableValue2(b *testing.B) {

variable/factory.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ var (
4242
errInvalidContext = "invalid context"
4343
errNoVariablesInContext = "no variables found in context"
4444
errSupportIndexedOnly = "this operation only support indexed variable"
45-
errGetterNotFound = "getter function undefined, variable name: "
4645
errSetterNotFound = "setter function undefined, variable name: "
46+
errValueNotFound = "variable value not found, variable name: "
4747
errVariableNotString = "variable type is not string"
4848
errValueNotString = "set string variable with non-string type"
4949
invalidVariableIndex = errors.New("get variable support name index or variable directly")

variable/types.go

+2-13
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,11 @@ package variable
2020
import "context"
2121

2222
const (
23-
MOSN_VAR_FLAG_CHANGEABLE = 1
24-
MOSN_VAR_FLAG_NOCACHEABLE = 2
25-
MOSN_VAR_FLAG_NOHASH = 4
26-
2723
ValueNotFound = "-"
2824
)
2925

3026
// StringGetterFunc used to get the value of string-typed variable, the implementation should handle the field
31-
// (Valid, NotFound) of IndexedValue if it was not nil, Valid means the value is valid; NotFound
32-
// means the value can not be found. It indicates that value can be cached for next-time get handle
33-
// if any one of (Valid, NotFound) is set to true.
27+
// Valid of IndexedValue if it was not nil, Valid means the value is valid.
3428
//
3529
// Function should return ValueNotFound("-") if target value not exists.
3630
// E.g. reference to the header which is not existed in current request.
@@ -61,8 +55,6 @@ type Variable interface {
6155
Name() string
6256
// variable data, which is useful for getter/setter
6357
Data() interface{}
64-
// variable flags
65-
Flags() uint32
6658
// value getter
6759
Getter() Getter
6860
// value setter
@@ -71,10 +63,7 @@ type Variable interface {
7163

7264
// IndexedValue used to store result value
7365
type IndexedValue struct {
74-
Valid bool
75-
NotFound bool
76-
noCacheable bool
77-
//escape bool
66+
Valid bool
7867

7968
data interface{}
8069
}

variable/var.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,6 @@ func (bv *BasicVariable) Data() interface{} {
9090
return bv.data
9191
}
9292

93-
// Flags returns variable's cache flag
94-
func (bv *BasicVariable) Flags() uint32 {
95-
return bv.flags
96-
}
97-
9893
// Getter is the variable's value get function if the variable contains it
9994
func (bv *BasicVariable) Getter() Getter {
10095
return bv.getter
@@ -158,5 +153,5 @@ func (g *getterImpl) Get(ctx context.Context, value *IndexedValue, data interfac
158153
return g.getter(ctx, value, data)
159154
}
160155

161-
return nil, errors.New(errGetterNotFound + g.name)
156+
return nil, errors.New(errValueNotFound + g.name)
162157
}

0 commit comments

Comments
 (0)