Using Cache In Win Form Applications
Caching the objects is possible directly in the ASP.Net Applications as the cache component is a part of the .Net Framework Specifically used for caching the web pages and other objects on the net.
This can also be used in the Normal WinForm applications so that u can reduce the over heads of creating the objects again and again from the Scratch.
For this u need to give the reference to System.Web.Caching assembly.
Code Sample:
using System.Web;
using System.Web.Caching;
//in the class defination.
private static HttpRuntime _httpRuntime;
private const string CACHE_KEY = "CACHEKEY";
public static Cache Cache
{
get
{
EnsureHttpRuntime();
return HttpRuntime.Cache;
}
}
private static void EnsureHttpRuntime()
{
if( null == _httpRuntime )
{
try
{
Monitor.Enter( typeof( clsConn ) );
if( null == _httpRuntime )
{
// Create an Http Content to give us access to the cache.
_httpRuntime = new HttpRuntime();
}
}
finally
{
Monitor.Exit( typeof( clsConn ) );
}
}
}
//then to access the cached object.
public OracleConnection OrclConnection()
{
OracleConnection pConn;
pConn = clsConn.Cache[CACHE_KEY] as OracleConnection;
if( null == pConn )
{
pConn=new OracleConnection(pConnectString);
clsConn.Cache.Insert(
CACHE_KEY,
pConn,
null,
Cache.NoAbsoluteExpiration,
TimeSpan.FromSeconds(3600));
}
try
{
if(pConn.State==0){pConn.Open();}
return pConn;
}
catch(Exception e)
{
return pConn;
}
}
This can also be used in the Normal WinForm applications so that u can reduce the over heads of creating the objects again and again from the Scratch.
For this u need to give the reference to System.Web.Caching assembly.
Code Sample:
using System.Web;
using System.Web.Caching;
//in the class defination.
private static HttpRuntime _httpRuntime;
private const string CACHE_KEY = "CACHEKEY";
public static Cache Cache
{
get
{
EnsureHttpRuntime();
return HttpRuntime.Cache;
}
}
private static void EnsureHttpRuntime()
{
if( null == _httpRuntime )
{
try
{
Monitor.Enter( typeof( clsConn ) );
if( null == _httpRuntime )
{
// Create an Http Content to give us access to the cache.
_httpRuntime = new HttpRuntime();
}
}
finally
{
Monitor.Exit( typeof( clsConn ) );
}
}
}
//then to access the cached object.
public OracleConnection OrclConnection()
{
OracleConnection pConn;
pConn = clsConn.Cache[CACHE_KEY] as OracleConnection;
if( null == pConn )
{
pConn=new OracleConnection(pConnectString);
clsConn.Cache.Insert(
CACHE_KEY,
pConn,
null,
Cache.NoAbsoluteExpiration,
TimeSpan.FromSeconds(3600));
}
try
{
if(pConn.State==0){pConn.Open();}
return pConn;
}
catch(Exception e)
{
return pConn;
}
}
0 Comments:
Post a Comment
<< Home