Skip to content

Commit

Permalink
Merge pull request #7 from CoverWhale/cache
Browse files Browse the repository at this point in the history
add interquerycache
  • Loading branch information
hooksie1 authored Dec 10, 2024
2 parents f3d8ae0 + 63a0413 commit e3df972
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions nopa.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/open-policy-agent/opa/rego"
"github.com/open-policy-agent/opa/storage"
"github.com/open-policy-agent/opa/storage/inmem"
"github.com/open-policy-agent/opa/topdown/cache"
)

var (
Expand All @@ -48,6 +49,7 @@ type Agent struct {
astFunc func(*rego.Rego)
Compiler *ast.Compiler
Modifiers []BundleModifyFunc
Cache cache.InterQueryCache
}

type AgentOpts struct {
Expand All @@ -59,6 +61,8 @@ type AgentOpts struct {
}

func NewAgent(opts AgentOpts) *Agent {
config, _ := cache.ParseCachingConfig(nil)
interQueryCache := cache.NewInterQueryCache(config)
a := &Agent{
BundleName: opts.BundleName,
ObjectStore: opts.ObjectStore,
Expand All @@ -67,6 +71,7 @@ func NewAgent(opts AgentOpts) *Agent {
OPAStore: inmem.New(),
Compiler: ast.NewCompiler(),
Modifiers: opts.Modifiers,
Cache: cache.InterQueryCache(interQueryCache),
}
if opts.Env != nil {
a.SetRuntime()
Expand Down Expand Up @@ -186,6 +191,7 @@ func (a *Agent) Eval(ctx context.Context, input []byte, pkg string) ([]byte, err
rego.Transaction(txn),
rego.Store(a.OPAStore),
rego.ParsedInput(data),
rego.InterQueryBuiltinCache(a.Cache),
a.astFunc,
)

Expand All @@ -198,6 +204,7 @@ func (a *Agent) Eval(ctx context.Context, input []byte, pkg string) ([]byte, err
results, err := prepared.Eval(ctx,
rego.EvalParsedInput(data),
rego.EvalTransaction(txn),
rego.EvalInterQueryBuiltinCache(a.Cache),
)
if err != nil {
a.Logger.Error(err)
Expand Down

0 comments on commit e3df972

Please sign in to comment.