Reputation: 34109
My application has kendo dropdown list to show states. The state drop downlist has been used on multiple views.
@(Html.Kendo().DropDownListFor(m => m.BlankReturn.StateProvinceCode)
.DataTextField("StateName")
.DataValueField("StateCode")
.DataSource(source =>
{
source.Read(read =>
{
read.Action("GetStateProvinces", "Lookup");
});
})
)
Right now every instance of this dropdownlist makes a call to server to get states. I want kendo to load states from the server only on the first call but any subsequent call needs to get it from client cache. How do i configure this?
Update1
Few answers below suggest to use server side caching. I was looking for client side caching. For example jQuery's ajax method will cache GET
method result (unless you explicitly disabled caching). I am assuming Kendo is using jQuery to make server call internally. However i guess kendo is disabling ajax caching. So kendo makes the server call each time to get result.
My question is how do i enable client side caching so there wont be any server call after first call.
Upvotes: 0
Views: 1636
Reputation: 15175
The answer is not Kendo related. If you are using MVC then you can either load the state collection from System.Web.Cache
or use the .net MVC caching method attributes.
Using Global Cache - The controller will always be called, however, if you cache the States on load then there will be a reduced number of database roundtrips.
using System.Web;
public ActionResult GetStates()
{
List<State> states=(List<State>)System.Web.HttpContext.Current.Cache["MY_STATES"];
if(states==null)
{
states=myDataLayer.LoadStates();
System.Web.HttpContext.Current.Cache.Insert(
"MY_STATES",
states,
null,
System.Web.Caching.Cache.NoAbsoluteExpiration,
new TimeSpan(0,1440,0),
System.Web.Caching.CacheItemPriority.Normal,
null);
}
return states;
}
MVC method Attributes - Set the browser cache attributes and instruct the browser via headers to cache for a specific time. There will be no controller calls while the associated browser cache has not yet expired.
[OutputCache(Duration = 86400)]
public ActionResult GetStates()
{
return myDataLayer.LoadStates();
}
MVC method Attributes - Single Config - Set the browser cache attributes
[OutputCache(CacheProfile = "TypeTableCacheProfile")]
public ActionResult GetStates()
{
return myDataLayer.LoadStates();
}
along with the config
<system.web>
<caching>
<outputCacheSettings>
<outputCacheProfiles>
<clear />
<!-- 24 hours-->
<add varyByParam="*" duration="86400" name="TypeTableCacheProfile" />
</outputCacheProfiles>
</outputCacheSettings>
</caching>
</system.web>
Upvotes: 0