RazorEngine 取消缓存编译模板 [英] RazorEngine un-cache compiled templates

查看:75
本文介绍了RazorEngine 取消缓存编译模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我使用 RazorEngine v2.1 作为发送模板化电子邮件(数千封)的后台进程的一部分.为了加快速度,模板使用它们的 md5 sum 作为名称进行编译.这使得当模板更改时,它会被重新编译,并且所有使用该模板的电子邮件都能够使用相同的编译模板.我在列表中跟踪已编译模板的名称,以便我知道何时再次调用 compile(并执行其他一些操作).

Currently, I am using RazorEngine v2.1 as part of a background process that sends templated emails (thousands of them). To speed things up, the templates are compiled with their md5 sum as a name. This makes it so that when a template is changed, it is re-compiled and all emails using the template are able to use the same compiled template. I keep track of the names of the compiled templates in a list so that I know when to call compile again (and do a few other things).

问题:我发现经过很长时间和大量模板修改后,所有这些缓存的编译模板可能仍在内存中,因为看起来它们正在被存储在 dynamic 中.对于这个特定的进程,它可能会一次运行数月而不重启,如果所有以前版本的模板仍然存在,这可能构成严重的内存泄漏.

The problem: It has occurred to me that after a really long time and after lots of template modifications that all these cached compiled templates will probably still be in memory since it looks like they are being stored in a dynamic. For this particular process, which might run for months at a time without a restart, this could constitute a serious memory leak if all the previous versions of templates are still hanging around.

问题:有没有办法取消缓存旧模板,使它们不再存在于动态中?

The question: Is there a way to un-cache old templates so that they are no longer hanging around in the dynamic?

例如,如果我能够自己保留已编译的模板对象,并在我想使用它们时将它们传递给 RazorEngine,我可以决定何时将它们扔掉,这将消除内存泄漏.但是,如果 RazorEngine 已经有办法解决这个问题,那么了解它也会很方便,因为我在互联网上找不到很多关于这个特定问题的参考.关于为什么应该使用编译模板来减少内存使用的原因有很多,但我很难找到有关在长期应用程序中积累的大量未使用的编译模板的任何信息.

If, for example, I was able to keep the compiled template objects myself and just pass them into RazorEngine when I wanted to use them I could decide when to toss them out and that would eliminate the memory leak. However, if RazorEngine already has a way around this then knowing about that would be handy as well since I can't find very many references to this particular problem on the internet. There are plenty of things about why compiled templates should be used to reduce memory usage, but I had a hard time finding anything about tons and tons of unused compiled templates accumulating in a long-lived application.

我刚刚阅读了一些关于缓存如何工作的内容,如果使用不同的模板传入相同的名称,它将重新缓存它并丢弃旧的.然而,这里的问题仍然存在,因为随着时间的推移,电子邮件将被添加和删除,随着时间的推移,所有已删除的旧电子邮件仍然存在(即使它不会存储模板的每个版本的副本).

I have just read a bit about how the caching works and that if the same name is passed in with a different template it will re-cache it and discard the old one. The problem here still remains, however, since over the course of time emails will be added and removed and with time all the old removed emails will still be around (even though it won't be storing copies of each version of the template).

推荐答案

回答这个是因为它似乎仍然与某些人相关.(https://github.com/Antaris/RazorEngine/issues/232#issuecomment-128802285)

Answering this because it still seems to be relevant for some people. (https://github.com/Antaris/RazorEngine/issues/232#issuecomment-128802285)

对于这个特定的进程,它可能会一次运行数月而不重启,如果所有以前版本的模板仍然存在,这可能构成严重的内存泄漏.

For this particular process, which might run for months at a time without a restart, this could constitute a serious memory leak if all the previous versions of templates are still hanging around.

当您更改和重新编译模板时,您会出现内存泄漏,因为您无法卸载已加载的程序集(RazorEngine 在后台为您编译和加载).

When you change and recompile templates you have a memory leak, because you cannot unload loaded assemblies (which RazorEngine compiles and loads for you in the background).

真正释放内存的唯一方法是重新加载 AppDomain 或重新启动进程.

The only way to really free the memory is to reload the AppDomain or restart the process.

其他答案似乎在谈论较新版本,这些版本可以防止默认配置中的内存泄漏(让您意识到问题),并且需要一些自定义配置才能使用另一个模板代码重新编译密钥.请注意,所有其他答案实际上都会增加内存消耗!

The other answers seem to talk about newer versions which prevent memory leaks in the default configuration (to make you aware of the problem) and require some custom configuration to be able to recompile a key with another template code. Note that all the other answers will actually increase memory consumption!

马蒂德,RazorEngine 贡献者

matthid, a RazorEngine contributor

这篇关于RazorEngine 取消缓存编译模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆