using System;
using System.Web;
using System.Web.Caching;
public static class ReportManager
{
private static bool _reportRemovedFromCache = false;
static ReportManager()
{ }
public static String GetReport()
{
lock (typeof(ReportManager))
{
if (HttpContext.Current.Cache["MyReport"] != null)
return (string)HttpRuntime.Cache["MyReport"];
else
{
CacheReport();
return (string)HttpRuntime.Cache["MyReport"];
}
}
}
public static void CacheReport()
{
lock (typeof(ReportManager))
{
HttpRuntime.Cache.Add("MyReport",
CreateReport(), null, Cache.NoAbsoluteExpiration,
new TimeSpan(0, 1, 0),
System.Web.Caching.CacheItemPriority.Default,
new CacheItemRemovedCallback(ReportRemovedCallback));
}
}
private static string CreateReport()
{
System.Text.StringBuilder myReport =
new System.Text.StringBuilder();
myReport.Append("Sales Report<br />");
myReport.Append("2005 Q2 Figures<br />");
myReport.Append("Sales NE Region - $2 million<br />");
myReport.Append("Sales NW Region - $4.5 million<br />");
myReport.Append("Report Generated: " + DateTime.Now.ToString()
+ "<br />");
myReport.Append("Report Removed From Cache: " +
_reportRemovedFromCache.ToString());
return myReport.ToString();
}
public static void ReportRemovedCallback(String key, object value,
CacheItemRemovedReason removedReason)
{
_reportRemovedFromCache = true;
CacheReport();
}
}
Imports System
Imports System.Web
Imports System.Web.Caching
Public Class ReportManager
Private Shared _reportRemovedFromCache As Boolean = False
Shared Sub New()
End Sub
Private Sub New()
End Sub
Public Shared Function GetReport() As String
SyncLock (GetType(ReportManager))
If HttpContext.Current.Cache("MyReport") IsNot Nothing Then
Return CStr(HttpRuntime.Cache("MyReport"))
Else
CacheReport()
Return CStr(HttpRuntime.Cache("MyReport"))
End If
End SyncLock
End Function
Public Shared Sub CacheReport()
SyncLock (GetType(ReportManager))
HttpRuntime.Cache.Add("MyReport", CreateReport(), _
Nothing, Cache.NoAbsoluteExpiration, New TimeSpan(0, 1, 0), _
System.Web.Caching.CacheItemPriority.Default, _
New CacheItemRemovedCallback(AddressOf ReportRemovedCallback))
End SyncLock
End Sub
Private Shared Function CreateReport() As String
Dim myReport As New System.Text.StringBuilder()
myReport.Append("Sales Report<br />")
myReport.Append("2005 Q2 Figures<br />")
myReport.Append("Sales NE Region - $2 million<br />")
myReport.Append("Sales NW Region - $4.5 million<br />")
myReport.Append("Report Generated: " & _
DateTime.Now.ToString() & "<br />")
myReport.Append("Report Removed From Cache: " _
& _reportRemovedFromCache.ToString())
Return myReport.ToString()
End Function
Public Shared Sub ReportRemovedCallback(ByVal key As String, _
ByVal value As Object, ByVal removedReason _
As CacheItemRemovedReason)
_reportRemovedFromCache = True
CacheReport()
End Sub
End Class