I am trying to write two Tag helpers that are used to localize strings in razor views. The purpose of the parent tag is to gather all of the keys that are requested by the child tags and get them from the database in one batch and cache them.
Then the child tags will use the cached versions, this way I am hoping to lower the load on the database. I am looking for something like this:
<parent-tag>
<child-tag key="Hello" />
some HTML here
<child-tag key="Hi!" />
</parent-tag>
I want to be able to get a list of objects in the Parent tag's Invoke method.
I have also tried storing data in TagHelperContext to communicate with other tag helpers, but this will not work either, because I have to call output.GetChildContentAsync()
inside the Parent's Invoke method, which defeats the whole purpose of the caching.
@Encrypt0r you could have an in-memory static cache relation of the TagHelpers context.Items and your localized keys. This would involve using context.Items in your to execute GetChildContentAsync
one time. All subsequent times would then be cached by looking up the key values based off of the context.Items (assuming the key values were not dynamic).
Think of it this way:
// Inside your TagHelper
if (_cache.TryGetValue(context.UniqueId, out var localizationKeys))
{
... You already have the keys, do whatever
}
else
{
var myStatefulObject = new SomeStatefulObject();
context.Items[typeof(SomeStatefulObject)] = myStatefulObject;
await output.GetChildContentAsync();
_cache[context.UniqueId] = new LocalizationKeyStuff(myStatefulObject);
}