-
-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
新增 ExpressionEngineLexer缓存机制,进一步提升性能
- Loading branch information
Showing
13 changed files
with
252 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package GoMybatis | ||
|
||
//Lexer 结果缓存 | ||
type ExpressionEngineLexerCache interface { | ||
Set(expression string,lexer interface{}) error | ||
Get(expression string) (interface{}, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package GoMybatis | ||
|
||
type ExpressionEngineLexerCacheable interface { | ||
SetUseLexerCache(use bool) error | ||
LexerCacheable() bool | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package GoMybatis | ||
|
||
import ( | ||
"github.com/zhuxiujia/GoMybatis/utils" | ||
) | ||
|
||
type ExpressionEngineLexerMapCache struct { | ||
mapCache map[string]interface{} | ||
} | ||
|
||
func (this ExpressionEngineLexerMapCache) New() ExpressionEngineLexerMapCache { | ||
if this.mapCache == nil { | ||
this.mapCache = make(map[string]interface{}) | ||
} | ||
return this | ||
} | ||
|
||
func (this *ExpressionEngineLexerMapCache) Set(expression string, lexer interface{}) error { | ||
if expression == "" { | ||
return utils.NewError("ExpressionEngineLexerMapCache", "set lexerMap chache key can not be ''!") | ||
} | ||
this.mapCache[expression] = lexer | ||
return nil | ||
} | ||
func (this *ExpressionEngineLexerMapCache) Get(expression string) (interface{}, error) { | ||
return this.mapCache[expression], nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package GoMybatis | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestExpressionEngineLexerMapCache_Get(t *testing.T) { | ||
engine := ExpressionEngineLexerMapCache{}.New() | ||
err := engine.Set("foo", 1) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func TestExpressionEngineLexerMapCache_Set(t *testing.T) { | ||
engine := ExpressionEngineLexerMapCache{}.New() | ||
err := engine.Set("foo", 1) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
result, err := engine.Get("foo") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
fmt.Println(result) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package GoMybatis | ||
|
||
import "github.com/zhuxiujia/GoMybatis/utils" | ||
|
||
type ExpressionEngineProxy struct { | ||
expressionEngine ExpressionEngine | ||
lexerCacheable bool //是否使用lexer缓存,默认false | ||
} | ||
|
||
//engine :表达式引擎,useLexerCache:是否缓存Lexer表达式编译结果 | ||
func (ExpressionEngineProxy) New(engine ExpressionEngine, useLexerCache bool) ExpressionEngineProxy { | ||
return ExpressionEngineProxy{ | ||
expressionEngine: engine, | ||
lexerCacheable: useLexerCache, | ||
} | ||
} | ||
|
||
//引擎名称 | ||
func (this ExpressionEngineProxy) Name() string { | ||
if this.expressionEngine == nil { | ||
return "" | ||
} | ||
return this.expressionEngine.Name() | ||
} | ||
|
||
//编译一个表达式 | ||
//参数:lexerArg 表达式内容 | ||
//返回:interface{} 编译结果,error 错误 | ||
func (this *ExpressionEngineProxy) Lexer(expression string) (interface{}, error) { | ||
if this.expressionEngine == nil { | ||
return nil, utils.NewError("ExpressionEngineProxy", "ExpressionEngineProxy not init for ExpressionEngineProxy{}.New(...)") | ||
} | ||
if this.expressionEngine.LexerCache() != nil && this.lexerCacheable { | ||
//如果 提供缓存,则使用缓存 | ||
cacheResult, cacheErr := this.expressionEngine.LexerCache().Get(expression) | ||
if cacheErr != nil { | ||
return nil, cacheErr | ||
} | ||
if cacheResult != nil { | ||
return cacheResult, nil | ||
} | ||
} | ||
var result, err = this.expressionEngine.Lexer(expression) | ||
if this.expressionEngine.LexerCache() != nil && this.lexerCacheable { | ||
//如果 提供缓存,则使用缓存 | ||
this.expressionEngine.LexerCache().Set(expression, result) | ||
} | ||
return result, err | ||
} | ||
|
||
//执行一个表达式 | ||
//参数:lexerResult=编译结果,arg=参数 | ||
//返回:执行结果,错误 | ||
func (this *ExpressionEngineProxy) Eval(lexerResult interface{}, arg interface{}, operation int) (interface{}, error) { | ||
if this.expressionEngine == nil { | ||
return nil, utils.NewError("ExpressionEngineProxy", "ExpressionEngineProxy not init for ExpressionEngineProxy{}.New(...)") | ||
} | ||
return this.expressionEngine.Eval(lexerResult, arg, operation) | ||
} | ||
|
||
func (this *ExpressionEngineProxy) LexerCache() ExpressionEngineLexerCache { | ||
if this.expressionEngine == nil { | ||
return nil | ||
} | ||
return this.expressionEngine.LexerCache() | ||
} | ||
|
||
func (this *ExpressionEngineProxy) SetUseLexerCache(isUseCache bool) error { | ||
this.lexerCacheable = isUseCache | ||
return nil | ||
} | ||
func (this *ExpressionEngineProxy) LexerCacheable() bool { | ||
return this.lexerCacheable | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package GoMybatis | ||
|
||
import ( | ||
"fmt" | ||
"github.com/zhuxiujia/GoMybatis/example" | ||
"testing" | ||
) | ||
|
||
func TestExpressionEngineProxy_Eval(t *testing.T) { | ||
var engine=ExpressionEngineProxy{}.New(&ExpressionEngineExpr{},false) | ||
var lexer,err=engine.Lexer("foo") | ||
if err!= nil{ | ||
t.Fatal(err) | ||
} | ||
var arg=make(map[string]interface{}) | ||
arg["foo"]="Bar" | ||
result,err:=engine.Eval(lexer,arg,0) | ||
if err!= nil{ | ||
t.Fatal(err) | ||
} | ||
if result.(string) != "Bar"{ | ||
t.Fatal("result != 'Bar'") | ||
} | ||
fmt.Println(result) | ||
} | ||
|
||
func TestExpressionEngineProxy_Lexer(t *testing.T) { | ||
var engine=ExpressionEngineProxy{}.New(&ExpressionEngineExpr{},false) | ||
var _,err=engine.Lexer("foo") | ||
if err!= nil{ | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func BenchmarkExpressionEngineProxy_Eval(b *testing.B) { | ||
b.StopTimer() | ||
var activity = example.Activity{ | ||
Id: "1", | ||
DeleteFlag: 1, | ||
} | ||
|
||
var engine = ExpressionEngineProxy{}.New(&ExpressionEngineExpr{},false) | ||
var evaluateParameters = make(map[string]interface{}) | ||
|
||
evaluateParameters["activity"] = &activity | ||
|
||
b.StartTimer() | ||
for i := 0; i < b.N; i++ { | ||
var expression = "activity.DeleteFlag == 1 and activity.DeleteFlag != 0 " | ||
evalExpression, err := engine.Lexer(expression) | ||
if err != nil { | ||
b.Fatal(err) | ||
} | ||
result, err := engine.Eval(evalExpression, evaluateParameters, 0) | ||
if err != nil { | ||
b.Fatal(err) | ||
} | ||
if result.(bool) { | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.