- mycache.zip
- MyCache
- Data
- Lib
- Microsoft.Practices.EnterpriseLibrary.Caching.Cryptography.dll
- Microsoft.Practices.EnterpriseLibrary.Caching.Database.dll
- Microsoft.Practices.EnterpriseLibrary.Caching.dll
- Microsoft.Practices.EnterpriseLibrary.Common.dll
- Microsoft.Practices.EnterpriseLibrary.Data.dll
- Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.dll
- Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.xml
- Microsoft.Practices.ObjectBuilder2.dll
- MyCache.sln
- MyCacheAPI
- MyCacheService
- App_Data
- aspnet_client
- system_web
- 4_0_30319
- bin
- Microsoft.Practices.EnterpriseLibrary.Caching.dll
- Microsoft.Practices.EnterpriseLibrary.Common.dll
- Microsoft.Practices.ObjectBuilder2.dll
- MyCacheService.dll
- MyCacheServiceLib.dll
- CacheService.svc
- MyCacheService.csproj
- MyCacheService.csproj.user
- obj
- Debug
- TempPE
- Release
- TempPE
- Properties
- web.config
- Web.Debug.config
- Web.Release.config
- MyCacheServiceLib
- bin
- Debug
- Microsoft.Practices.EnterpriseLibrary.Caching.dll
- Release
- Microsoft.Practices.EnterpriseLibrary.Caching.dll
- Microsoft.Practices.EnterpriseLibrary.Common.dll
- Microsoft.Practices.ObjectBuilder2.dll
- SimpleCacheLib.dll
- CacheEntry.cs
- CacheService.cs
- ICacheService.cs
- LockManager.cs
- MyCacheServiceLib.csproj
- MyCacheServiceLib.csproj.user
- obj
- Debug
- TempPE
- Release
- TempPE
- Properties
- SimpleCacheLib.csproj.user
- Site1
- Site2
- Site3
- Site4
|
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using MyCacheAPI;
using System.Web.Caching;
using System.IO;
using System.Text;
using System.Data.SqlClient;
namespace DistributedCache
{
public partial class _Default : System.Web.UI.Page
{
MyCache cache = new MyCache();
protected void Page_Load(object sender, EventArgs e)
{
LoadData();
}
/// <summary>
/// Load data from Cache, or read data and store in cache
/// </summary>
protected void LoadData()
{
lblWebFarm.Text = ConfigurationManager.AppSettings["WebFarmId"];
string strData = cache.Get("Key") as string;
if (strData == null)
{
divCacheMiss.Visible = true;
string dependencyFilePath = GetDependencyFilePath();
DateTime startTimeForCache = DateTime.Now;
object Value = GetObjectFromFile(dependencyFilePath);
DateTime endTimeForCache = DateTime.Now;
cache.Add("Key", Value, dependencyFilePath, Cache.NoAbsoluteExpiration, new TimeSpan(0, 5, 10), CacheItemPriority.Normal, new CacheItemRemovedCallback(onRemoveCallback));
lblData2.Text = Value.ToString();
}
else
{
divCacheHit.Visible = true;
DateTime startTimeForCache = DateTime.Now;
StringBuilder sbOutput = new StringBuilder();
string data = cache.Get("Key").ToString();
DateTime endTimeForCache = DateTime.Now;
lblCacheRetrievalTime.Text = string.Format("Retrieved following data in <b>{0}</b> milliseconds", (endTimeForCache - startTimeForCache).TotalMilliseconds);
lblData.Text = data;
}
}
/// <summary>
/// Get dependency file location
/// </summary>
/// <returns></returns>
private string GetDependencyFilePath()
{
string dependencyFilePath = Path.Combine(Path.Combine(Path.GetDirectoryName(Server.MapPath("")), "Data"), "Test.txt");
return dependencyFilePath;
}
/// <summary>
/// Callback function to re-insert the item in cache if cache dependency changed
/// </summary>
/// <param name="Key"></param>
/// <param name="Value"></param>
/// <param name="reason"></param>
protected void onRemoveCallback(string Key, object Value, CacheItemRemovedReason reason)
{
if (cache == null)
{
cache = new MyCache();
}
if (reason == CacheItemRemovedReason.DependencyChanged)
{
//Aquire lock on MyCache service for the Key and proceed only if
//no lock is currently set for this Key. This has been done to prevent
//multiple load-balanced web application update the same data on MyCache service
//sumultaneously when the underlying file content is modified
if (cache.SetLock(Key))
{
string dependencyFilePath = GetDependencyFilePath();
object modifiedValue = GetObjectFromFile(dependencyFilePath);
cache.Add(Key, modifiedValue, dependencyFilePath, Cache.NoAbsoluteExpiration, new TimeSpan(0, 5, 60), CacheItemPriority.Normal, new CacheItemRemovedCallback(onRemoveCallback));
//Release lock when done
cache.ReleaseLock(Key);
}
}
if (reason == CacheItemRemovedReason.Expired || reason == CacheItemRemovedReason.Removed)
{
if (cache.Exists(Key))
{
cache.Remove(Key);
}
}
}
/// <summary>
/// Read data from File
/// </summary>
/// <param name="dependencyFilePath"></param>
/// <returns></returns>
private object GetObjectFromFile(string dependencyFilePath)
{
object updatedValue = File.ReadAllText(dependencyFilePath);
return updatedValue;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.