NuCache: reorg some classes

This commit is contained in:
Stephan
2019-02-22 16:03:39 +01:00
parent 66f9ecd8e2
commit b7a03f0153
7 changed files with 87 additions and 123 deletions

View File

@@ -0,0 +1,37 @@
using System;
using System.Threading;
namespace Umbraco.Web.PublishedCache.NuCache.Snap
{
internal class GenObj
{
public GenObj(long gen)
{
Gen = gen;
WeakGenRef = new WeakReference(null);
}
public GenRef GetGenRef()
{
// not thread-safe but always invoked from within a lock
var genRef = (GenRef)WeakGenRef.Target;
if (genRef == null)
WeakGenRef.Target = genRef = new GenRef(this);
return genRef;
}
public readonly long Gen;
public readonly WeakReference WeakGenRef;
public int Count;
public void Reference()
{
Interlocked.Increment(ref Count);
}
public void Release()
{
Interlocked.Decrement(ref Count);
}
}
}

View File

@@ -0,0 +1,13 @@
namespace Umbraco.Web.PublishedCache.NuCache.Snap
{
internal class GenRef
{
public GenRef(GenObj genObj)
{
GenObj = genObj;
}
public readonly GenObj GenObj;
public long Gen => GenObj.Gen;
}
}

View File

@@ -0,0 +1,20 @@
namespace Umbraco.Web.PublishedCache.NuCache.Snap
{
internal class LinkedNode<TValue>
where TValue : class
{
public LinkedNode(TValue value, long gen, LinkedNode<TValue> next = null)
{
Value = value;
Gen = gen;
Next = next;
}
public readonly long Gen;
// reading & writing references is thread-safe on all .NET platforms
// mark as volatile to ensure we always read the correct value
public volatile TValue Value;
public volatile LinkedNode<TValue> Next;
}
}