Skip to content

Commit

Permalink
add automatic collection
Browse files Browse the repository at this point in the history
  • Loading branch information
mgravell committed Jul 22, 2011
1 parent 724caf1 commit 8ed5f61
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions .hgignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ syntax: glob
glob:Dapper.Contrib/NuGet.exe
*.nupkg
*/NuGet.exe
_ReSharper.*
26 changes: 26 additions & 0 deletions Dapper/SqlMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,34 @@ private static void PurgeQueryCache(Identity key)
static readonly System.Collections.Concurrent.ConcurrentDictionary<Identity, CacheInfo> _queryCache = new System.Collections.Concurrent.ConcurrentDictionary<Identity, CacheInfo>();
private static void SetQueryCache(Identity key, CacheInfo value)
{
if(Interlocked.Increment(ref collect)==COLLECT_PER_ITEMS)
{
CollectCacheGarbage();
}
_queryCache[key] = value;
}

private static void CollectCacheGarbage()
{
try
{
foreach (var pair in _queryCache)
{
if (pair.Value.GetHitCount() <= COLLECT_HIT_COUNT_MIN)
{
CacheInfo cache;
_queryCache.TryRemove(pair.Key, out cache);
}
}
}
finally
{
Interlocked.Exchange(ref collect, 0);
}
}

private const int COLLECT_PER_ITEMS = 1000, COLLECT_HIT_COUNT_MIN = 0;
private static int collect;
private static bool TryGetQueryCache(Identity key, out CacheInfo value)
{
if(_queryCache.TryGetValue(key, out value))
Expand Down

0 comments on commit 8ed5f61

Please sign in to comment.