有没有办法在OPA中存储值

发布于 2025-01-12 13:42:17 字数 238 浏览 4 评论 0原文

我有一个用例,如果已经定义了变量,则返回该值,否则调用休息端点来获取该变量。

get_value = value {
   data.value
   value
}else = value {
    value := <> #invoke rest
}

我将运行 OPA 作为服务器,我的期望是,在第一次调用中它将转到 else 块,然后转到第一个块以进行其余的调用。请你帮助我好吗

I have a usecase to do like, if a variable is already defined then return that value else invoke a rest endpoint to get the variable.

get_value = value {
   data.value
   value
}else = value {
    value := <> #invoke rest
}

I will be running OPA as a server and my expectation is like, in the first invokation it will go to the else block then to the first block for rest of the calls. Could you please help me

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情栀口红 2025-01-19 13:42:17

您无法从策略写入内存存储,不。除此之外,您的代码看起来不错,而且我想说,在通过 http.senddata 中是否存在值是一种非常常见的模式代码>.但需要注意的一件事是,您可以在 http.send 调用上使用缓存,这将有效地执行与您相同的操作:

value := http.send({
    "url": "https://example.com",
    "method": "get",
    
    # Cached for one hour
    "force_cache": true,
    "force_cache_duration": 3600
})

如果服务器使用缓存标头进行响应,您甚至不会需要使用 force_cache 但可以简单地说 cache: true 并且 http.send 客户端将根据服务器的建议进行缓存。

You cannot write to the in-memory store from a policy, no. Except for that though, your code looks fine, and I'd say it's a pretty common pattern to first check for the existence of a value in data before fetching it via http.send. One thing to note though is that you may use caching on the http.send call, which would effectively do the same thing you're after:

value := http.send({
    "url": "https://example.com",
    "method": "get",
    
    # Cached for one hour
    "force_cache": true,
    "force_cache_duration": 3600
})

If the server responds with caching headers, you won't even need to use force_cache but can simply say cache: true and the http.send client will cache according to what the server suggests.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文