Welcome to Idea R | Branding - Web Agency - Digital Strategies
Switch to the mobile layout

      Idea R - Do others know who you are? Design your corporate identity

Blog

Take a software geek from the 80s, add a new generation graphic designer and dilute with a longtime marketing strategist. Shake vigorously and you'll get the Idea R's blog.

Change language... italiano

Come cancellare la cache degli oggetti SqlDataSource

Published on 12/24/2011
Categories: Web & Apps

This article is available in English too.

La proprietà SqlDataSource.EnableCaching permette di salvare i risultati di una particolare interrogazione al database.
Una volta attivato il caching, se si reimposta EnableCaching a false, il risultato dell'interrogazione non viene più prelevato dalla cache ma viene rieseguito ogni volta.
La cosa che però può sorprendere è che se a questo punto si reimposta EnableCaching a true, vengono riprelevati i dati dalla vecchia cache.

// The Customers table contains PIPPO as the name of the customer 1
SqlDataSource dataSource = new SqlDataSource(myConnectionString, "SELECT Name FROM Customers WHERE CustomerID = 1");
dataSource.EnableCaching = true;
// The query returns PIPPO
 
...
 
// Update the database changing the name of the customer 1 to MINNY
 
...
 
dataSource.EnableCaching = false;
// This time the cache is disabled, so the query returns MINNY
 
...
 
dataSource.EnableCaching = true;
// WARNING! The query returns the old and expired value PIPPO again

Per cancellare la cache dunque non serve a niente impostare EnableCaching a false, ma bisogna procedere utilizzando la proprietà SqlDataSource.CacheKeyDependency.

// The Customers table contains PIPPO as the name of the customer 1
SqlDataSource dataSource = new SqlDataSource(myConnectionString, "SELECT Name FROM Customers WHERE CustomerID = 1");
dataSource.EnableCaching = true;
dataSource.CacheKeyDependency = "MyCacheKey";
if (Cache["MyCacheKey"] == null] Cache["MyCacheKey"] = DateTime.Now;
// The query returns PIPPO
 
...
 
// Update the database changing the name of the customer 1 to MINNY
 
...
 
Cache["MyCacheKey"] = DateTime.Now; // Clear the cache
// Now the query correctly returns MINNY

 

Google Reviews

Ti piace questo blog?

Per noi la tua opinione conta, perciò ci farebbe molto piacere se potessi scrivere una recensione su di noi o sul nostro blog.

You are the reader number 15,182.

Comments

Previous article

Previous article

Dropdown menus with CSS

Next article

Reflections on the water with Photoshop

Next article

Scroll to top