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

Remove item from DefaultCache #235

Closed
jpolvora opened this issue Feb 8, 2015 · 1 comment
Closed

Remove item from DefaultCache #235

jpolvora opened this issue Feb 8, 2015 · 1 comment
Labels

Comments

@jpolvora
Copy link

jpolvora commented Feb 8, 2015

It would be useful if we could just remove an item from the cache.

  if (loadedFromDb)
                        {
                            //remove item from cache
                            //Engine.Razor.CachingProvider.Remove(templateKey); <<<<<< ?
                            model.Content = Engine.Razor.RunCompile(model.Markup, templateKey, typeof(PageModel), model);
                        }
                        else
                        {
                            model.Content = Engine.Razor.IsTemplateCached(templateKey, typeof(PageModel))
                                ? Engine.Razor.Run(templateKey, typeof(PageModel), model)
                                : Engine.Razor.RunCompile(model.Markup, templateKey, typeof(PageModel), model);
                        }

Today what I'm doing is invalidating all cached templates by just creating another instance of RazorEngineService. By I don't know how this will affect memory usage, because the previously cached templates probably will remain in memory.

Any ideas ?

@matthid
Copy link
Collaborator

matthid commented Feb 8, 2015

There in no way to do that with the default configuration and there is a good reason why. But first to your problem: The way to do what you want to do is to implement a custom ITemplateManager and a custom ICachingProvider, depending on your needs that will simplify your code to

model.Content = Engine.Razor.RunCompile(templateKey, typeof(PageModel), model);

When your template source changes the TemplateManager should notify the CachingProvider and invalidate the cache.

But now a warning to the memory usage: You will never get your memory back this way. There is no way to implement a CachingProvider which gives you back the memory, because templates are basically loaded assemblies. And as you might know there is no way to unload loaded assemblies.
So if you need unloading templates very often you should use Isolation and a custom logic for re-use (for example unload the domain, if you have 100 stale assemblies or something like that)

There is also a quick-fix posted here, but I don't recommend using it (see my post below).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants