Cache
Providers ending with Cached, e.g. .LocalizableTextCached, contain an internal cache which may need to be flushed. An individal provider or the whole localization context can be casted to ICached to get access for cache invalidation.
// Create localization
ILocalization localization = Localization.CreateDefault();
// Create text using caches
ILocalizedText text = localization.LocalizableTextCached["Namespace.Apples"];
// Print
WriteLine(text.Print(null, new object[] { 3 }));
// Flush cache
(localization as Avalanche.Utilities.ICached)?.InvalidateCache(deep: true);
If embedded resources or resource managers are read and cached, and more assemblies are loaded into AppDomain that provide lines to already queried and cached namespaces, then caches need to be flushed. This is a rare case, but one way solve this is to have an AppDomain.AssemblyLoad event to flush the cache automatically. Note though, that this lowers performance if assemblies are loaded often.
// Create localization
ILocalization localization = Localization.CreateDefault()
.AddFileSystem(LocalizationFileSystem.ApplicationRoot)
.AddFileFormats(LocalizationFileFormatYaml.Instance, LocalizationFileFormatXml.Instance, LocalizationFileFormatJson.Instance)
.AddFilePatterns(LocalizationFilePatterns.ResourcesFolder) // "Resources/{Key}", "Resources/{Culture}/{Key}"
.AddFileSystemWithPattern(LocalizationFileSystemEmbedded.AppDomain, LocalizationFilePatterns.ResourcesEmbedded) // "*/*.Resources.{Key}", "*/*.Resources.{Culture}.{Key}", "*/{Key}", "*/{Key}.{Culture}"
.AddResourceManagerProvider();
// Add cache flush on assembly load
AppDomain.CurrentDomain.AssemblyLoad += (object? sender, AssemblyLoadEventArgs args) => (localization as ICached)?.InvalidateCache(true);
// Create text using caches
ILocalizedText text = localization.LocalizableTextCached["Namespace.Apples"];
// Print
WriteLine(text.Print(null, new object[] { 3 }));
// Load assembly and have localization cache flushed
Assembly a = Assembly.Load("System.xml");
The providers that do not end with Cached, e.g. .LocalizableText, do not cache anything. On every invocation they scan, read, parse, etc each localization file again. These providers reflect modifications at run-time and can be used for development purposes.
// Create localization
ILocalization localization = Localization.CreateDefault();
// Create text without any caches
ILocalizedText text = localization.LocalizableText["Namespace.Apples"];
// Print
WriteLine(text.Print(null, new object[] { 3 }));
Full Example
Full example
using System.Reflection;
using Avalanche.Localization;
using Avalanche.Utilities;
using static System.Console;
class cache
{
public static void Run()
{
{
// <01>
// Create localization
ILocalization localization = Localization.CreateDefault();
// Create text using caches
ILocalizedText text = localization.LocalizableTextCached["Namespace.Apples"];
// Print
WriteLine(text.Print(null, new object[] { 3 }));
// Flush cache
(localization as Avalanche.Utilities.ICached)?.InvalidateCache(deep: true);
// </01>
}
{
// <02>
// Create localization
ILocalization localization = Localization.CreateDefault()
.AddFileSystem(LocalizationFileSystem.ApplicationRoot)
.AddFileFormats(LocalizationFileFormatYaml.Instance, LocalizationFileFormatXml.Instance, LocalizationFileFormatJson.Instance)
.AddFilePatterns(LocalizationFilePatterns.ResourcesFolder) // "Resources/{Key}", "Resources/{Culture}/{Key}"
.AddFileSystemWithPattern(LocalizationFileSystemEmbedded.AppDomain, LocalizationFilePatterns.ResourcesEmbedded) // "*/*.Resources.{Key}", "*/*.Resources.{Culture}.{Key}", "*/{Key}", "*/{Key}.{Culture}"
.AddResourceManagerProvider();
// Add cache flush on assembly load
AppDomain.CurrentDomain.AssemblyLoad += (object? sender, AssemblyLoadEventArgs args) => (localization as ICached)?.InvalidateCache(true);
// Create text using caches
ILocalizedText text = localization.LocalizableTextCached["Namespace.Apples"];
// Print
WriteLine(text.Print(null, new object[] { 3 }));
// Load assembly and have localization cache flushed
Assembly a = Assembly.Load("System.xml");
// </02>
}
{
// <03>
// Create localization
ILocalization localization = Localization.CreateDefault();
// Create text without any caches
ILocalizedText text = localization.LocalizableText["Namespace.Apples"];
// Print
WriteLine(text.Print(null, new object[] { 3 }));
// </03>
}
}
}