DEV Community

Discussion on: How can I cache output of a method's parameters, so I don't have to create a new object when the input(s) are the same?

Collapse
 
baenencalin profile image
Calin Baenen

What if I want to retain the original value(s) of the parameters, like Python's LRU cache?

Collapse
 
podobaas profile image
Aleksey Podoba • Edited

I'm not good at Python, but if I understood you correctly, you have need retain Type type and string val into cache. In this case you can define a new class and implements properties for these params or use Tuple types.
ASP.NET Core or .NET 5 have ready implementation of cache

For example

    public class MyParams
    {
        public MyParams(Type type, string val)
        {
            Type = type;
            Val = val;
        }

        public Type Type { get; }

        public string Val { get; }
    }
Enter fullscreen mode Exit fullscreen mode

Or for C# 9

    public class MyParams
    { 
        public Type Type { get; init;}

        public string Val { get; init;}
    }
Enter fullscreen mode Exit fullscreen mode
public JankyToken Create(MyParams myParams)
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
baenencalin profile image
Calin Baenen

Okay. Thanks for your help.