Page 281 - DCAP312_WEB_TECHNOLOGIES_II
P. 281
Unit 14: Debugging and Optimization
is traditional application data caching, which we can use to programmatically store arbitrary Notes
objects, such as data sets, to server memory so that our application can save the time and
resources it takes to recreate them.
Microsoft C 1.0, based on Lattice C, was Microsoft's first C product in 1983.
14.2.2 The Upside of Using the Cache
ASP.NET’s cache can be used in a variety of ways-too many ways to cover in which can be
problematic in some cases due to how difficult this makes it to unit test repositories or factories.
Another caveat is that it is possible to shoot our self in the foot by allowing cached items to
trump actual, tangible, data as it should exist within our application.
For the sake of simplicity, suppose that tracks products that can be sold online. Furthermore,
assume that while the underlying data storage mechanism for all of these products is a SQL
Server database, the only way that products can be updated or added to the system is through
my application. But let us also assume that this little application gets tons of traffic, and that
products are fairly infrequently added or updated. And, when they are update, it is usually just
to reflect something critical, such as a change in inventory status.
Assume LINQ to SQL to pull these products in and out of the database and into my application.
With such a scenario we could easily create a repository that would put all of my products into
memory on the web server when requested like so:
Public List<Product> GetAllProducts ()
{
List<Product> output =
HttpContext.Current.Cache [“AllProducts”] as List<Product>
if (output == null)
{
output = db.Products
.Select()
.ToList();
HttpContext.Current.Cache.Add(
“AllProducts”, output,null,Cache.NoAbsoluteExpiration,
Cache.NoSlidingExpiration, CacheItemPriority.AboveNormal, null);
}
return output;
}
With this approach, we are then free to slice and dice my products for consumption by various
other methods within my repository, such as the ability to get all products from a certain
product family:
public List<Products> GetProductsByProdLine(string prodLine)
{
return this.GetAllProducts()
.Where(prod => prod.ProductLine == prodLine).ToList();
}
And, as we can see, we are able to do that all from within memory using LINQ to Objects.
LOVELY PROFESSIONAL UNIVERSITY 275