RickStrahl / Westwind.Globalization

Database driven resource localization for .NET applications
543 stars 135 forks source link

DB Not Caching Results #137

Open tbasallo opened 6 years ago

tbasallo commented 6 years ago

I was under the impression that the resources were cached on first load, but looking at the logs it seems that resources are loaded on every page load. Is there a setting that may have disabled this? Or do I need to enable? I looked and can't seem to find anything.

But get multiple calls per page, every time.

This file seems like caching is implemented, https://github.com/RickStrahl/Westwind.Globalization/blob/701eb72f0c85f09b3102feb1334d3466b9e87708/src/Net45/Westwind.Globalization.Web/DbSimpleResourceProvider/DbSimpleResourceProvider.cs

Environment: Azure ASP.NET Core 2.1

SQL: select Value,Type from Localizations where ResourceId=@ResourceId and ResourceSet=@ResourceSet and LocaleId=@LocaleId

Config:

            var supportedCultures = new[] { new CultureInfo("en-US"), new CultureInfo("en"), new CultureInfo("es") };
            services.Configure<RequestLocalizationOptions>(options =>
            {
                options.DefaultRequestCulture = new RequestCulture(culture: supportedCultures.First(), uiCulture: supportedCultures.First());
                options.SupportedCultures = supportedCultures;
                options.SupportedUICultures = supportedCultures;
                options.RequestCultureProviders = new List<IRequestCultureProvider>();
                options.RequestCultureProviders.Add(new CookieRequestCultureProvider());
            });
            services.AddLocalization(options => { options.ResourcesPath = "Properties"; });
            services.AddWestwindGlobalization(opt =>
            {
                opt.ResourceAccessMode = ResourceAccessMode.DbResourceManager;
                opt.ConnectionString = app.DbConnectionString;
                opt.DataProvider = DbResourceProviderTypes.SqlServer;
                opt.ResourceTableName = "Localizations";
                opt.ResourceBaseNamespace = "AppResources";
                opt.AddMissingResources = true;
            });
RickStrahl commented 6 years ago

Are you sure it's the same resources you are seeing being loaded in those SQL statements? Resources load as they are needed one resourceset and locale at a time, so you will see SQL commands for each resource set and each language being loaded like this once. It doesn't all load at once. IOW - lots of activity up front, then from cache.

tbasallo commented 6 years ago

Yeah - the same exact page calls into the DB with every refresh.

RickStrahl commented 6 years ago

Any chance to you can step into that code? I've checked this in the past and do not see this behavior. The ResourceSet should be caching unless .NET Core is not holding on to it and releasing it somewhere.

I'm curious to see a call stack when it reloads on a second request with the same resources set and same locale.

tbasallo commented 6 years ago

I'll be working on this and see what I find. This is running about 3 SQL calls on every page load - they're fast at only 1ms, but... :)

RickStrahl commented 6 years ago

Yes this is definitely not right - but I don't think this is the provider that's doing it - it must be the runtime that's recreating the resource sets.

I can't look at this for the next couple of weeks I have too much going on, but will take a look when my time frees up.

tbasallo commented 6 years ago

OK - I think I found all the culprits. Not sure if they're bugs or by design, so I didn't look for where it's happening or a fix to PR.

MNF commented 6 years ago

@tbasallo, By default in DbResourceProvider IgnoreCase =true. https://github.com/RickStrahl/Westwind.Globalization/blob/701eb72f0c85f09b3102feb1334d3466b9e87708/src/Net45/Westwind.Globalization.Web/DbResourceProvider/DbResourceProvider.cs#L93

As you are using DbSimpleResourceProvider, you probably need to set it by yourself like in https://github.com/RickStrahl/Westwind.Globalization/issues/81#issuecomment-260245090

Also ensure that your DB is not case-sensitive.