0

When we update any object after retrieving it from memorycache it also got updated in memory cache. Is there any solution where we can remove the reference from memorycache, and when any update happens on the object then it never update the object values in memorycache?

GPS
  • 29
  • 3
  • 1
    Make a deep copy of the object. – Sani Huttunen Jul 17 '18 at 18:48
  • If class has many nested objects then deep copy wont be a ideal solution e.g Class A { void A() { B= new B(); C=new C(); } } I can do this with serialize / deserialize using JSON.NET but that would have an impact on performance.There should be some way in Memory cache to stop auto update. – GPS Jul 17 '18 at 21:05

1 Answers1

0

If you have that object's source code, you can use MemberwiseClone(), that method is protected. You can use only in where you inherited.

Just add that code your class,

  public class MyClass
    {
        public MyClass Copy()
        {
            return (MyClass)MemberwiseClone();
        }
    }

You can use Copy method for your object. That will create a new instance with same values. You can visit: What is the method MemberwiseClone() doing?

enisn
  • 625
  • 7
  • 14
  • That would only works if we have no nested object under parent class . like: Class A { void A() { B= new B(); C=new C(); } } In above scenario it wont create a copy of B and C object and if i update any value on B and C it will auto update on parent cache object. – GPS Jul 17 '18 at 21:00