nager / Nager.PublicSuffix

.NET public suffix domain parser
MIT License
153 stars 21 forks source link

Introduce MemoryCacheProvider #74

Open stevcooo opened 3 months ago

stevcooo commented 3 months ago

Description This PR extends the Nager.PublicSuffix to include a MemoryCache option. Due to Azure restrictions that prevent using LocalFileCache, I implemented a MemoryCache to ensure efficient caching within the application.

Changes I added a new MemoryCache class to handle in-memory caching. Integrated MemoryCache into the existing caching mechanism, allowing it to be used as an alternative to LocalFileCache. I updated relevant documentation to reflect the new caching option. Added unit tests for the MemoryCache to ensure reliability and performance. Motivation and Context Azure's restrictions on using LocalFileCache necessitated an alternative caching strategy. The MemoryCache provides a viable solution, enabling caching functionality while complying with Azure's limitations. This improvement will enhance performance and maintain the flexibility of the caching system within the library.

How Has This Been Tested? Added unit tests specifically for MemoryCache to verify its functionality. Extended the Program.cs to make a test in a similar fashion as it's done with the LocalFile cache.

tinohager commented 3 months ago

Does the cache even come into play here? As far as I know, an Azure function only remains in memory for a few minutes. After that, a “cold start” is performed. Here it would be sufficient to define a single tone for the DomainParser.

stevcooo commented 3 months ago

I see your point about the AzureFunction. Using a memory cache implementation will allow consumers to take full advantage of the dependency injection, the opposite of what will happen in declaring an instance of DomainParser inside of the function. In our case, we have a huge AzureFunction, that executes every two hours. During each execution, we need to extract the RegistrableDomain for several hundred records. In this case, the memory cache is a nice way to avoid using the default LocalFileSystemCacheProvider.

milkyjoe90 commented 3 months ago

Would another/better option here be to implement DistributedCache, which would allow for in memory or the myriad of other supported caching options like Redis, SQL, Cosmos etc without requiring anything more from the PublicSuffix library itself?

https://learn.microsoft.com/en-us/aspnet/core/performance/caching/distributed?view=aspnetcore-8.0