Skip Navigation Links.

Northwind\Data\CacheManagerBase.cs

D:\NorthwindForWeb\Northwind\CS\DotNet\ADO.Net\Northwind\Data\CacheManagerBase.cs
/*
 * This file was generated by ProCG version 2.0
 *
 * File name:	Northwind\Data\CacheManagerBase.cs
 * Language:	C# - ADO.Net 
 * Database:	My Sql
 *
 * Copyright (c) 2002-2019 iGenXSoft.
 * For more information visit http://www.igenxsoft.com
 */

using System;
using System.Collections;
using System.Collections;
using Northwind.General;
using System.Threading.Tasks;

namespace Northwind.Data
{
	public enum ActionWhenDataChangedEnum
	{
		RemoveTableCachData = 1,
		ReloadTableCacheData = 2
	}

	public class CacheManagerBase
	{
		protected System.Collections.Hashtable m_ResultsTable;
		protected System.Collections.Hashtable m_UncachedObjects;
	
		protected bool m_bReadFromCache;
		protected bool m_bWriteToCache;
		protected bool m_bReadFromDirtyCache;

		private ActionWhenDataChangedEnum m_ActionWhenDataChanged = ActionWhenDataChangedEnum.RemoveTableCachData;

		public DateTime m_ResetDate = DateTime.Now;
		public static int MinElements = 500;
		public static int MaxElements = 1000;
		public static int TimeToClearCache = 0;
		public static bool ParamsLoaded = false;
		
		protected static System.Threading.ReaderWriterLock readerWriterLock = new System.Threading.ReaderWriterLock();
	
		public CacheManagerBase()
		{
			m_ResultsTable = new System.Collections.Hashtable();
			m_UncachedObjects = new System.Collections.Hashtable();
			if (!ParamsLoaded)
			{
				LoadParams();
				ParamsLoaded = true;
			}
		}

		protected virtual void LoadParams()
		{
			try
			{
				string shortCompanyName = NorthwindGeneral.GetShortCompanyName();
				string minCacheElementsString = NorthwindGeneral.GetStringFromRegistry(shortCompanyName, "Northwind", "MinCacheElements");
				MinElements = Int32.Parse(minCacheElementsString);
				string maxCacheElementsString = Northwind.General.NorthwindGeneral.GetStringFromRegistry(shortCompanyName, "Northwind", "MaxCacheElements");
				MaxElements = Int32.Parse(maxCacheElementsString);
				string timeToClearCacheString = Northwind.General.NorthwindGeneral.GetStringFromRegistry(shortCompanyName, "Northwind", "TimeToClearCache");
				TimeToClearCache = Int32.Parse(timeToClearCacheString);
			}
			catch(Exception ex) // symbols were not defined, set default values
			{
				MinElements = 50;
				MaxElements = 500;
				TimeToClearCache = 2;
			}
		}
		
		public ActionWhenDataChangedEnum ActionWhenDataChanged
		{
			get { return m_ActionWhenDataChanged; }
			set { m_ActionWhenDataChanged = value; }
		}
	
		public virtual ResultInfo GetResultInfo(string resultName) 
		{
			ResultInfo resultInfo = null;
	
			DateTime now = new DateTime();
			now = DateTime.Now;
			System.Threading.LockCookie lockCookie;
	
			readerWriterLock.AcquireReaderLock(System.Threading.Timeout.Infinite);
	
			try
			{
				if (TimeToClearCache > 0 && now.Date > this.m_ResetDate.Date)
				{
					if (now.TimeOfDay > new TimeSpan(TimeToClearCache, 0, 0))
					{
						lockCookie = readerWriterLock.UpgradeToWriterLock(System.Threading.Timeout.Infinite);
						try
						{
							m_ResultsTable.Clear();
							m_ResetDate = now;
						}
						finally
						{
							readerWriterLock.DowngradeFromWriterLock(ref lockCookie);
						}
	
					}
				}
	
				if (m_ResultsTable.Count >= MaxElements)
				{
					lockCookie = readerWriterLock.UpgradeToWriterLock(System.Threading.Timeout.Infinite);
	
					try
					{
						ArrayList resultsNameToRemove = new ArrayList();
	
						foreach (ResultInfo resultInfo1 in m_ResultsTable.Values)
						{
							if (resultInfo1.NumHits == 0)
								resultsNameToRemove.Add(resultInfo1.ResultName);
						}
						foreach (string resultName1 in resultsNameToRemove)
						{
							if (m_ResultsTable.ContainsKey(resultName1))
								m_ResultsTable.Remove(resultName1);
						}
	
						if (m_ResultsTable.Count >= MaxElements)
						{
							m_ResultsTable.Clear();
						}
					}
					finally
					{
						readerWriterLock.DowngradeFromWriterLock(ref lockCookie);
					}
				}
				resultInfo = (ResultInfo)m_ResultsTable[resultName];
			}
			finally
			{
				readerWriterLock.ReleaseReaderLock();
			}
			return (resultInfo);
		}
	
		public virtual void AddOrReplaceResultInfo(ResultInfo resultInfo) 
		{
			readerWriterLock.AcquireWriterLock(System.Threading.Timeout.Infinite);
			try
			{
				m_ResultsTable[resultInfo.ResultName] = resultInfo;
			}
			finally
			{
				readerWriterLock.ReleaseWriterLock();
			}
		}
	
		public virtual int NumCachedObjects
		{
			get
			{
				int numCachedObjects = 0;
				if (m_ResultsTable != null)
				{
					readerWriterLock.AcquireReaderLock(System.Threading.Timeout.Infinite);
					try
					{
						numCachedObjects = m_ResultsTable.Count;
					}
					finally
					{
						readerWriterLock.ReleaseReaderLock();
					}
				}
				return numCachedObjects;
			}
		}

		public virtual void RemoveTableResultsInfo(string tableName)
		{
			readerWriterLock.AcquireWriterLock(System.Threading.Timeout.Infinite);
			try
			{
				ResultInfo[] resultsInfo = new ResultInfo[m_ResultsTable.Values.Count];
				m_ResultsTable.Values.CopyTo(resultsInfo, 0);
				foreach (ResultInfo resultInfo in resultsInfo)
				{
					if (resultInfo.TableName == tableName)
					{
						m_ResultsTable.Remove(resultInfo.ResultName);
					}
				}
			}
			finally
			{
				readerWriterLock.ReleaseWriterLock();
			}
		}

		public virtual void ReloadTableResultsInfo(IDataManagerBase dataManager,  string tableName)
		{
			
			readerWriterLock.AcquireWriterLock(System.Threading.Timeout.Infinite);
			try
			{
				foreach (ResultInfo resultInfo in m_ResultsTable.Values)
				{
					if (resultInfo.TableName == tableName)
					{
						dataManager.GetRecordByName(tableName).SetAccessMethodParamsFromString(resultInfo.AccessMethodNum, resultInfo.AccessMethodParams);
						ArrayList arrayList = new ArrayList();
						dataManager.GetRecordByName(tableName).Load(resultInfo.AccessMethodNum, arrayList, 0);
						resultInfo.Result = arrayList;
						resultInfo.ResetHits();
					}
				}
			}
			finally
			{
				readerWriterLock.ReleaseWriterLock();
				
			}
		}

		public virtual async Task ReloadTableResultsInfoAsync(IDataManagerBase dataManager,  string tableName)
		{
			
			readerWriterLock.AcquireWriterLock(System.Threading.Timeout.Infinite);
			try
			{
				foreach (ResultInfo resultInfo in m_ResultsTable.Values)
				{
					if (resultInfo.TableName == tableName)
					{
						dataManager.GetRecordByName(tableName).SetAccessMethodParamsFromString(resultInfo.AccessMethodNum, resultInfo.AccessMethodParams);
						ArrayList arrayList = new ArrayList();
						await dataManager.GetRecordByName(tableName).LoadAsync(resultInfo.AccessMethodNum, arrayList, 0);
						resultInfo.Result = arrayList;
						resultInfo.ResetHits();
					}
				}
			}
			finally
			{
				readerWriterLock.ReleaseWriterLock();
				
			}
		}

		public virtual ArrayList GetTableResultsInfo(string tableName)
		{
			ArrayList resultsInfo = new ArrayList();
	
			readerWriterLock.AcquireReaderLock(System.Threading.Timeout.Infinite);
			try
			{
				foreach (ResultInfo resultInfo in m_ResultsTable.Values)
				{
					if (resultInfo.TableName == tableName)
					{
						resultsInfo.Add(resultInfo);
					}
				}
			}
			finally
			{
				readerWriterLock.ReleaseReaderLock();
			}
			return(resultsInfo);
		}
	
		public virtual ArrayList GetResultsInfoByType(string typeName)
		{
			ArrayList resultsInfo = new ArrayList();
	
			readerWriterLock.AcquireReaderLock(System.Threading.Timeout.Infinite);
			try
			{
				foreach (ResultInfo resultInfo in m_ResultsTable.Values)
				{
					//resultInfo = oneResultInfo as ResultInfo;
					if (resultInfo.ResultObjectName == typeName)
					{
						resultsInfo.Add(resultInfo);
					}
				}
			}
			finally
			{
				readerWriterLock.ReleaseReaderLock();
			}
			
			return(resultsInfo);
		}
	
		public virtual ArrayList GetAllResultsInfo()
		{
			ArrayList resultsInfo = new ArrayList();
	
			readerWriterLock.AcquireReaderLock(System.Threading.Timeout.Infinite);
			try
			{
				foreach (ResultInfo resultInfo in m_ResultsTable.Values)
				{
					resultsInfo.Add(resultInfo);
				}
			}
			finally
			{
				readerWriterLock.ReleaseReaderLock();
			}
			return(resultsInfo);
		}
	
		

	
		public virtual void AddToUncachedObjects(string objectName)
		{
			m_UncachedObjects[objectName] = 1;		
		}
	
		public virtual void RemoveFromUncachedObjects(string objectName)
		{
			if (m_UncachedObjects.ContainsKey(objectName))			
				m_UncachedObjects.Remove(objectName);
		}
	
		public virtual bool IsCachedObject(string objectName)
		{
			if (m_UncachedObjects.ContainsKey(objectName))			
				return (false);
			else
				return (true);
		}
	
		public virtual ArrayList GetUncachedObjects()
		{
			ArrayList uncachedObjects = new ArrayList();
	
			foreach (string key in m_UncachedObjects.Keys)
			{
				uncachedObjects.Add(key);
			}
			return(uncachedObjects);
		}
	
		public virtual void ClearAll()
		{
			readerWriterLock.AcquireWriterLock(System.Threading.Timeout.Infinite);
			try
			{
				m_ResultsTable.Clear();
			}
			finally
			{
				readerWriterLock.ReleaseWriterLock();
			}
		}
	
		public bool ReadFromCache
		{
			get { return m_bReadFromCache;}
			set { m_bReadFromCache = value;}
		}
	
		public bool ReadFromDirtyCache
		{
			get { return m_bReadFromDirtyCache;}
			set { m_bReadFromDirtyCache = value;}
		}
	
		public bool WriteToCache
		{
			get { return m_bWriteToCache;}
			set { m_bWriteToCache = value;}
		}

	}
}

  //      9857 ProCG uses this line - don't edit it