dimanche 18 octobre 2020

Looking for simple way to implement cache with dictionary

I have below code where i am looping through the materials and creating osm model object and adding that to list of materials(material vector), and this is working fine.

Here i am adding creating duplicate osm model objects because of duplicate materials. so to avoid this i am implementing a generic concrete class that has a regular dictionary to cache the objects, and a Func factory parameter to create new items if not found in the cache

    public Construction AddToOsm(Model model)
    {
        if (model is null)
        {
            throw new ArgumentNullException(nameof(model));
        }

        var construction = new Construction(model);
        construction.setName($"{this.Name} - {this.AshraeClimateZone?.Name}");

        using var materials = new MaterialVector();
        foreach (var glazingMaterial in this.Layers)
        {
            if (glazingMaterial != default)
            {
                using var material = glazingMaterial.AddToOsm(model); // here i am creating duplicate osm model objects 
                materials.Add(material);
            }
        }

        construction.setLayers(materials);
        return construction;
    }

and below is the class where i am implementing that check for creating or retrieving objects

public class CacheObject<K, T>
{
    public CacheObject() { }

    private Dictionary<K, T> cache = new Dictionary<K, T>(); 
    
   // here is where i am stuck with that 

}

i am struck at this and i am not sure how to proceed further to check whether the OSM object is already available or not and essentially i need to move that adding osm object to this class or any other way to check without moving that adding osm object into cacheObject class

Could any one please suggest any ideas on this to proceed further that would be very grateful to me.

many thanks in advance

Aucun commentaire:

Enregistrer un commentaire