You've already forked linux-packaging-mono
Imported Upstream version 4.6.0.125
Former-commit-id: a2155e9bd80020e49e72e86c44da02a8ac0e57a4
This commit is contained in:
parent
a569aebcfd
commit
e79aa3c0ed
@@ -0,0 +1,247 @@
|
||||
//------------------------------------------------------------
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
//------------------------------------------------------------
|
||||
|
||||
namespace System.Runtime.Collections
|
||||
{
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Threading;
|
||||
using System.Diagnostics;
|
||||
|
||||
|
||||
// This cache works like a MruCache, but operates loosely and without locks in the mainline path.
|
||||
//
|
||||
// It consists of three 'hoppers', which are Hashtables (chosen for their nice threading characteristics - reading
|
||||
// doesn't require a lock). Items enter the cache in the second hopper. On lookups, cache hits result in the
|
||||
// cache entry being promoted to the first hopper. When the first hopper is full, the third hopper is dropped,
|
||||
// and the first and second hoppers are shifted down, leaving an empty first hopper. If the second hopper is
|
||||
// full when a new cache entry is added, the third hopper is dropped, the second hopper is shifted down, and a
|
||||
// new second hopper is slotted in to become the new item entrypoint.
|
||||
//
|
||||
// Items can only be added and looked up. There's no way to remove an item besides through attrition.
|
||||
//
|
||||
// This cache has a built-in concept of weakly-referenced items (which can be enabled or disabled in the
|
||||
// constructor). It needs this concept since the caller of the cache can't remove dead cache items itself.
|
||||
// A weak HopperCache will simply ignore dead entries.
|
||||
//
|
||||
// This structure allows cache lookups to be almost lock-free. The only time the first hopper is written to
|
||||
// is when a cache entry is promoted. Promoting a cache entry is not critical - it's ok to skip a promotion.
|
||||
// Only one promotion is allowed at a time. If a second is attempted, it is skipped. This allows promotions
|
||||
// to be synchronized with just an Interlocked call.
|
||||
//
|
||||
// New cache entries go into the second hopper, which requires a lock, as does shifting the hoppers down.
|
||||
//
|
||||
// The hopperSize parameter determines the size of the first hopper. When it reaches this size, the hoppers
|
||||
// are shifted. The second hopper is allowed to grow to twice this size. This is because it needs room to get
|
||||
// new cache entries into the system, and the second hopper typically starts out 'full'. Entries are never added
|
||||
// directly to the third hopper.
|
||||
//
|
||||
// It's a error on the part of the caller to add the same key to the cache again if it's already in the cache
|
||||
// with a different value. The new value will not necessarily overwrite the old value.
|
||||
//
|
||||
// If a cache entry is about to be promoted from the third hopper, and in the mean time the third hopper has been
|
||||
// shifted away, an intervening GetValue for the same key might return null, even though the item is still in
|
||||
// the cache and a later GetValue might find it. So it's very important never to add the same key to the cache
|
||||
// with two different values, even if GetValue returns null for the key in-between the first add and the second.
|
||||
// (If this particular behavior is a problem, it may be possible to tighten up, but it's not necessary for the
|
||||
// current use of HopperCache - UriPrefixTable.)
|
||||
class HopperCache
|
||||
{
|
||||
readonly int hopperSize;
|
||||
readonly bool weak;
|
||||
|
||||
Hashtable outstandingHopper;
|
||||
Hashtable strongHopper;
|
||||
Hashtable limitedHopper;
|
||||
int promoting;
|
||||
LastHolder mruEntry;
|
||||
|
||||
|
||||
public HopperCache(int hopperSize, bool weak)
|
||||
{
|
||||
Fx.Assert(hopperSize > 0, "HopperCache hopperSize must be positive.");
|
||||
|
||||
this.hopperSize = hopperSize;
|
||||
this.weak = weak;
|
||||
|
||||
this.outstandingHopper = new Hashtable(hopperSize * 2);
|
||||
this.strongHopper = new Hashtable(hopperSize * 2);
|
||||
this.limitedHopper = new Hashtable(hopperSize * 2);
|
||||
}
|
||||
|
||||
// Calls to Add must be synchronized.
|
||||
public void Add(object key, object value)
|
||||
{
|
||||
Fx.Assert(key != null, "HopperCache key cannot be null.");
|
||||
Fx.Assert(value != null, "HopperCache value cannot be null.");
|
||||
|
||||
// Special-case DBNull since it can never be collected.
|
||||
if (this.weak && !object.ReferenceEquals(value, DBNull.Value))
|
||||
{
|
||||
value = new WeakReference(value);
|
||||
}
|
||||
|
||||
Fx.Assert(this.strongHopper.Count <= this.hopperSize * 2,
|
||||
"HopperCache strongHopper is bigger than it's allowed to get.");
|
||||
|
||||
if (this.strongHopper.Count >= this.hopperSize * 2)
|
||||
{
|
||||
Hashtable recycled = this.limitedHopper;
|
||||
recycled.Clear();
|
||||
recycled.Add(key, value);
|
||||
|
||||
// The try/finally is here to make sure these happen without interruption.
|
||||
try { } finally
|
||||
{
|
||||
this.limitedHopper = this.strongHopper;
|
||||
this.strongHopper = recycled;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// We do nothing to prevent things from getting added multiple times. Also may be writing over
|
||||
// a dead weak entry.
|
||||
this.strongHopper[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
// Calls to GetValue do not need to be synchronized, but the object used to synchronize the Add calls
|
||||
// must be passed in. It's sometimes used.
|
||||
public object GetValue(object syncObject, object key)
|
||||
{
|
||||
Fx.Assert(key != null, "Can't look up a null key.");
|
||||
|
||||
WeakReference weakRef;
|
||||
object value;
|
||||
|
||||
// The MruCache does this so we have to too.
|
||||
LastHolder last = this.mruEntry;
|
||||
if (last != null && key.Equals(last.Key))
|
||||
{
|
||||
if (this.weak && (weakRef = last.Value as WeakReference) != null)
|
||||
{
|
||||
value = weakRef.Target;
|
||||
if (value != null)
|
||||
{
|
||||
return value;
|
||||
}
|
||||
this.mruEntry = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
return last.Value;
|
||||
}
|
||||
}
|
||||
|
||||
// Try the first hopper.
|
||||
object origValue = this.outstandingHopper[key];
|
||||
value = this.weak && (weakRef = origValue as WeakReference) != null ? weakRef.Target : origValue;
|
||||
if (value != null)
|
||||
{
|
||||
this.mruEntry = new LastHolder(key, origValue);
|
||||
return value;
|
||||
}
|
||||
|
||||
// Try the subsequent hoppers.
|
||||
origValue = this.strongHopper[key];
|
||||
value = this.weak && (weakRef = origValue as WeakReference) != null ? weakRef.Target : origValue;
|
||||
if (value == null)
|
||||
{
|
||||
origValue = this.limitedHopper[key];
|
||||
value = this.weak && (weakRef = origValue as WeakReference) != null ? weakRef.Target : origValue;
|
||||
if (value == null)
|
||||
{
|
||||
// Still no value? It's not here.
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
this.mruEntry = new LastHolder(key, origValue);
|
||||
|
||||
// If we can get the promoting semaphore, move up to the outstanding hopper.
|
||||
int wasPromoting = 1;
|
||||
try
|
||||
{
|
||||
try { } finally
|
||||
{
|
||||
// This is effectively a lock, which is why it uses lock semantics. If the Interlocked call
|
||||
// were 'lost', the cache wouldn't deadlock, but it would be permanently broken.
|
||||
wasPromoting = Interlocked.CompareExchange(ref this.promoting, 1, 0);
|
||||
}
|
||||
|
||||
// Only one thread can be inside this 'if' at a time.
|
||||
if (wasPromoting == 0)
|
||||
{
|
||||
Fx.Assert(this.outstandingHopper.Count <= this.hopperSize,
|
||||
"HopperCache outstandingHopper is bigger than it's allowed to get.");
|
||||
|
||||
if (this.outstandingHopper.Count >= this.hopperSize)
|
||||
{
|
||||
lock (syncObject)
|
||||
{
|
||||
Hashtable recycled = this.limitedHopper;
|
||||
recycled.Clear();
|
||||
recycled.Add(key, origValue);
|
||||
|
||||
// The try/finally is here to make sure these happen without interruption.
|
||||
try { } finally
|
||||
{
|
||||
this.limitedHopper = this.strongHopper;
|
||||
this.strongHopper = this.outstandingHopper;
|
||||
this.outstandingHopper = recycled;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// It's easy for this to happen twice with the same key.
|
||||
//
|
||||
// It's important that no one else can be shifting the current oustandingHopper
|
||||
// during this operation. We are only allowed to modify the *current* outstandingHopper
|
||||
// while holding the pseudo-lock, which would be violated if it could be shifted out from
|
||||
// under us (and potentially added to by Add in a ----).
|
||||
this.outstandingHopper[key] = origValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (wasPromoting == 0)
|
||||
{
|
||||
this.promoting = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
class LastHolder
|
||||
{
|
||||
readonly object key;
|
||||
readonly object value;
|
||||
|
||||
internal LastHolder(object key, object value)
|
||||
{
|
||||
this.key = key;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
internal object Key
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.key;
|
||||
}
|
||||
}
|
||||
|
||||
internal object Value
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,372 @@
|
||||
//-----------------------------------------------------------------------------
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
namespace System.Runtime.Collections
|
||||
{
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Runtime;
|
||||
|
||||
class NullableKeyDictionary<TKey, TValue> : IDictionary<TKey, TValue>
|
||||
{
|
||||
bool isNullKeyPresent;
|
||||
TValue nullKeyValue;
|
||||
IDictionary<TKey, TValue> innerDictionary;
|
||||
|
||||
public NullableKeyDictionary()
|
||||
: base()
|
||||
{
|
||||
this.innerDictionary = new Dictionary<TKey, TValue>();
|
||||
}
|
||||
|
||||
public int Count
|
||||
{
|
||||
get { return this.innerDictionary.Count + (this.isNullKeyPresent ? 1 : 0); }
|
||||
}
|
||||
|
||||
public bool IsReadOnly
|
||||
{
|
||||
get { return false; }
|
||||
}
|
||||
|
||||
public ICollection<TKey> Keys
|
||||
{
|
||||
get
|
||||
{
|
||||
return new NullKeyDictionaryKeyCollection<TKey, TValue>(this);
|
||||
}
|
||||
}
|
||||
|
||||
public ICollection<TValue> Values
|
||||
{
|
||||
get { return new NullKeyDictionaryValueCollection<TKey, TValue>(this); }
|
||||
}
|
||||
|
||||
public TValue this[TKey key]
|
||||
{
|
||||
get
|
||||
{
|
||||
if (key == null)
|
||||
{
|
||||
if (this.isNullKeyPresent)
|
||||
{
|
||||
return this.nullKeyValue;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw Fx.Exception.AsError(new KeyNotFoundException());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return this.innerDictionary[key];
|
||||
}
|
||||
}
|
||||
set
|
||||
{
|
||||
if (key == null)
|
||||
{
|
||||
this.isNullKeyPresent = true;
|
||||
this.nullKeyValue = value;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.innerDictionary[key] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Add(TKey key, TValue value)
|
||||
{
|
||||
if (key == null)
|
||||
{
|
||||
if (this.isNullKeyPresent)
|
||||
{
|
||||
throw Fx.Exception.Argument("key", InternalSR.NullKeyAlreadyPresent);
|
||||
}
|
||||
this.isNullKeyPresent = true;
|
||||
this.nullKeyValue = value;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.innerDictionary.Add(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
public bool ContainsKey(TKey key)
|
||||
{
|
||||
return key == null ? this.isNullKeyPresent : this.innerDictionary.ContainsKey(key);
|
||||
}
|
||||
|
||||
public bool Remove(TKey key)
|
||||
{
|
||||
if (key == null)
|
||||
{
|
||||
bool result = this.isNullKeyPresent;
|
||||
this.isNullKeyPresent = false;
|
||||
this.nullKeyValue = default(TValue);
|
||||
return result;
|
||||
}
|
||||
else
|
||||
{
|
||||
return this.innerDictionary.Remove(key);
|
||||
}
|
||||
}
|
||||
|
||||
public bool TryGetValue(TKey key, out TValue value)
|
||||
{
|
||||
if (key == null)
|
||||
{
|
||||
if (this.isNullKeyPresent)
|
||||
{
|
||||
value = this.nullKeyValue;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
value = default(TValue);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return this.innerDictionary.TryGetValue(key, out value);
|
||||
}
|
||||
}
|
||||
|
||||
public void Add(KeyValuePair<TKey, TValue> item)
|
||||
{
|
||||
Add(item.Key, item.Value);
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
this.isNullKeyPresent = false;
|
||||
this.nullKeyValue = default(TValue);
|
||||
this.innerDictionary.Clear();
|
||||
}
|
||||
|
||||
public bool Contains(KeyValuePair<TKey, TValue> item)
|
||||
{
|
||||
if (item.Key == null)
|
||||
{
|
||||
if (this.isNullKeyPresent)
|
||||
{
|
||||
return item.Value == null ? this.nullKeyValue == null : item.Value.Equals(this.nullKeyValue);
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return this.innerDictionary.Contains(item);
|
||||
}
|
||||
}
|
||||
|
||||
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
|
||||
{
|
||||
this.innerDictionary.CopyTo(array, arrayIndex);
|
||||
if (this.isNullKeyPresent)
|
||||
{
|
||||
array[arrayIndex + this.innerDictionary.Count] = new KeyValuePair<TKey, TValue>(default(TKey), this.nullKeyValue);
|
||||
}
|
||||
}
|
||||
|
||||
public bool Remove(KeyValuePair<TKey, TValue> item)
|
||||
{
|
||||
if (item.Key == null)
|
||||
{
|
||||
if (this.Contains(item))
|
||||
{
|
||||
this.isNullKeyPresent = false;
|
||||
this.nullKeyValue = default(TValue);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return this.innerDictionary.Remove(item);
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
|
||||
{
|
||||
IEnumerator<KeyValuePair<TKey, TValue>> innerEnumerator = this.innerDictionary.GetEnumerator() as IEnumerator<KeyValuePair<TKey, TValue>>;
|
||||
|
||||
while (innerEnumerator.MoveNext())
|
||||
{
|
||||
yield return innerEnumerator.Current;
|
||||
}
|
||||
|
||||
if (this.isNullKeyPresent)
|
||||
{
|
||||
yield return new KeyValuePair<TKey, TValue>(default(TKey), this.nullKeyValue);
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return ((IEnumerable<KeyValuePair<TKey, TValue>>)this).GetEnumerator();
|
||||
}
|
||||
|
||||
class NullKeyDictionaryKeyCollection<TypeKey, TypeValue> : ICollection<TypeKey>
|
||||
{
|
||||
NullableKeyDictionary<TypeKey, TypeValue> nullKeyDictionary;
|
||||
|
||||
public NullKeyDictionaryKeyCollection(NullableKeyDictionary<TypeKey, TypeValue> nullKeyDictionary)
|
||||
{
|
||||
this.nullKeyDictionary = nullKeyDictionary;
|
||||
}
|
||||
|
||||
public int Count
|
||||
{
|
||||
get
|
||||
{
|
||||
int count = this.nullKeyDictionary.innerDictionary.Keys.Count;
|
||||
if (this.nullKeyDictionary.isNullKeyPresent)
|
||||
{
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsReadOnly
|
||||
{
|
||||
get { return true; }
|
||||
}
|
||||
|
||||
public void Add(TypeKey item)
|
||||
{
|
||||
throw Fx.Exception.AsError(new NotSupportedException(InternalSR.KeyCollectionUpdatesNotAllowed));
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
throw Fx.Exception.AsError(new NotSupportedException(InternalSR.KeyCollectionUpdatesNotAllowed));
|
||||
}
|
||||
|
||||
public bool Contains(TypeKey item)
|
||||
{
|
||||
return item == null ? this.nullKeyDictionary.isNullKeyPresent : this.nullKeyDictionary.innerDictionary.Keys.Contains(item);
|
||||
}
|
||||
|
||||
public void CopyTo(TypeKey[] array, int arrayIndex)
|
||||
{
|
||||
this.nullKeyDictionary.innerDictionary.Keys.CopyTo(array, arrayIndex);
|
||||
if (this.nullKeyDictionary.isNullKeyPresent)
|
||||
{
|
||||
array[arrayIndex + this.nullKeyDictionary.innerDictionary.Keys.Count] = default(TypeKey);
|
||||
}
|
||||
}
|
||||
|
||||
public bool Remove(TypeKey item)
|
||||
{
|
||||
throw Fx.Exception.AsError(new NotSupportedException(InternalSR.KeyCollectionUpdatesNotAllowed));
|
||||
}
|
||||
|
||||
public IEnumerator<TypeKey> GetEnumerator()
|
||||
{
|
||||
foreach (TypeKey item in this.nullKeyDictionary.innerDictionary.Keys)
|
||||
{
|
||||
yield return item;
|
||||
}
|
||||
|
||||
if (this.nullKeyDictionary.isNullKeyPresent)
|
||||
{
|
||||
yield return default(TypeKey);
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return ((IEnumerable<TypeKey>)this).GetEnumerator();
|
||||
}
|
||||
}
|
||||
|
||||
class NullKeyDictionaryValueCollection<TypeKey, TypeValue> : ICollection<TypeValue>
|
||||
{
|
||||
NullableKeyDictionary<TypeKey, TypeValue> nullKeyDictionary;
|
||||
|
||||
public NullKeyDictionaryValueCollection(NullableKeyDictionary<TypeKey, TypeValue> nullKeyDictionary)
|
||||
{
|
||||
this.nullKeyDictionary = nullKeyDictionary;
|
||||
}
|
||||
|
||||
public int Count
|
||||
{
|
||||
get
|
||||
{
|
||||
int count = this.nullKeyDictionary.innerDictionary.Values.Count;
|
||||
if (this.nullKeyDictionary.isNullKeyPresent)
|
||||
{
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsReadOnly
|
||||
{
|
||||
get { return true; }
|
||||
}
|
||||
|
||||
public void Add(TypeValue item)
|
||||
{
|
||||
throw Fx.Exception.AsError(new NotSupportedException(InternalSR.ValueCollectionUpdatesNotAllowed));
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
throw Fx.Exception.AsError(new NotSupportedException(InternalSR.ValueCollectionUpdatesNotAllowed));
|
||||
}
|
||||
|
||||
public bool Contains(TypeValue item)
|
||||
{
|
||||
return this.nullKeyDictionary.innerDictionary.Values.Contains(item) ||
|
||||
(this.nullKeyDictionary.isNullKeyPresent && this.nullKeyDictionary.nullKeyValue.Equals(item));
|
||||
}
|
||||
|
||||
public void CopyTo(TypeValue[] array, int arrayIndex)
|
||||
{
|
||||
this.nullKeyDictionary.innerDictionary.Values.CopyTo(array, arrayIndex);
|
||||
if (this.nullKeyDictionary.isNullKeyPresent)
|
||||
{
|
||||
array[arrayIndex + this.nullKeyDictionary.innerDictionary.Values.Count] = this.nullKeyDictionary.nullKeyValue;
|
||||
}
|
||||
}
|
||||
|
||||
public bool Remove(TypeValue item)
|
||||
{
|
||||
throw Fx.Exception.AsError(new NotSupportedException(InternalSR.ValueCollectionUpdatesNotAllowed));
|
||||
}
|
||||
|
||||
public IEnumerator<TypeValue> GetEnumerator()
|
||||
{
|
||||
foreach (TypeValue item in this.nullKeyDictionary.innerDictionary.Values)
|
||||
{
|
||||
yield return item;
|
||||
}
|
||||
|
||||
if (this.nullKeyDictionary.isNullKeyPresent)
|
||||
{
|
||||
yield return this.nullKeyDictionary.nullKeyValue;
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return ((IEnumerable<TypeValue>)this).GetEnumerator();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,15 @@
|
||||
//------------------------------------------------------------
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
//------------------------------------------------------------
|
||||
|
||||
namespace System.Runtime.Collections
|
||||
{
|
||||
abstract class ObjectCacheItem<T>
|
||||
where T : class
|
||||
{
|
||||
// only valid when you've called TryAddReference successfully
|
||||
public abstract T Value { get; }
|
||||
public abstract bool TryAddReference();
|
||||
public abstract void ReleaseReference();
|
||||
}
|
||||
}
|
@@ -0,0 +1,93 @@
|
||||
//------------------------------------------------------------
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
//------------------------------------------------------------
|
||||
|
||||
namespace System.Runtime.Collections
|
||||
{
|
||||
class ObjectCacheSettings
|
||||
{
|
||||
int cacheLimit;
|
||||
TimeSpan idleTimeout;
|
||||
TimeSpan leaseTimeout;
|
||||
int purgeFrequency;
|
||||
|
||||
const int DefaultCacheLimit = 64;
|
||||
const int DefaultPurgeFrequency = 32;
|
||||
static TimeSpan DefaultIdleTimeout = TimeSpan.FromMinutes(2);
|
||||
static TimeSpan DefaultLeaseTimeout = TimeSpan.FromMinutes(5);
|
||||
|
||||
public ObjectCacheSettings()
|
||||
{
|
||||
this.CacheLimit = DefaultCacheLimit;
|
||||
this.IdleTimeout = DefaultIdleTimeout;
|
||||
this.LeaseTimeout = DefaultLeaseTimeout;
|
||||
this.PurgeFrequency = DefaultPurgeFrequency;
|
||||
}
|
||||
|
||||
ObjectCacheSettings(ObjectCacheSettings other)
|
||||
{
|
||||
this.CacheLimit = other.CacheLimit;
|
||||
this.IdleTimeout = other.IdleTimeout;
|
||||
this.LeaseTimeout = other.LeaseTimeout;
|
||||
this.PurgeFrequency = other.PurgeFrequency;
|
||||
}
|
||||
|
||||
internal ObjectCacheSettings Clone()
|
||||
{
|
||||
return new ObjectCacheSettings(this);
|
||||
}
|
||||
|
||||
public int CacheLimit
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.cacheLimit;
|
||||
}
|
||||
set
|
||||
{
|
||||
Fx.Assert(value >= 0, "caller should validate cache limit is non-negative");
|
||||
this.cacheLimit = value;
|
||||
}
|
||||
}
|
||||
|
||||
public TimeSpan IdleTimeout
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.idleTimeout;
|
||||
}
|
||||
set
|
||||
{
|
||||
Fx.Assert(value >= TimeSpan.Zero, "caller should validate cache limit is non-negative");
|
||||
this.idleTimeout = value;
|
||||
}
|
||||
}
|
||||
|
||||
public TimeSpan LeaseTimeout
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.leaseTimeout;
|
||||
}
|
||||
set
|
||||
{
|
||||
Fx.Assert(value >= TimeSpan.Zero, "caller should validate cache limit is non-negative");
|
||||
this.leaseTimeout = value;
|
||||
}
|
||||
}
|
||||
|
||||
public int PurgeFrequency
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.purgeFrequency;
|
||||
}
|
||||
set
|
||||
{
|
||||
Fx.Assert(value >= 0, "caller should validate purge frequency is non-negative");
|
||||
this.purgeFrequency = value;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,342 @@
|
||||
//-----------------------------------------------------------------------------
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
namespace System.Runtime.Collections
|
||||
{
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Specialized;
|
||||
|
||||
// System.Collections.Specialized.OrderedDictionary is NOT generic.
|
||||
// This class is essentially a generic wrapper for OrderedDictionary.
|
||||
class OrderedDictionary<TKey, TValue> : IDictionary<TKey, TValue>, IDictionary
|
||||
{
|
||||
OrderedDictionary privateDictionary;
|
||||
|
||||
public OrderedDictionary()
|
||||
{
|
||||
this.privateDictionary = new OrderedDictionary();
|
||||
}
|
||||
|
||||
public OrderedDictionary(IDictionary<TKey, TValue> dictionary)
|
||||
{
|
||||
if (dictionary != null)
|
||||
{
|
||||
this.privateDictionary = new OrderedDictionary();
|
||||
|
||||
foreach (KeyValuePair<TKey, TValue> pair in dictionary)
|
||||
{
|
||||
this.privateDictionary.Add(pair.Key, pair.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int Count
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.privateDictionary.Count;
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsReadOnly
|
||||
{
|
||||
get
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public TValue this[TKey key]
|
||||
{
|
||||
get
|
||||
{
|
||||
if (key == null)
|
||||
{
|
||||
throw Fx.Exception.ArgumentNull("key");
|
||||
}
|
||||
|
||||
if (this.privateDictionary.Contains(key))
|
||||
{
|
||||
return (TValue)this.privateDictionary[(object)key];
|
||||
}
|
||||
else
|
||||
{
|
||||
throw Fx.Exception.AsError(new KeyNotFoundException(InternalSR.KeyNotFoundInDictionary));
|
||||
}
|
||||
}
|
||||
set
|
||||
{
|
||||
if (key == null)
|
||||
{
|
||||
throw Fx.Exception.ArgumentNull("key");
|
||||
}
|
||||
|
||||
this.privateDictionary[(object)key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
public ICollection<TKey> Keys
|
||||
{
|
||||
get
|
||||
{
|
||||
List<TKey> keys = new List<TKey>(this.privateDictionary.Count);
|
||||
|
||||
foreach (TKey key in this.privateDictionary.Keys)
|
||||
{
|
||||
keys.Add(key);
|
||||
}
|
||||
|
||||
// Keys should be put in a ReadOnlyCollection,
|
||||
// but since this is an internal class, for performance reasons,
|
||||
// we choose to avoid creating yet another collection.
|
||||
|
||||
return keys;
|
||||
}
|
||||
}
|
||||
|
||||
public ICollection<TValue> Values
|
||||
{
|
||||
get
|
||||
{
|
||||
List<TValue> values = new List<TValue>(this.privateDictionary.Count);
|
||||
|
||||
foreach (TValue value in this.privateDictionary.Values)
|
||||
{
|
||||
values.Add(value);
|
||||
}
|
||||
|
||||
// Values should be put in a ReadOnlyCollection,
|
||||
// but since this is an internal class, for performance reasons,
|
||||
// we choose to avoid creating yet another collection.
|
||||
|
||||
return values;
|
||||
}
|
||||
}
|
||||
|
||||
public void Add(KeyValuePair<TKey, TValue> item)
|
||||
{
|
||||
Add(item.Key, item.Value);
|
||||
}
|
||||
|
||||
public void Add(TKey key, TValue value)
|
||||
{
|
||||
if (key == null)
|
||||
{
|
||||
throw Fx.Exception.ArgumentNull("key");
|
||||
}
|
||||
|
||||
this.privateDictionary.Add(key, value);
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
this.privateDictionary.Clear();
|
||||
}
|
||||
|
||||
public bool Contains(KeyValuePair<TKey, TValue> item)
|
||||
{
|
||||
if (item.Key == null || !this.privateDictionary.Contains(item.Key))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
return this.privateDictionary[(object)item.Key].Equals(item.Value);
|
||||
}
|
||||
}
|
||||
|
||||
public bool ContainsKey(TKey key)
|
||||
{
|
||||
if (key == null)
|
||||
{
|
||||
throw Fx.Exception.ArgumentNull("key");
|
||||
}
|
||||
|
||||
return this.privateDictionary.Contains(key);
|
||||
}
|
||||
|
||||
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
|
||||
{
|
||||
if (array == null)
|
||||
{
|
||||
throw Fx.Exception.ArgumentNull("array");
|
||||
}
|
||||
|
||||
if (arrayIndex < 0)
|
||||
{
|
||||
throw Fx.Exception.AsError(new ArgumentOutOfRangeException("arrayIndex"));
|
||||
}
|
||||
|
||||
if (array.Rank > 1 || arrayIndex >= array.Length || array.Length - arrayIndex < this.privateDictionary.Count)
|
||||
{
|
||||
throw Fx.Exception.Argument("array", InternalSR.BadCopyToArray);
|
||||
}
|
||||
|
||||
int index = arrayIndex;
|
||||
foreach (DictionaryEntry entry in this.privateDictionary)
|
||||
{
|
||||
array[index] = new KeyValuePair<TKey, TValue>((TKey)entry.Key, (TValue)entry.Value);
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
|
||||
{
|
||||
foreach (DictionaryEntry entry in this.privateDictionary)
|
||||
{
|
||||
yield return new KeyValuePair<TKey, TValue>((TKey)entry.Key, (TValue)entry.Value);
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return GetEnumerator();
|
||||
}
|
||||
|
||||
public bool Remove(KeyValuePair<TKey, TValue> item)
|
||||
{
|
||||
if (Contains(item))
|
||||
{
|
||||
this.privateDictionary.Remove(item.Key);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public bool Remove(TKey key)
|
||||
{
|
||||
if (key == null)
|
||||
{
|
||||
throw Fx.Exception.ArgumentNull("key");
|
||||
}
|
||||
|
||||
if (this.privateDictionary.Contains(key))
|
||||
{
|
||||
this.privateDictionary.Remove(key);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public bool TryGetValue(TKey key, out TValue value)
|
||||
{
|
||||
if (key == null)
|
||||
{
|
||||
throw Fx.Exception.ArgumentNull("key");
|
||||
}
|
||||
|
||||
bool keyExists = this.privateDictionary.Contains(key);
|
||||
value = keyExists ? (TValue)this.privateDictionary[(object)key] : default(TValue);
|
||||
|
||||
return keyExists;
|
||||
}
|
||||
|
||||
void IDictionary.Add(object key, object value)
|
||||
{
|
||||
this.privateDictionary.Add(key, value);
|
||||
}
|
||||
|
||||
void IDictionary.Clear()
|
||||
{
|
||||
this.privateDictionary.Clear();
|
||||
}
|
||||
|
||||
bool IDictionary.Contains(object key)
|
||||
{
|
||||
return this.privateDictionary.Contains(key);
|
||||
}
|
||||
|
||||
IDictionaryEnumerator IDictionary.GetEnumerator()
|
||||
{
|
||||
return this.privateDictionary.GetEnumerator();
|
||||
}
|
||||
|
||||
bool IDictionary.IsFixedSize
|
||||
{
|
||||
get
|
||||
{
|
||||
return ((IDictionary)this.privateDictionary).IsFixedSize;
|
||||
}
|
||||
}
|
||||
|
||||
bool IDictionary.IsReadOnly
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.privateDictionary.IsReadOnly;
|
||||
}
|
||||
}
|
||||
|
||||
ICollection IDictionary.Keys
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.privateDictionary.Keys;
|
||||
}
|
||||
}
|
||||
|
||||
void IDictionary.Remove(object key)
|
||||
{
|
||||
this.privateDictionary.Remove(key);
|
||||
}
|
||||
|
||||
ICollection IDictionary.Values
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.privateDictionary.Values;
|
||||
}
|
||||
}
|
||||
|
||||
object IDictionary.this[object key]
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.privateDictionary[key];
|
||||
}
|
||||
set
|
||||
{
|
||||
this.privateDictionary[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
void ICollection.CopyTo(Array array, int index)
|
||||
{
|
||||
this.privateDictionary.CopyTo(array, index);
|
||||
}
|
||||
|
||||
int ICollection.Count
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.privateDictionary.Count;
|
||||
}
|
||||
}
|
||||
|
||||
bool ICollection.IsSynchronized
|
||||
{
|
||||
get
|
||||
{
|
||||
return ((ICollection)this.privateDictionary).IsSynchronized;
|
||||
}
|
||||
}
|
||||
|
||||
object ICollection.SyncRoot
|
||||
{
|
||||
get
|
||||
{
|
||||
return ((ICollection)this.privateDictionary).SyncRoot;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,61 @@
|
||||
//------------------------------------------------------------
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
//------------------------------------------------------------
|
||||
|
||||
namespace System.Runtime.Collections
|
||||
{
|
||||
using System.Collections.ObjectModel;
|
||||
|
||||
// simple helper class to allow passing in a func that performs validations of
|
||||
// acceptible values
|
||||
class ValidatingCollection<T> : Collection<T>
|
||||
{
|
||||
public ValidatingCollection()
|
||||
{
|
||||
}
|
||||
|
||||
public Action<T> OnAddValidationCallback { get; set; }
|
||||
public Action OnMutateValidationCallback { get; set; }
|
||||
|
||||
void OnAdd(T item)
|
||||
{
|
||||
if (OnAddValidationCallback != null)
|
||||
{
|
||||
OnAddValidationCallback(item);
|
||||
}
|
||||
}
|
||||
|
||||
void OnMutate()
|
||||
{
|
||||
if (OnMutateValidationCallback != null)
|
||||
{
|
||||
OnMutateValidationCallback();
|
||||
}
|
||||
}
|
||||
|
||||
protected override void ClearItems()
|
||||
{
|
||||
OnMutate();
|
||||
base.ClearItems();
|
||||
}
|
||||
|
||||
protected override void InsertItem(int index, T item)
|
||||
{
|
||||
OnAdd(item);
|
||||
base.InsertItem(index, item);
|
||||
}
|
||||
|
||||
protected override void RemoveItem(int index)
|
||||
{
|
||||
OnMutate();
|
||||
base.RemoveItem(index);
|
||||
}
|
||||
|
||||
protected override void SetItem(int index, T item)
|
||||
{
|
||||
OnAdd(item);
|
||||
OnMutate();
|
||||
base.SetItem(index, item);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user