/// <summary> /// Gets the <see cref="CommandCacheItem"/> from the cache. /// Returns <c>null</c> if the corresponding item does not exist. /// </summary> /// <param name="identity">The identity for retrieving the cache.</param> /// <returns>The <see cref="CommandCacheItem"/> or <c>null</c>.</returns> public static CommandCacheItem Get(CommandIdentity identity) { try { Lock.EnterReadLock(); CommandCacheItem cache; // we suppose in more situations G1 is enough for use, // so search from G1 before G2 will be more quickly if (!Generation1.TryGetValue(identity, out cache)) { if (!_generation2Enabled) { return(null); } if (!_generation2.TryGetValue(identity, out cache)) { return(null); } } Interlocked.Increment(ref cache.Reused); return(cache); } finally { Lock.ExitReadLock(); } }
/// <summary> /// Sets the <see cref="CommandCacheItem"/> in the cache. /// </summary> /// <param name="identity">The identity for retrieving the cache.</param> /// <param name="cacheItem">The <see cref="CommandCacheItem"/> to set.</param> public static void Set(CommandIdentity identity, CommandCacheItem cacheItem) { try { Lock.EnterWriteLock(); if (Generation1.Count >= Generation1Threshold) { CollectGeneration1(); } Generation1[identity] = cacheItem; } finally { Lock.ExitWriteLock(); } }