Using Session to store Datatable

后端 未结 5 860
时光取名叫无心
时光取名叫无心 2021-01-13 19:04

currently we are using Sessions to store datatables in our pages so that we don\'t have to make Database hits to get the same datatable again and again. But my worry is that

5条回答
  •  感情败类
    2021-01-13 19:23

    It is preferable to store "commonly used" data in Memory; that's good logic. However "Session" means that it exists for the life of that Session, and hence that user. Secondly, pending the user "Session" life, as you already said, this could be consume valuable resources on the Server Side.

    What you may want to consider using is the "Cache" object, as it serves the same purpose with "Expiration".

    DataTable users = new DataTable();
    
    if (Cache["users"] == null)
    {
        // users = getUsers(customer);
       Cache.Add(“users”, users, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 60, 0), System.Web.Caching.CacheItemPriority.Default, null);
    }
     else
    {
        sers = (DataTable)Cache["users"];
    }
    

    There are many ways to re-use memory in .NET (1) ViewState (2) Cache (3) Session (4) Cookies

    But I would go for the "Cache" object.

提交回复
热议问题