|
|||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||
|
Announcements
Services
Chapters
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
Introduction
Think about [currency exchange rate] in an online market. This data must be in DB and developers will use that in calculation for currency conversion.
Given that the rate changes periodically once a day, at 6 AM sharp. Apparently the data will never change during 24 hours.
Then we don't have to access DB every time we need that data until 6 AM next day if we already have that.
As above diagram, substituting web server's local XML data for the real DB data will reduce DB access and eventually will make DB happier.
Sample
1. Open and execute 'SetUp_JustExecute.sql' file first. That will create a database, an account and a table. Using the code
1. Import WebPageXmlCaching namespace by adding WebPageXmlCaching.cs to your .NET project. You will use only WebPageXmlCachingAPI class and Periodicality class in the namespace.
A. Notice "CarSpec" and Request.QueryString["carno"] ; the XML cache file will be created like "[the page name]/CarSpec_[carno].xml". If you don't want different cache version along with "carno", just set that "" i.e. 'WebPageXmlCachingAPI.GetCacheInXml(Request, "CarSpec", "", ref strXMLCarSpec);'
<%
string strXMLCarSpec = null;
WebPageXmlCachingAPI.GetCacheInXml(Request, "CarSpec", Request.QueryString["carno"], ref strXMLCarSpec);
if (strXMLCarSpec != null ) Response.Write(strXMLCarSpec + " [Data from the cached XML, not from DB]");
else
{
WebPageXmlCachingAPI.StartRecordingResponse(Response);
%>
B. Notice Periodicality(...); You should specify when the cache will be updated.
<%
string strHTMLCache = WebPageXmlCachingAPI.StopRecordingResponse(Response);
// at PM 8:00 every monday. There are more methods in Periodicality class to tweak the cycle.
Periodicality prd = new Periodicality(DayOfWeek.Monday, TimeSpan.FromHours(20));
// Save in a xml file
WebPageXmlCachingAPI.SetCacheInXml(Request, "CarSpec", prd, Request.QueryString["carno"], strHTMLCache);
}
%>
Note two pairs of methods : Get/Set and Start/Stop. You might be aware of how to use them
WebPageXmlCachingAPI.GetCacheInXml(Request, "CarSpec", "", ref strXMLCarSpec);
WebPageXmlCachingAPI.StartRecordingResponse(Response);
WebPageXmlCachingAPI.StopRecordingResponse(Response);
WebPageXmlCachingAPI.SetCacheInXml(Request, "CarSpec", prd, "", strHTMLCache);
4. Compile and execute. At the very first execution, the car data will come from DB. But from the next execution the data (HTML fragment) will come from local XML file. You will feel how fast the page open because it doesn't access DB. Make sure the cache file created. Until expiration time, web server will continue to substitute local XML cache for remote DB data.
Discussion
I guess this situation always happens in every online market : informations of each goods usually don't change for a while.
So the periodically updated data could be an easily recognizable target for caching to reduce DB overhead.
//After deploying a 'user control' on page, specify below
<%@ OutputCache Duration="3600" VaryByParam="carno" %>
//the cache will be expired after 60 minutes and
//the cache will create and manage different versions of cache based on URL parameter 'carno'
But suppose that I have 5000 goods to display. Unfortunately I don't totally trust the mechanism behind this user control caching in .Net though I guess it must be very efficient.
Besides, the 'duration' doesn't meet my needs. Provided that the data will be updated at 6 AM everyday. If the page has been called 5:59 AM, what will happen next? The expired, meaningless data will last up to 6:59 AM ( for 3600 seconds ). There is absolute-time based cache expiration but that expiration happens just once. Automatic repetition is needed in this case. So I invented my own caching 1) to verify the cache contents with my own eyes by opening the xml file directly 2) to update cache periodically and more sharply 3) to apply to other web server environment that is not .NET Though I've tried to simplify A and B blocks, the aboves were my best. I'm sorry that MACRO is restrictly used in C#. MACRO can generate long codes in a simple function. Maybe other languages can apply this caching technique more simply using MACRO. Further Study
1. Framework for the 'currency exchange rate' kind data caching History1. Created
|
||||||||||||||||||||||||||||||||||||||