在运行时更改Perl Memoize缓存

发布于 2025-02-07 22:09:15 字数 347 浏览 2 评论 0原文

我有一种从文件中读取标志的方法,并记忆了。

use Memoize;

my %cache;
memoize('readFlag',  SCALAR_CACHE => [HASH => \%cache]);
sub readFlag {
 # read flag & return
}

备忘录正常,我总是得到缓存的价值。现在,我想在运行时修改此缓存条目和任何运行脚本以获取更改。尝试使用该方法的指针,但是仅在哈希图中创建另一个条目并设置该值但不会更新现有值。

$cache{\readFlag} = newVal;

有人可以帮我吗?

I have a method that reads a flag from a file and it is memoized.

use Memoize;

my %cache;
memoize('readFlag',  SCALAR_CACHE => [HASH => \%cache]);
sub readFlag {
 # read flag & return
}

Memoize works fine and I get the cached value always. Now, I want to modify this cache entry during runtime and any running script to pick up the change. Tried using a pointer to the method, however that just creates another entry in the hashmap and sets the value but does not update the existing value.

$cache{\readFlag} = newVal;

Can someone help me out?

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

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

发布评论

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

评论(1

找个人就嫁了吧 2025-02-14 22:09:15

缓存将被键入参数(不是您认为\ readflag做的任何内容)。

具体来说,

默认标准器只是将参数与字符28在

之间

,因此,如果您想将其进行readflag(“ A”,“ B”)返回c,您将使用

$cache{ join chr(28), "a", "b" } = "c";

请注意,这不会影响“任何运行脚本”,只是此过程。缓存仅存在于此过程中。您可以采取步骤使缓存持久,但是您没有显示任何类型的东西。

The cache would be keyed on parameters (not whatever you think \readFlag does).

Specifically,

The default normalizer just concatenates the arguments with character 28 in between

So if you wanted to make it so readFlag("a", "b") returns c, you would use

$cache{ join chr(28), "a", "b" } = "c";

Note that this won't affect "any running script", just this process. The cache only exists in the process. You can take steps to make the cache persistent, but you didn't show anything of the kind.

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