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

feat(test): Enable inter-query cache #1073

Merged
merged 1 commit into from
Feb 25, 2025
Merged
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
23 changes: 16 additions & 7 deletions policy/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,21 @@ import (
"github.com/open-policy-agent/opa/storage"
"github.com/open-policy-agent/opa/storage/inmem"
"github.com/open-policy-agent/opa/topdown"
"github.com/open-policy-agent/opa/topdown/cache"
"github.com/open-policy-agent/opa/topdown/print"
"github.com/open-policy-agent/opa/version"
)

// Engine represents the policy engine.
type Engine struct {
trace bool
builtinErrors bool
modules map[string]*ast.Module
compiler *ast.Compiler
store storage.Store
policies map[string]string
docs map[string]string
trace bool
builtinErrors bool
modules map[string]*ast.Module
compiler *ast.Compiler
store storage.Store
policies map[string]string
docs map[string]string
enableInterQueryCache bool
}

// CompilerOptions defines the options for the Rego compiler.
Expand Down Expand Up @@ -171,6 +173,10 @@ func (e *Engine) ShowBuiltinErrors() {
e.builtinErrors = true
}

func (e *Engine) EnableInterQueryCache() {
e.enableInterQueryCache = true
}

// Check executes all of the loaded policies against the input and returns the results.
func (e *Engine) Check(ctx context.Context, configs map[string]any, namespace string) ([]output.CheckResult, error) {
var checkResults []output.CheckResult
Expand Down Expand Up @@ -454,6 +460,9 @@ func (e *Engine) query(ctx context.Context, input any, query string) (output.Que
rego.PrintHook(ph),
rego.BuiltinErrorList(builtInErrors),
}
if e.enableInterQueryCache {
options = append(options, rego.InterQueryBuiltinCache(cache.NewInterQueryCacheWithContext(ctx, nil)))
}

regoInstance := rego.New(options...)
resultSet, err := regoInstance.Eval(ctx)
Expand Down
1 change: 1 addition & 0 deletions runner/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func (t *TestRunner) Run(ctx context.Context, fileList []string) ([]output.Check
if err != nil {
return nil, fmt.Errorf("load: %w", err)
}
engine.EnableInterQueryCache()

if t.Trace {
engine.EnableTracing()
Expand Down
Loading