This repository has been archived by the owner on May 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add base on timer evictor,tmed validation pooled objects
- Loading branch information
ULiiAn
committed
Jun 29, 2017
1 parent
671d0ed
commit 5026f22
Showing
7 changed files
with
245 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright (c) GZNB. All rights reserved. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace CodeProject.ObjectPool | ||
{ | ||
/// <summary> | ||
/// Eviction Config Infomation | ||
/// </summary> | ||
public class EvictionConfig | ||
{ | ||
/// <summary> | ||
/// Eviction Is Enable default is false | ||
/// </summary> | ||
public bool Enable { get; set; } = false; | ||
|
||
/// <summary> | ||
/// Timer delay time default is zero | ||
/// </summary> | ||
public TimeSpan Delay { get; set; } = TimeSpan.Zero; | ||
|
||
/// <summary> | ||
/// Timer period default is one minute | ||
/// </summary> | ||
public TimeSpan Period { get; set; } = TimeSpan.FromMinutes(1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// Copyright (c) GZNB. All rights reserved. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
|
||
#if !NET35 | ||
using CodeProject.ObjectPool.Logging; | ||
#endif | ||
|
||
|
||
namespace CodeProject.ObjectPool | ||
{ | ||
#if !NETSTD10 | ||
public class EvictorTimer : IEvictionTimer, IDisposable | ||
{ | ||
#if !NET35 | ||
private static readonly ILog Log = LogProvider.GetLogger(typeof(EvictorTimer)); | ||
#endif | ||
private Dictionary<Action, Timer> _actionMap; | ||
private volatile bool _disposed; | ||
|
||
public EvictorTimer() | ||
{ | ||
this._actionMap = new Dictionary<Action, Timer>(); | ||
} | ||
|
||
/// <summary>执行与释放或重置非托管资源关联的应用程序定义的任务。</summary> | ||
public void Dispose() | ||
{ | ||
this.Dispose(true); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
/// <summary> | ||
/// Add Eviction action | ||
/// </summary> | ||
/// <param name="action">eviction action</param> | ||
/// <param name="delay">delay time</param> | ||
/// <param name="period">period</param> | ||
public void Schedule(Action action, TimeSpan delay, TimeSpan period) | ||
{ | ||
this.ThrowIfDisposed(); | ||
if (action == null) | ||
{ | ||
return; | ||
} | ||
lock (typeof(EvictorTimer)) | ||
{ | ||
#if!NET35 | ||
Action piplineAction = () => Log.Debug("Begin Schedule Evictor"); | ||
piplineAction += action; | ||
piplineAction += () => Log.Debug("End Schedule Evictor"); | ||
action = piplineAction; | ||
#endif | ||
if (this._actionMap.TryGetValue(action, out Timer timer)) | ||
{ | ||
timer.Change(delay, period); | ||
} | ||
else | ||
{ | ||
var t = new Timer(state => action(), null, delay, period); | ||
this._actionMap[action] = t; | ||
} | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Cancle Action | ||
/// </summary> | ||
/// <param name="task"></param> | ||
public void Cancel(Action task) | ||
{ | ||
this.ThrowIfDisposed(); | ||
lock (typeof(EvictorTimer)) | ||
{ | ||
if (this._actionMap.TryGetValue(task, out Timer timer)) | ||
{ | ||
this._actionMap.Remove(task); | ||
timer.Dispose(); | ||
} | ||
} | ||
} | ||
|
||
private void ThrowIfDisposed() | ||
{ | ||
if (this._disposed) | ||
{ | ||
throw new ObjectDisposedException(this.GetType().FullName); | ||
} | ||
} | ||
|
||
~EvictorTimer() | ||
{ | ||
this.Dispose(false); | ||
} | ||
|
||
protected virtual void Dispose(bool disposing) | ||
{ | ||
if (!this._disposed) | ||
{ | ||
this._disposed = true; | ||
if (disposing) | ||
{ | ||
Timer[] timers = this._actionMap?.Values.ToArray() ?? new Timer[0]; | ||
this._actionMap.Clear(); | ||
this._actionMap = null; | ||
foreach (Timer t in timers) | ||
{ | ||
t.Dispose(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
#endif | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace CodeProject.ObjectPool | ||
{ | ||
/// <summary> | ||
/// Eviction Timer Interface | ||
/// </summary> | ||
public interface IEvictionTimer | ||
{ | ||
/// <summary> | ||
/// Add Eviction action | ||
/// </summary> | ||
/// <param name="action">eviction action</param> | ||
/// <param name="delay">delay time</param> | ||
/// <param name="period">period</param> | ||
void Schedule(Action action, TimeSpan delay, TimeSpan period); | ||
|
||
/// <summary> | ||
/// Cancle Action | ||
/// </summary> | ||
/// <param name="task"></param> | ||
void Cancel(Action task); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters