-1

I have a method in an ASP.NET application which gets hit a lot and needs to be runtime cached. It accepts the following:

public List<ModelTwo> SomeMethod(List<ModelOne> models, List<Guid> guids)

I can loop through each list selecting unique values and concatenating into a large string. But I'm wondering if there is a faster and more efficient way of doing this?

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
YodasMyDad
  • 9,248
  • 24
  • 76
  • 121
  • 1
    Unclear. Why a large string is a viable approach at all? Why you don't store the list(s) in the cache? How are both lists related to each other? What is `ModelTwo`? – Tim Schmelter May 08 '18 at 13:46
  • String because I'm using the runtime method CacheItem. https://msdn.microsoft.com/en-us/library/system.runtime.caching.cacheitem(v=vs.110).aspx – YodasMyDad May 08 '18 at 13:49
  • 1
    "I can loop through each list selecting unique values and concatenating into a large string", there is something known as serialization that's normally used for this kind of problems – Camilo Terevinto May 08 '18 at 13:55
  • It sounds like you just want the unique values from your lists to be cached. I'd create a list of these unique values and set the CacheItem.Value to the new list, one CacheItem for each list. There's no need to create a large string. – anu start May 08 '18 at 13:57
  • @CamiloTerevinto there certainly is, however this would create an even larger string for the cache key. – YodasMyDad May 08 '18 at 14:17
  • @leen3o: why the cache key can't be the `Guid.ToString()`? – Tim Schmelter May 08 '18 at 14:18
  • It's two lists of objects. One my own models and one guids. If a guid changes or a property in ModelOne then the cachekey will need to change. – YodasMyDad May 08 '18 at 14:20

1 Answers1

0

If these two items are somehow related, you could just create a class to group them together with a unique id. You might have to edit some of this code (I haven't compiled this), but you can get a basic idea of how you can relate the two collections with a unique id(the key) and use that key for a cache. This would be more efficient than looping through the lists and concatenating it into a large string.

Strings are immutable. Meaning they can't be changed once they are created. So every time you're concatenating, you are in fact creating a new string. Which might slow down performance if you have a lot of values.

public class MyValue
{
    public guid key {get;set;}
    public List<ModelOne> models {get;set;}
    public List<Guid> guids {get;set;}

    public void MyValue(List<ModelOne> modelsIn, List<Guid> guidsIn)
    {
        key = Guid.NewGuid();
        models = modelsIn;
        guids = guidsIn;
    }
}

public Dictionary<Guid,MyValue> Cache = new Dictionary<Guid,MyValue>(); 

public List<ModelTwo> SomeMethod(MyValue valueIn)
{
   MyValue val;
   If(Cache.TryGetValue(valueIn.Key, value)
      return Cache[valueIn.Key].models;
   else
   {
      Do Stuf...
      put in cache...
      return value;
   }

}
A.sharif
  • 1,977
  • 1
  • 16
  • 27
  • Thanks but this is not possible. – YodasMyDad May 08 '18 at 14:17
  • 1
    @leeno What are the restrictions that my solutions fails on? Do you just need to be able to make a key from a list? if so this post explains on how to get a key from a list: https://stackoverflow.com/questions/7278136/create-hash-value-on-a-list – A.sharif May 08 '18 at 14:23