- zetalibnet-src.zip
- Core
- Base
- bin
- Release
- de
- ZetaLib.Core.resources.dll
- ICSharpCode.SharpZipLib.dll
- Interop.ADODB.dll
- Interop.JRO.dll
- log4net.dll
- ZetaLib.Core.dll
- Caching
- Collections
- Common
- Core.csproj
- Core.csproj.user
- Core.csproj.vspscc
- Data
- DirectoryServices
- IO
- LibraryConfiguration.cs
- Localization
- Logging
- Misc
- mssccprj.scc
- Properties
- vssver2.scc
- zetalib.core.snk
- mssccprj.scc
- References
- ClickOnceButton
- bin
- ClickOnceButton.dll
- src
- log4net
- log4net.dll
- SharpZip.NET
- Test
- vssver2.scc
- Web
- Base
- bin
- Release
- ClickOnceButton.dll
- de
- ZetaLib.Core.resources.dll
- ZetaLib.Web.resources.dll
- ICSharpCode.SharpZipLib.dll
- Interop.ADODB.dll
- Interop.JRO.dll
- log4net.dll
- ZetaLib.Core.dll
- ZetaLib.Web.dll
- Caching
- Common
- Controls
- LibraryConfiguration.cs
- mssccprj.scc
- Properties
- vssver2.scc
- Web.csproj
- Web.csproj.user
- Web.csproj.vspscc
- zetalib.web.snk
- Windows
- Base
- bin
- Release
- de
- ZetaLib.Core.resources.dll
- ZetaLib.Windows.resources.dll
- ICSharpCode.SharpZipLib.dll
- Interop.ADODB.dll
- Interop.JRO.dll
- log4net.dll
- ZetaLib.Core.dll
- ZetaLib.Windows.dll
- Common
- Controls
- Dialogs
- LibraryConfiguration.cs
- Misc
- mssccprj.scc
- Properties
- vssver2.scc
- Windows.csproj
- Windows.csproj.user
- Windows.csproj.vspscc
- zetalib.windows.snk
- ZetaLibNet.sln
- ZetaLibNet.vssscc
|
namespace ZetaLib.Core.Caching
{
#region Using directives.
// ----------------------------------------------------------------------
using System;
using System.Threading;
using ZetaLib.Core.Common;
using System.Configuration;
using ZetaLib.Core.Logging;
// ----------------------------------------------------------------------
#endregion
/////////////////////////////////////////////////////////////////////////
/// <summary>
/// Cache that cleans up asynchronously in a background thread.
/// </summary>
/// <remarks>
/// Configure externally through the configuration app setting values:
/// - "asyncThreadCleanupInterval":
/// The interval in milliseconds. Default is 60,000 (60 seconds).
/// - "disableAsyncThreadCleanup":
/// Disables the asynchron handling, behaves synchron.
/// Default is FALSE.
/// </remarks>
public class AsyncCleanupCacheBackend :
SimpleCacheBackend,
IDisposable
{
#region Public methods.
// ------------------------------------------------------------------
/// <summary>
/// Constructor.
/// </summary>
public AsyncCleanupCacheBackend()
{
if ( isAsyncEnabled )
{
LogCentral.Current.LogDebug(
string.Format(
@"[Cache] Constructing asynchron cache backend with 'isAsyncEnabled' set to {0}.",
isAsyncEnabled ) );
TimerCallback timerDelegate =
new TimerCallback( DoAsyncCleanup );
// Create new timer to regularily execute the cleanup.
asyncTimer = new Timer(
DoAsyncCleanup,
null,
timerIntervalMilliSeconds,
timerIntervalMilliSeconds
);
}
}
// ------------------------------------------------------------------
#endregion
#region Private methods.
// ------------------------------------------------------------------
/// <summary>
/// Function that is being called after a value is being set.
/// The default checks syncronously for old items and deletes them.
/// Override to provide your own implementation.
/// </summary>
protected override void OnAfterValueSet()
{
if ( isAsyncEnabled )
{
base.OnAfterValueSet();
}
else
{
// Do NOT call the base class here to prevent
// syncronous update.
}
}
/// <summary>
/// Does the async cleanup.
/// </summary>
/// <param name="state">The state.</param>
private void DoAsyncCleanup(
object state )
{
lock ( thisLock )
{
DateTime d1 = DateTime.Now;
LogCentral.Current.LogDebug(
string.Format(
@"[Cache] Starting asnychron cleanup of cache at '{0}'.",
d1 ) );
int removedCount = CleanupOldEntries();
DateTime d2 = DateTime.Now;
LogCentral.Current.LogDebug(
string.Format(
@"[Cache] Finished asnychron cleanup of cache at '{0}', removed {1} items, took '{2}'.",
d2,
removedCount,
d2 - d1 ) );
}
}
// ------------------------------------------------------------------
#endregion
#region Private variables.
// ------------------------------------------------------------------
/// <summary>
/// Best practice, see C# MSDN documentation of the "lock" keyword.
/// </summary>
private object thisLock = new object();
/// <summary>
/// The timer that regularily executes the cleanup.
/// </summary>
private Timer asyncTimer;
/// <summary>
/// Once a minute by default, changeable via config.
/// </summary>
private static readonly int timerIntervalMilliSeconds =
ConvertHelper.ToInt32(
ConfigurationManager.AppSettings[@"asyncThreadCleanupInterval"],
60 * 1000 );
/// <summary>
/// Flag to disable.
/// </summary>
private static readonly bool isAsyncEnabled =
!ConvertHelper.ToBoolean(
ConfigurationManager.AppSettings[@"disableAsyncThreadCleanup"],
false );
// ------------------------------------------------------------------
#endregion
#region IDisposable members.
// ------------------------------------------------------------------
/// <summary>
/// Performs application-defined tasks associated with freeing,
/// releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
if ( asyncTimer != null )
{
Timer t = asyncTimer;
asyncTimer = null;
t.Dispose();
}
}
// ------------------------------------------------------------------
#endregion
}
/////////////////////////////////////////////////////////////////////////
}
|
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.