Replies: 1 comment
-
Hi, zen-go as well as other languages (zen-engine for node, python, c) support memory loaders / caching. When invoking zen.GetDecision it creates an in memory instance of decision model based on the loader that can be cached. Next you can cache it in map and re-use it. For example: package rules
import (
"embed"
"log"
"path"
"github.com/gorules/zen-go"
)
var decisions = make(map[string]zen.Decision)
//go:embed assets/*
var ruleFs embed.FS
func decisionLoader(key string) ([]byte, error) {
p := path.Join("assets", key)
return ruleFs.ReadFile(p)
}
func InitializeRules() {
if len(decisions) > 0 {
return
}
entries, err := ruleFs.ReadDir("assets")
if err != nil {
log.Fatal(err)
}
engine := zen.NewEngine(zen.EngineConfig{Loader: decisionLoader})
for _, e := range entries {
decision, err := engine.GetDecision(e.Name())
if err != nil {
log.Fatalf("failed to load decision file %+v", err)
} else {
decisions[e.Name()] = decision
}
}
}
func GetDecision(key string) (zen.Decision, bool) {
decision, ok := decisions[key]
return decision, ok
} This logic loads decision model (json files) from asset folder and creates in memory representation. We call InitializeRules() on startup. Next, when model is needed for evaluation you can retrieve a decision by doing decision, ok := rules.GetDecision("some key") and then decision.Evaluate(context); |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I noticed that the
rust
binding has a few options for loading whereasgo
only has the filesystem loader. Are they plans to additional loaders or am I just reading it wrong?Beta Was this translation helpful? Give feedback.
All reactions