c# - Why isn't the Cache invalidated after table update using the SqlCacheDependency? -
i have been trying sqlcachedependency working. think have set correctly, when update table, item in cache isn't invalidated.
can @ code , see if missing anything?
i enabled service broker sandbox database. have placed following code in global.asax file. restart iis make sure called.
void application_start(object sender, eventargs e) { sqldependency.start(configurationmanager.connectionstrings["sandboxconnectionstring"].connectionstring); }
i have placed entry in web.config file:
<system.web> <caching> <sqlcachedependency enabled="true" polltime="10000"> <databases> <add name="sandbox" connectionstringname="sandboxconnectionstring"/> </databases> </sqlcachedependency> </caching> </system.web>
i call code put item cache:
protected void cachedatasetbutton_click(object sender, eventargs e) { using (sqlconnection sqlconnection = new sqlconnection(configurationmanager.connectionstrings["sandboxconnectionstring"].connectionstring)) { using (sqlcommand sqlcommand = new sqlcommand("select petid, name, breed, age, sex, fixed, microchipped dbo.pets", sqlconnection)) { using (sqldataadapter sqldataadapter = new sqldataadapter(sqlcommand)) { dataset petsdataset = new dataset(); sqldataadapter.fill(petsdataset, "pets"); sqlcachedependency petssqlcachedependency = new sqlcachedependency(sqlcommand); cache.insert("pets", petsdataset, petssqlcachedependency, datetime.now.addseconds(10), cache.noslidingexpiration); } } } }
then bind gridview code:
protected void bindgridviewbutton_click(object sender, eventargs e) { if (cache["pets"] != null) { gridview1.datasource = cache["pets"] dataset; gridview1.databind(); } }
between attempts databind gridview, change table's values expecting invalidate cache["pets"] item, seems stay in cache indefinitely.
you must attach sqlcachedependency sqlcommand before execute command:
using (sqldataadapter sqldataadapter = new sqldataadapter(sqlcommand)) { dataset petsdataset = new dataset(); sqlcachedependency petssqlcachedependency = new sqlcachedependency(sqlcommand); sqldataadapter.fill(petsdataset, "pets"); cache.insert("pets", petsdataset, petssqlcachedependency, datetime.now.addseconds(10), cache.noslidingexpiration); }
Comments
Post a Comment