Mvc 缓存禁用对象更改跟踪
Mvc Caching disable object changes tracking
我在我的 mvc 5 网站中使用缓存,
我在缓存中有一个对象可用。
当我得到这个对象时,让我们从缓存中调用它 object1,并将它复制到另一个对象,让我们调用它 object2。
我在 object2 上执行的每个更改都会自动反映到 object1 和缓存的对象
现在,当我再次从缓存中获取对象时,由于更改跟踪,它会根据我对 objec2 所做的更改进行修改,而我不需要它
我怎样才能避免反映对缓存的更改
这是我的代码
public class HomeController : Controller
{
public ActionResult Index()
{
//a model with 10 adds
Model model = new Model()
{
pageName = "test",
ads = new List<Ads>()
{
new Ads() {id = 1, image = "1" },
new Ads() {id = 2, image = "2" },
new Ads() {id = 3, image = "3" },
new Ads() {id = 4, image = "4" },
new Ads() {id = 5, image = "5" },
new Ads() {id = 6, image = "6" },
new Ads() {id = 7, image = "7" },
new Ads() {id = 8, image = "8" },
new Ads() {id = 9, image = "9" },
new Ads() {id = 10, image = "10" },
},
};
//cache it
HttpContext.Cache.Insert("demo", model, null, DateTime.Now.AddMinutes(1), Cache.NoSlidingExpiration);
//get cached object
Model object1 = HttpContext.Cache.Get("demo") as Model;
// => 10 items
Console.WriteLine(model.ads.Count());
//just get 3 items of that list
Model object2 = object1; // disable changes tracking here
object2.ads = object2.ads.Take(3).ToList();
//this changes will be reflected to cached object, i need to disable this
//get cached object (from cache) again
Model newCachedModel = HttpContext.Cache.Get("demo") as Model;
Console.WriteLine(newCachedModel.ads.Count());//3 items only
//note i have never change the cached object, the changes reflected from modelToReturn (using changes tracking feature in c#)
return View(object2);
}
}
public class Model
{
public string pageName { get; set; }
public List<Ads> ads { get; set; }
}
public class Ads
{
public int id { get; set; }
public string image { get; set; }
}
我找到了解决办法
在对其进行任何更改之前,只需克隆该对象
public class Model
{
public string pageName { get; set; }
public List<Ads> ads { get; set; }
public Model clone()
{
return (Model)this.MemberwiseClone();
}
}
//after clone any changes to object2 will not reflect to object1
Model object2 = object1.clone();
我在我的 mvc 5 网站中使用缓存,
我在缓存中有一个对象可用。
当我得到这个对象时,让我们从缓存中调用它 object1,并将它复制到另一个对象,让我们调用它 object2。
我在 object2 上执行的每个更改都会自动反映到 object1 和缓存的对象
现在,当我再次从缓存中获取对象时,由于更改跟踪,它会根据我对 objec2 所做的更改进行修改,而我不需要它
我怎样才能避免反映对缓存的更改
这是我的代码
public class HomeController : Controller
{
public ActionResult Index()
{
//a model with 10 adds
Model model = new Model()
{
pageName = "test",
ads = new List<Ads>()
{
new Ads() {id = 1, image = "1" },
new Ads() {id = 2, image = "2" },
new Ads() {id = 3, image = "3" },
new Ads() {id = 4, image = "4" },
new Ads() {id = 5, image = "5" },
new Ads() {id = 6, image = "6" },
new Ads() {id = 7, image = "7" },
new Ads() {id = 8, image = "8" },
new Ads() {id = 9, image = "9" },
new Ads() {id = 10, image = "10" },
},
};
//cache it
HttpContext.Cache.Insert("demo", model, null, DateTime.Now.AddMinutes(1), Cache.NoSlidingExpiration);
//get cached object
Model object1 = HttpContext.Cache.Get("demo") as Model;
// => 10 items
Console.WriteLine(model.ads.Count());
//just get 3 items of that list
Model object2 = object1; // disable changes tracking here
object2.ads = object2.ads.Take(3).ToList();
//this changes will be reflected to cached object, i need to disable this
//get cached object (from cache) again
Model newCachedModel = HttpContext.Cache.Get("demo") as Model;
Console.WriteLine(newCachedModel.ads.Count());//3 items only
//note i have never change the cached object, the changes reflected from modelToReturn (using changes tracking feature in c#)
return View(object2);
}
}
public class Model
{
public string pageName { get; set; }
public List<Ads> ads { get; set; }
}
public class Ads
{
public int id { get; set; }
public string image { get; set; }
}
我找到了解决办法
在对其进行任何更改之前,只需克隆该对象
public class Model
{
public string pageName { get; set; }
public List<Ads> ads { get; set; }
public Model clone()
{
return (Model)this.MemberwiseClone();
}
}
//after clone any changes to object2 will not reflect to object1
Model object2 = object1.clone();