I am currently facing an issue with the FrozenSet in C# with .NET 8 Web API where it appears to be managing the order of data unexpectedly. Below is the method where I am encountering this behavior:
private async Task<FrozenSet<DemoDataRes>> GetDataAsync(int Id, string cacheKey, CancellationToken cancellationToken)
{
var results = _cacheProvider.Get<FrozenSet<DemoDataRes>>(cacheKey, CacheKeys.DEMOCACHEGROUP);
if (results is null)
{
results = (await (from avt in _DemoDbContext.Catalogs
where avt.Id == Id
orderby avt.Id
select new DemoDataRes
{
Id = avt.Id,
Type = avt.Name,
}
).ToListAsync(cancellationToken));
var value = results.ToFrozenSet();
_cacheProvider.Set(cacheKey, value, CacheKeys.DEMOCACHEGROUP, Defaults.CacheExpiryMinutes);
}
return results;
}
The method is supposed to asynchronously fetch data from a database context, map it to a DemoDataRes object, and store it in a cache. If the cache is empty, it retrieves the data from the database, orders it by the Id, and then converts this list to a FrozenSet before caching it.
Issue: I expect the FrozenSet not to manage or preserve the order of the items since it is typically a set that should not have any order. However, the FrozenSet seems to be preserving the order of insertion, which is not the intended behavior for sets in general.
Is this an expected behavior of FrozenSet in certain circumstances or could it be influenced by how the data is being inserted or retrieved?Are there any known issues or considerations with FrozenSet in C# that might cause it to behave like this? What are the best practices for ensuring that a collection does not manage or preserve order when caching data objects?
Can anyone please help me here by providing their guidance? Any help would be greatly appreciated.