How To Cache the objects

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Diagnostics;using System.Linq;using System.ServiceProcess;using System.Text;using System.Threading;using System.Timers;using System.Configuration;using System.IO;using System.Collections;using System.Data.SqlClient;using System.Windows.Forms;namespace
{
FireflyCacheCleanpublic partial class CacheCleanService : Form //ServiceBase{
System.Timers.
System.Timers.














{
InitializeComponent();


System.Diagnostics.

eventLog1 =
eventLog1.Source =
eventLog1.Log =
Timer cleanTimer = new System.Timers.Timer();Timer cacheTimer = new System.Timers.Timer();private static String ImgRootPath;private static String[] ImgSubDirs;private Int32 IDayFreq;private Int32 ITick;private string LastDate = "";private static Int32 Debug = 0;private static Int32 PreCacheRequired = 0;private Hashtable hash_Conns = new Hashtable();private string[] delimit = new string[1];private bool bCacheOtherFiles = false;private string[] CacheExtList;private Int64 ICleanTick;enum DEBUG_LEVEL : int { ERROR=0, HIGH=1, MEDIUM=2, LOW=3}public CacheCleanService()string CacheOtherFilesExt="";if (!System.Diagnostics.EventLog.SourceExists("FireflyClearCache"))EventLog.CreateEventSource("FireflyClearCache","Application");new EventLog();"FireflyClearCache";"Application";try{
ImgRootPath =

ImgSubDirs =
IDayFreq =
ITick =

{
ICleanTick =
}
ConfigurationSettings.AppSettings["ImgRoot"].ToString();if (ConfigurationSettings.AppSettings["ImgSubDir"] != null)ConfigurationSettings.AppSettings["ImgSubDir"].ToString().Split(new string[]{"^^"},StringSplitOptions.None);Convert.ToInt32(ConfigurationSettings.AppSettings["DayFreq"].ToString());Convert.ToInt32(ConfigurationSettings.AppSettings["TimerTick"].ToString());if (ConfigurationSettings.AppSettings["CleanTick"] != null)Convert.ToInt32(ConfigurationSettings.AppSettings["CleanTick"].ToString());else{

}
Debug =
PreCacheRequired =
throw new Exception("'CleanTick' Key Not Found");Convert.ToInt32(ConfigurationSettings.AppSettings["Debug"].ToString());Convert.ToInt32(ConfigurationSettings.AppSettings["PreCacheRequired"].ToString());//try//{// CacheOtherFilesExt = ConfigurationSettings.AppSettings["CacheOtherFiles"].ToString();// CacheExtList = CacheOtherFilesExt.Split(';');// bCacheOtherFiles = true;//}//catch//{// //if Key not specified in the Configuration file// bCacheOtherFiles = false;//}
cleanTimer.Interval = ICleanTick;
cleanTimer.Elapsed +=
new ElapsedEventHandler(OnCleanTimer);// delimit[0] = "^^";cacheTimer.Interval = ITick;
cacheTimer.Elapsed +=
delimit[0] =

{

!key.ToLower().Equals(
!key.ToLower().Equals(
!key.ToLower().Equals(
!key.ToLower().Equals(
!key.ToLower().Equals(
!key.ToLower().Equals(
{
hash_Conns[key] =

hash_Conns[key] = ((

hash_Conns[key] = ((

hash_Conns[key] = ((

{
WriteLog(

}
}
}

{
WriteLog(

}

OnCacheTimer(
new ElapsedEventHandler(OnCacheTimer);"^^";foreach (string key in ConfigurationSettings.AppSettings.Keys)if (!key.ToLower().Equals("timertick") &&"debug") &&"cleantick") &&"imgsubdir") &&"dayfreq") &&"precacherequired") &&"imgroot"))ConfigurationSettings.AppSettings[key];while (((string)hash_Conns[key]).IndexOf(Environment.NewLine) != -1)string)hash_Conns[key]).Replace(Environment.NewLine, "");while (((string)hash_Conns[key]).IndexOf(@" ") != -1)string)hash_Conns[key]).Replace(@" ", " ");while (((string)hash_Conns[key]).IndexOf(@" ^^") != -1)string)hash_Conns[key]).Replace(@" ^^", "^^");if (ConfigurationSettings.AppSettings[key].Split(delimit, StringSplitOptions.RemoveEmptyEntries).Count() < 3)"Error in Config:" + key + " [Connection string, Cache URL and Primary URL should be delimited with ^^]", (int)DEBUG_LEVEL.ERROR);return;if (hash_Conns.Keys.Count == 0)"Error in Config: No Servers configured.", (int)DEBUG_LEVEL.ERROR);return;null, null);//cleanTimer.Enabled = true;//while (true)//{// System.Threading.Thread.Sleep(1000);//}}

{
WriteLog(
}
}

{
catch (Exception ex)"Error in Config:" + ex.ToString() + ex.StackTrace, (int)DEBUG_LEVEL.ERROR);protected void OnCleanTimer(object source, ElapsedEventArgs e)try{
cleanTimer.Enabled =


{
WriteLog(
LastDate = Date;
}
false;string Date = System.DateTime.Now.ToShortDateString();if (!LastDate.Equals(Date))"Running on " + Date, (int)DEBUG_LEVEL.HIGH);//Enhanced to Introduce Customer and project level cleanup//Joel P//28 Mar 2011//StartCleanup(ImgRootPath, IDayFreq);// StartCleanup(ImgRootPath, ImgSubDirs, IDayFreq);
//End
{
WriteLog(

bg.DoWork += StartCleanup;
bg.RunWorkerAsync(Key +
}
}

{
WriteLog(
}
foreach (string Key in hash_Conns.Keys)"PreCaching startup - " + Key, (int)DEBUG_LEVEL.HIGH);BackgroundWorker bg = new BackgroundWorker();"^^" + ((string)hash_Conns[Key]));catch (Exception ex)"Clean Error :" + ex.ToString(), (int)DEBUG_LEVEL.ERROR);finally{
cleanTimer.Enabled =
}
}

{
true;protected void OnCacheTimer(object source, ElapsedEventArgs e)try{
cacheTimer.Enabled =

PreCacheFolders();
}

{
WriteLog(
}
false;if ( PreCacheRequired > 0)catch (Exception ex)"Cache Error :" + ex.ToString(), (int)DEBUG_LEVEL.ERROR);finally{
cacheTimer.Enabled =
}
}

{
true;protected void PreCacheFolders()try{

{
WriteLog(

bg.DoWork += PerformPreCaching;
bg.RunWorkerAsync(Key +
}
}

{
WriteLog(
}
}

{
foreach (string Key in hash_Conns.Keys)"PreCaching startup - " + Key, (int)DEBUG_LEVEL.HIGH);BackgroundWorker bg = new BackgroundWorker();"^^" + ((string)hash_Conns[Key]));catch (Exception ex)"Error :" + ex.ToString() + ex.StackTrace, (int)DEBUG_LEVEL.ERROR);static void WriteLog(string Message, int Level)try{

eventLog1.WriteEntry(Message);
}

{
WriteLog(
}
}

{
if (Level == 0 || Debug >= Level)catch (Exception ex)"Error :" + ex.ToString() + ex.StackTrace, (int)DEBUG_LEVEL.ERROR);static void PerformPreCaching(object sender, DoWorkEventArgs e)//string string ConStr, string CacheURL, string PrimaryURLtry{


delimit[0] =




WriteLog(
bool bCacheOtherFiles=false;string[] delimit = new string[1];"^^";string[] sArr = ((string)e.Argument).Split(delimit, StringSplitOptions.RemoveEmptyEntries);String worksetSQL = "SELECT WorksetName, Customer, Project, JulianDate from Inventory WITH(NOLOCK) where UPLOADSTATUS='INKEY' ORDER BY RECEIVEDON";string fileSQL = @"select DISTINCT P.FileName,D.DCN,P.Pageid,p.pagetype from Pages P WITH(NOLOCK) INNER JOIN Documents D WITH(NOLOCK) on P.DocumentId=D.DocumentId " +@"INNER JOIN Worksets W WITH(NOLOCK) on W.WorksetId=D.WorksetId and W.WorksetName='XXX' ORDER BY P.Pageid";"PerformingPreCaching - " + sArr[0], (int)DEBUG_LEVEL.MEDIUM);//Use the Primary web service to fetch the worksets in WIPPrimaryServiceReference.
snipService.Url = sArr[3];

{
bCacheOtherFiles =
}

PrimaryServiceReference.
sp.ParameterName =
sp.SqlDbType = PrimaryServiceReference.
sp.Value = worksetSQL;
PrimaryServiceReference.
spColl[0] = sp;
SnippetKey snipService = new FireflyCacheClean.PrimaryServiceReference.SnippetKey();if (sArr.Length > 4)true;SqlParameter sp = new PrimaryServiceReference.SqlParameter();"ExecSQL";SqlDbType.VarChar;SqlParameter[] spColl = new PrimaryServiceReference.SqlParameter[1];int iRetry = 0;//Try atleast 3 times before giving up.

{
DataSet ds = null;while ( true )try{
ds = snipService.GetDataset(

}

{



}
}
"usp_FetchData", spColl, "", sArr[1]);break;catch (Exception ex)Thread.Sleep(1000);if ( ++iRetry > 2 )throw ex;DataTable dt = ds.Tables[0];//Iterate through the workset list and cache the pages in it
{

WriteLog(

{
foreach (DataRow row in dt.Rows)string WorkSet = row["WorksetName"].ToString().Trim();"PerformingPreCaching Folder - " + sArr[0] + "-" + WorkSet, (int)DEBUG_LEVEL.MEDIUM);if (WorkSet.Length > 0)//Get the pages for the current worksetsp.Value = fileSQL.Replace(

iRetry = 0;
"XXX", WorkSet);DataSet fileDs = null;//Try at least thrice before giving up
{
while (true)try{
fileDs = snipService.GetDataset(

}

{



}
}

"usp_FetchData", spColl, "", sArr[1]);break;catch (Exception ex)Thread.Sleep(1000);if (++iRetry > 2)throw ex;DataTable fileDt = fileDs.Tables[0];//Go through the pages in the workset and cache them
{


{
WriteLog(
CacheImage(sArr[0],
row[
row[
row[
row[
fileRow[
sArr[2], sArr[3]);
}

{
CacheFile(sArr[0],
row[
row[
row[
row[
fileRow[
sArr[2], sArr[3],sArr[4]);
}
}
}
}
WriteLog(
}

{
WriteLog(
}
foreach (DataRow fileRow in fileDt.Rows)ReadConfig R = new ReadConfig();if (ReadConfig.PageFilters.ContainsKey(row["Project"].ToString().ToLower()) && !ReadConfig.PageFilters.ContainsValue(fileRow["pageType"].ToString().ToLower()))"Files - " + sArr[0] + "-" + fileRow["FileName"].ToString().Trim(), (int)DEBUG_LEVEL.LOW);"WorksetName"].ToString().Trim(),"Customer"].ToString().Trim(),"Project"].ToString().Trim(),"JulianDate"].ToString().Trim(),"FileName"].ToString().Trim(),if (bCacheOtherFiles == true)"WorksetName"].ToString().Trim(),"Customer"].ToString().Trim(),"Project"].ToString().Trim(),"JulianDate"].ToString().Trim(),"DCN"].ToString().Trim(),"Ending Precaching - " + sArr[0], (int)DEBUG_LEVEL.HIGH);catch (Exception ex)"Error in PerformPreCaching:" + ex.ToString() + ex.StackTrace, (int)DEBUG_LEVEL.ERROR);finally{

}
}

{
static void CacheFile(string setup, string workset, string customer, string project, string julianDate, string fileName, string cacheURL, string primaryURL,String Extensions)try{


{

fileName= fileName + sExts[iCnt];
string[] sExts = Extensions.Split(';');for (int iCnt=0; iCnt<sExts.Length ; iCnt++)if (sExts[iCnt] == "") continue;//Cache the pages if they are not cached already!//Temporarily ignoring DCN files which contain hypen in their name, DCN with hypen indicates a child dcn which got split and there will be no files with that name.
{
if (!File.Exists(ImgRootPath + "\\" + customer + "\\" + project + @"\" + workset + @"\" + (fileName)) && !(fileName.Contains('-')))//Call the caching web service to cache the pagesWriteLog(
CachingServiceReference.
cacheService.Url = cacheURL;
cacheService.GetFile(workset, customer, project, julianDate, fileName, primaryURL);
WriteLog(
}
"file caching..\r\n" + cacheURL + "\r\n" + workset + "\r\n" + project + "\r\n" + customer + "\r\n" + julianDate + "\r\n" + fileName + "\r\n" + primaryURL, (int)DEBUG_LEVEL.LOW);ImageCaching cacheService = new FireflyCacheClean.CachingServiceReference.ImageCaching();"File cached :" + fileName, (int)DEBUG_LEVEL.LOW);else{
WriteLog(
}
}
}

{
WriteLog(
}
}

{
"File already exists!:" + fileName, (int)DEBUG_LEVEL.LOW);catch (Exception ex)"Error in caching Image - " + setup + " - " + workset + " - " + fileName + ex.Message + "\r\n" + ex.StackTrace, (int)DEBUG_LEVEL.ERROR);static void CacheImage(string setup, string workset, string customer, string project, string julianDate, string fileName, string cacheURL, string primaryURL)try{//Cache the pages if they are not cached already!
{
if (!File.Exists(ImgRootPath + "\\" + customer + "\\" + project + @"\" + workset + @"\" + ChangeFileName(fileName)))//Call the caching web service to cache the pagesWriteLog(
CachingServiceReference.
cacheService.Url = cacheURL;
cacheService.GetFullPageImage(workset, project, customer, julianDate, fileName, primaryURL);
WriteLog(
}
"file caching..\r\n" + cacheURL + "\r\n" + workset + "\r\n" + project + "\r\n" + customer + "\r\n" + julianDate + "\r\n" + fileName + "\r\n" + primaryURL, (int)DEBUG_LEVEL.LOW);ImageCaching cacheService = new FireflyCacheClean.CachingServiceReference.ImageCaching();"File cached :" + fileName, (int)DEBUG_LEVEL.LOW);else{
WriteLog(
}
}

{
WriteLog(
}
}

{
"File already exists!:" + fileName, (int)DEBUG_LEVEL.LOW);catch (Exception ex)"Error in caching Image - " + setup + " - " + workset + " - " + fileName + ex.Message + "\r\n" + ex.StackTrace, (int)DEBUG_LEVEL.ERROR);static string ChangeFileName(string fName)try{//The files will be cached in a different name in the caching server//22810180_021_0_0_0_0.bin
}

{
WriteLog(
}


}
return fName.Replace('.', '_') + "_0_0_0_0.bin";catch (Exception ex)"Error :" + ex.ToString() + ex.StackTrace, (int)DEBUG_LEVEL.ERROR);return null;//static void CacheWorkset(string
{
static SqlConnection getConnection(string sConStr)try{

oCon.ConnectionString = sConStr;
oCon.Open();

}

{
WriteLog(
}

}
SqlConnection oCon = new SqlConnection();return oCon;catch (Exception ex)"Error :" + ex.ToString() + ex.StackTrace, (int)DEBUG_LEVEL.ERROR);return null;//protected override void OnStart(string[] args)//{// if (ImgRootPath.ToLower().IndexOf(@"\ffcache") == -1)// {// WriteLog("ERROR:Cached folder should be named FFCache :" + ImgRootPath, (int)DEBUG_LEVEL.ERROR);// Stop();// }// else// {// cleanTimer.Enabled = true;// cacheTimer.Enabled = true;// }//}//protected override void OnStop()//{// cleanTimer.Enabled = false;// cacheTimer.Enabled = false;// WriteLog("Stopped on " + DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString(), (int)DEBUG_LEVEL.ERROR);//}//private void StartCleanup(String ImgRoot, Int32 Freq)//{// try// {// bool FolderDeleted = true;// while (FolderDeleted)// {// FolderDeleted = false;// WriteLog("Retrieving Directries...." + DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString(), (int)DEBUG_LEVEL.HIGH);
// String[] Dirs = Directory.GetDirectories(ImgRoot);// if (Dirs != null)// {// for (Int32 ilp = 0; ilp < Dirs.Length; ilp++)// {// TimeSpan ts = DateTime.Now.Subtract(Directory.GetLastWriteTime(Dirs[ilp]));// if (ts.Days >= Freq)// {// WriteLog("Deleting Data for batch " + Path.GetFileName(Dirs[ilp]) + " - " + ts.Days.ToString() + " day(s) old", (int)DEBUG_LEVEL.LOW);// try// {// Directory.Delete(Dirs[ilp], true);// FolderDeleted = true;// }// catch (Exception ex)// {// WriteLog("Error deleting batch " + Path.GetFileName(Dirs[ilp]) + " - " + ex.Message, (int)DEBUG_LEVEL.ERROR);// }// }// else// {// WriteLog("Ignoring batch " + Path.GetFileName(Dirs[ilp]) + " - Only " + ts.Days.ToString() + " day(s) old", (int)DEBUG_LEVEL.LOW);// }// }// }// }// }// catch (Exception)// {// throw;// }//}
{
private void StartCleanup(object sender, DoWorkEventArgs e)//if (CleanType.ToString() == "Shipped")//{try{// bool bCacheOtherFiles = false;
delimit[0] =


PrimaryServiceReference.
sp.ParameterName =
sp.SqlDbType = PrimaryServiceReference.
sp.Value = ShippedSQL;
PrimaryServiceReference.
spColl[0] = sp;
string[] delimit = new string[1];"^^";string[] sArr = ((string)e.Argument).Split(delimit, StringSplitOptions.RemoveEmptyEntries);String ShippedSQL = "SELECT I.WorksetName,I.Customer,I.Project,I.JulianDate FROM Inventory I WITH(NOLOCK) WHERE UploadStatus='SHIPPED'";SqlParameter sp = new PrimaryServiceReference.SqlParameter();"ExecSQL";SqlDbType.VarChar;SqlParameter[] spColl = new PrimaryServiceReference.SqlParameter[1];//Use the Primary web service to fetch the worksets in WIPPrimaryServiceReference.
snipService.Url = sArr[3];


{
SnippetKey snipService = new FireflyCacheClean.PrimaryServiceReference.SnippetKey();DataSet ds = null;while (true)try{
ds = snipService.GetDataset(

}

{
WriteLog(
}
}

{

{


Dirs =

{

{


{
WriteLog(
"usp_FetchData", spColl, "", sArr[1]);break;catch (Exception ex)"Error :" + ex.ToString() + ex.StackTrace, (int)DEBUG_LEVEL.ERROR);if (ds.Tables[0].Rows.Count > 0)for (Int32 Indx = 0; Indx < ds.Tables[0].Rows.Count; Indx++)String[] Dirs = Directory.GetDirectories(ImgRootPath + "\\" + ds.Tables[0].Rows[Indx]["customer"].ToString() + "\\" + ds.Tables[0].Rows[Indx]["Project"].ToString() + "\\", ds.Tables[0].Rows[Indx]["WorksetName"].ToString(), SearchOption.AllDirectories);if (Dirs.Length == 0)Directory.GetDirectories(ImgRootPath + "\\", ds.Tables[0].Rows[Indx]["WorksetName"].ToString(), SearchOption.AllDirectories);if (Dirs != null)for (Int32 ilp = 0; ilp < Dirs.Length; ilp++)TimeSpan ts = DateTime.Now.Subtract(Directory.GetLastWriteTime(Dirs[ilp]));if (ts.Days >= Indx)"Deleting Data for batch " + Path.GetFileName(Dirs[ilp]) + " - " + ts.Days.ToString() + " day(s) old", (int)DEBUG_LEVEL.LOW);try{

}

{
WriteLog(
}
}
Directory.Delete(Dirs[ilp], true);catch (Exception ex)"Error deleting batch " + Path.GetFileName(Dirs[ilp]) + " - " + ex.Message, (int)DEBUG_LEVEL.ERROR);else{
WriteLog(
}
}
}
Dirs =
}
}
}

{
WriteLog(
}
"Ignoring batch " + Path.GetFileName(Dirs[ilp]) + " - Only " + ts.Days.ToString() + " day(s) old", (int)DEBUG_LEVEL.LOW);null;catch (Exception ex)"Error in Clean the Image - " + ex.Message + "\r\n" + ex.StackTrace, (int)DEBUG_LEVEL.ERROR);//}//else//{// try// {// bool FolderDeleted = true;// while (FolderDeleted)// {// FolderDeleted = false;// WriteLog("Retrieving Directries...." + DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString(), (int)DEBUG_LEVEL.HIGH);// for (Int32 Indx = 0; Indx < SubDirs.Length; Indx++)// {// String[] Dirs = Directory.GetDirectories(ImgRoot + "\\" + SubDirs[Indx] + "\\");// if (Dirs != null)// {// for (Int32 ilp = 0; ilp < Dirs.Length; ilp++)// {// TimeSpan ts = DateTime.Now.Subtract(Directory.GetLastWriteTime(Dirs[ilp]));// if (ts.Days >= Freq)// {// WriteLog("Deleting Data for batch " + Path.GetFileName(Dirs[ilp]) + " - " + ts.Days.ToString() + " day(s) old", (int)DEBUG_LEVEL.LOW);// try// {// Directory.Delete(Dirs[ilp], true);// FolderDeleted = true;// }// catch (Exception ex)// {// WriteLog("Error deleting batch " + Path.GetFileName(Dirs[ilp]) + " - " + ex.Message, (int)DEBUG_LEVEL.ERROR);// }// }// else// {// WriteLog("Ignoring batch " + Path.GetFileName(Dirs[ilp]) + " - Only " + ts.Days.ToString() + " day(s) old", (int)DEBUG_LEVEL.LOW);// }// }// }// Dirs = null;// }// }// }// catch (Exception)// {// throw;// }//}}

{
}
private void CacheCleanService_Load(object sender, EventArgs e)//private void StartCleanup(String ImgRoot,String[] SubDirs, Int32 Freq)//{// try// {// bool FolderDeleted = true;// while (FolderDeleted)// {// FolderDeleted = false;// WriteLog("Retrieving Directries...." + DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString(), (int)DEBUG_LEVEL.HIGH);// for (Int32 Indx = 0; Indx < SubDirs.Length; Indx++)// {// String[] Dirs = Directory.GetDirectories(ImgRoot + "\\" + SubDirs[Indx] + "\\");// if (Dirs != null)// {// for (Int32 ilp = 0; ilp < Dirs.Length; ilp++)// {// TimeSpan ts = DateTime.Now.Subtract(Directory.GetLastWriteTime(Dirs[ilp]));// if (ts.Days >= Freq)// {// WriteLog("Deleting Data for batch " + Path.GetFileName(Dirs[ilp]) + " - " + ts.Days.ToString() + " day(s) old", (int)DEBUG_LEVEL.LOW);// try// {// Directory.Delete(Dirs[ilp], true);// FolderDeleted = true;// }// catch (Exception ex)// {// WriteLog("Error deleting batch " + Path.GetFileName(Dirs[ilp]) + " - " + ex.Message, (int)DEBUG_LEVEL.ERROR);// }// }// else// {// WriteLog("Ignoring batch " + Path.GetFileName(Dirs[ilp]) + " - Only " + ts.Days.ToString() + " day(s) old", (int)DEBUG_LEVEL.LOW);// }// }// }// Dirs = null;// }// }// }// catch (Exception ex)// {// throw ex;// }//}}
}

Comments