简体   繁体   English

如何使用Cache obect缓存数据asp.net mvc

[英]How to cache data asp.net mvc using Cache obect

I am using data caching in my asp.net application. 我在我的asp.net应用程序中使用数据缓存。 This is my interface for the ICacheProvider.cs 这是我的ICacheProvider.cs接口

public interface ICacheProvider
{
    object Get(string key);
    void Set(string key, object data, int cacheTime);
    bool IsSet(string key);
    void Invalidate(string key);
}

This the way I'm using caching in the services. 这就是我在服务中使用缓存的方式。

public List<EmployeeLookUp> GetEmployeeLookUp(RecordStatusEnum recordStatusEnum, int locatioId)
    {
        var status = (int)recordStatusEnum;

        var employeeData = Cache.Get("EmployeeLookUp") as IEnumerable;

        if (employeeData == null)
        {
            var result = MyDb.Employee.Where(w => w.Status == status && w.LocationId == locatioId).ToList().Select(s => new EmployeeLookUp()
            {
                EmployeeId = s.EmployeeId,
                DepartmentId = s.DepartmentId,
                EmployeeValue = string.Format("{0}{1}{2} {3} {4}", "[", s.CustomEmployeeId, "]", s.FirstName, s.LastName)
            }).ToList();

            if (result.Any())
                Cache.Set("EmployeeLookUp", result, 30);

            return result;
        }

        return (List<EmployeeLookUp>) employeeData;
    }

In the controller I'm using the returned of employees like this. 在控制器中,我正在使用像这样返回的员工。

 public ActionResult Index()
    {
        var employees = _employeeServices.GetEmployeeLookUp(RecordStatusEnum.Active, User.GetCemexUser().LocationId);
        employees.Insert(0, new EmployeeLookUp() { EmployeeId = -1, EmployeeValue = "All" });

        var complexRosterViewModel = new ComplexRosterViewModel
        {
            EmployeeList = new SelectList(employees, "EmployeeId", "EmployeeValue"),
            ListEmployeeGroups = new SelectList(_employeeGroupServices.GetEmployeeGroup(RecordStatusEnum.Active, User.GetCemexUser().LocationId), "EmployeeGroupId", "Value"),
            ListDepartments = new SelectList(_departmentService.GetDepartments(RecordStatusEnum.Active,User.GetCemexUser().LocationId),"DepartmentId","Value")
        };

        return View(complexRosterViewModel);
    }

Now my problem is, when i'm reloading the page several times, the additional "All" option that I added additionally to the employee select list, has been added to cached object("EmployeeLookUp") several times. 现在我的问题是,当我多次重新加载页面时,我添加到员工选择列表中的附加“全部”选项已多次添加到缓存对象(“EmployeeLookUp”)。 How this can be possible? 这怎么可能? I do not want the "All" option to be cached. 我不希望缓存“全部”选项。

It happens because you're using a reference to cached object. 这是因为您正在使用对缓存对象的引用。 If you change the object, it will reflect the changes in cached data. 如果更改对象,它将反映缓存数据中的更改。

Asp.Net Cache, modify an object from cache and it changes the cached value Asp.Net Cache,从缓存中修改对象,它会更改缓存的值

You must clone the object or create a new and copy the properties values (you can use AutoMapper to do it for you) 您必须克隆该对象或创建一个新对象并复制属性值(您可以使用AutoMapper为您执行此操作)

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM