public void TestTryGetValue() { // Create the cache and register our test types EveCache cache = new EveCache(new MemoryCache("Eve.Tests")); // Verify the cache is initially empty Assert.AreEqual(((IEnumerable <KeyValuePair <string, object> >)cache.InnerCache).Count(), 0); // Verify no item with ID 5 is in the cache TestItem result; bool success = cache.TryGetValue(5, out result); Assert.IsFalse(success); Assert.IsNull(result); Assert.AreEqual(cache.Statistics.Hits, 0); Assert.AreEqual(cache.Statistics.Misses, 1); // Add a test item to the cache TestItem item = new TestItem(5); TestItem returnedItem = cache.GetOrAdd(item); Assert.AreEqual(cache.Statistics.Hits, 0); Assert.AreEqual(cache.Statistics.Misses, 2); // Verify the test item is returned success = cache.TryGetValue(5, out result); Assert.IsTrue(success); Assert.AreEqual(result, item); Assert.AreEqual(cache.Statistics.Hits, 1); Assert.AreEqual(cache.Statistics.Misses, 2); }
public void TestClear() { // Create the cache and register our test types EveCache cache = new EveCache(new MemoryCache("Eve.Tests")); TestItem[] items = new TestItem[10]; TestChildItem[] childItems = new TestChildItem[10]; TestStringItem[] stringItems = new TestStringItem[10]; // Populate with initial items for (int i = 0; i < 10; i++) { items[i] = new TestItem(i); childItems[i] = new TestChildItem(i + 100); // Add 100 to avoid ID collisions stringItems[i] = new TestStringItem("Test" + i.ToString()); cache.GetOrAdd(items[i]); cache.GetOrAdd(childItems[i]); cache.GetOrAdd(stringItems[i]); } // Verify that all test items have been added Assert.AreEqual(((IEnumerable <KeyValuePair <string, object> >)cache.InnerCache).Count(), 30); Assert.AreEqual(cache.Statistics.Writes, 30); // Clean the cache cache.Clear(); // Verify that the objects have been removed Assert.AreEqual(((IEnumerable <KeyValuePair <string, object> >)cache.InnerCache).Count(), 0); }
public async Task SetCache <TResult>(string key, string content, TResult result) { var data = result as IKeyedModel; if (data == null) { return; } data.Key = key; await EveCache.SetCache(data); }
public void TestGetOrAddValueFactoryWithInvalidKey() { // Create the cache and register our test types EveCache cache = new EveCache(new MemoryCache("Eve.Tests")); // Verify the cache is initially empty Assert.AreEqual(((IEnumerable <KeyValuePair <string, object> >)cache.InnerCache).Count(), 0); // Add a test item to the cache with an incorrect key TestStringItem item = new TestStringItem("Real Key"); Assert.Throws(typeof(InvalidOperationException), () => cache.GetOrAdd("Requested Key", () => item)); }
public void TestClearPermanent() { // Create the cache and register our test types EveCache cache = new EveCache(new MemoryCache("Eve.Tests")); TestItem[] permanentItems = { new TestItem(1), new TestItem(2), new TestItem(3), new TestItem(4) }; TestStringItem[] transientItems = { new TestStringItem("First"), new TestStringItem("Second"), new TestStringItem("Third") }; // First add the permanent items foreach (TestItem permanentItem in permanentItems) { cache.AddOrReplace(permanentItem, true); } // Then add the transient items foreach (TestStringItem transientItem in transientItems) { cache.AddOrReplace(transientItem, false); } // The inner cache should only contain the non-permanent items Assert.AreEqual(((IEnumerable <KeyValuePair <string, object> >)cache.InnerCache).Count(), transientItems.Length); Assert.That(((IEnumerable <KeyValuePair <string, object> >)cache.InnerCache).All(x => transientItems.Contains(x.Value))); // Now clear the cache. All transient items should be removed. cache.Clear(); // Verify that all transient items were removed and the inner cache is empty Assert.AreEqual(cache.InnerCache.Count(), 0); // Verify that all permanent items remain Assert.That(permanentItems.All(x => { TestItem result; if (!cache.TryGetValue <TestItem>(x.CacheKey, out result)) { return(false); } if (!object.Equals(x, result)) { return(false); } return(true); })); }
public void TestClearNonPrefix() { // Create the cache and register our test types EveCache cache = new EveCache(new MemoryCache("Eve.Tests")); TestItem[] items = new TestItem[10]; TestChildItem[] childItems = new TestChildItem[10]; TestStringItem[] stringItems = new TestStringItem[10]; // Populate with initial items for (int i = 0; i < 10; i++) { items[i] = new TestItem(i); childItems[i] = new TestChildItem(i + 100); // Add 100 to avoid ID collisions stringItems[i] = new TestStringItem("Test" + i.ToString()); cache.GetOrAdd(items[i]); cache.GetOrAdd(childItems[i]); cache.GetOrAdd(stringItems[i]); } Assert.AreEqual(((IEnumerable <KeyValuePair <string, object> >)cache.InnerCache).Count(), 30); Assert.AreEqual(cache.Statistics.Writes, 30); cache.InnerCache.Add("NON-EveCacheRegionPrefix-1", "Test1", new CacheItemPolicy()); cache.InnerCache.Add("NON-EveCacheRegionPrefix-2", "Test2", new CacheItemPolicy()); Assert.AreEqual(((IEnumerable <KeyValuePair <string, object> >)cache.InnerCache).Count(), 32); // Verify that all test items have been added // Clean the cache cache.Clear(); // Verify that the objects with the EVE prefix were removed but the others were not affected Assert.AreEqual(((IEnumerable <KeyValuePair <string, object> >)cache.InnerCache).Count(), 2); Assert.That(cache.InnerCache.Contains("NON-EveCacheRegionPrefix-1")); Assert.That(cache.InnerCache.Contains("NON-EveCacheRegionPrefix-2")); }
public void TestAddOrReplace() { // Create the cache and register our test types EveCache cache = new EveCache(new MemoryCache("Eve.Tests")); // Verify the cache is initially empty Assert.AreEqual(((IEnumerable <KeyValuePair <string, object> >)cache.InnerCache).Count(), 0); // Add a test item to the cache TestItem item = new TestItem(5); TestItem returnedItem = cache.GetOrAdd(item); Assert.AreEqual(cache.Statistics.Hits, 0); Assert.AreEqual(cache.Statistics.CacheHits, 0); Assert.AreEqual(cache.Statistics.ReferenceHits, 0); Assert.AreEqual(cache.Statistics.Misses, 1); // Verify the method returns the correct value Assert.That(item == returnedItem); Assert.AreEqual(cache.Statistics.Writes, 1); // Verify that the item was added to the cache Assert.AreEqual(((IEnumerable <KeyValuePair <string, object> >)cache.InnerCache).Count(), 1); // Add a new item with the same ID TestItem newItem = new TestItem(5); cache.AddOrReplace(newItem); // Retrieve the newly-added item and make sure the cache returns // the new value cache.TryGetValue(5, out item); Assert.That(item == newItem); Assert.AreEqual(cache.Statistics.Writes, 2); // Verify that the cache count remains the same Assert.AreEqual(((IEnumerable <KeyValuePair <string, object> >)cache.InnerCache).Count(), 1); }
public void TestGetOrAddValueFactory() { // Create the cache and register our test types EveCache cache = new EveCache(new MemoryCache("Eve.Tests")); // Verify the cache is initially empty Assert.AreEqual(((IEnumerable <KeyValuePair <string, object> >)cache.InnerCache).Count(), 0); // Add a test item to the cache TestItem item = new TestItem(5); TestItem returnedItem = cache.GetOrAdd(item.Id, () => item); Assert.AreEqual(cache.Statistics.Hits, 0); Assert.AreEqual(cache.Statistics.Misses, 1); // Verify the method returns the correct value Assert.That(item == returnedItem); Assert.AreEqual(cache.Statistics.Writes, 1); // Verify that the item was added to the cache Assert.AreEqual(((IEnumerable <KeyValuePair <string, object> >)cache.InnerCache).Count(), 1); // Add a new item with the same ID TestItem newItem = new TestItem(5); returnedItem = cache.GetOrAdd(newItem.Id, () => newItem); Assert.AreEqual(cache.Statistics.Hits, 1); Assert.AreEqual(cache.Statistics.Misses, 1); // Verify the method returns the original, cached value, not the new one Assert.That(item == returnedItem); Assert.AreEqual(cache.Statistics.Writes, 1); // Verify that the cache count remains the same Assert.AreEqual(((IEnumerable <KeyValuePair <string, object> >)cache.InnerCache).Count(), 1); }
public void TestGetOrAddReferenceTracker() { // Create the cache and register our test types EveCache cache = new EveCache(new MemoryCache("Eve.Tests")); // Verify the cache is initially empty Assert.AreEqual(((IEnumerable <KeyValuePair <string, object> >)cache.InnerCache).Count(), 0); // Add a test item to the cache TestStringItem item = new TestStringItem("Real Key"); cache.AddOrReplace(item); // Retrieve the item and verify it was retrieved from the reference tracker long cacheCountBefore = cache.Statistics.CacheHits; long referenceCountBefore = cache.Statistics.ReferenceHits; cache.GetOrAdd(item.CacheKey, () => item); long cacheCountAfter = cache.Statistics.CacheHits; long referenceCountAfter = cache.Statistics.ReferenceHits; Assert.That(cacheCountAfter == cacheCountBefore); Assert.That(referenceCountAfter == referenceCountBefore + 1); }
/* Methods */ /// <summary> /// Computes a compound ID for the specified sub-IDs. /// </summary> /// <param name="assemblyLineTypeId"> /// The ID of the assembly line type. /// </param> /// <param name="groupId"> /// The ID of the group. /// </param> /// <returns> /// A compound ID combining the two sub-IDs. /// </returns> public static IConvertible CreateCacheKey(byte assemblyLineTypeId, GroupId groupId) { return(EveCache.CreateCompoundCacheKey(assemblyLineTypeId, groupId)); }
/// <summary> /// Computes a compound ID for the specified sub-IDs. /// </summary> /// <param name="itemTypeId"> /// The item type ID. /// </param> /// <param name="attributeId"> /// The attribute ID. /// </param> /// <returns> /// A compound ID combining the two sub-IDs. /// </returns> internal static IConvertible CreateCacheKey(int itemTypeId, AttributeId attributeId) { return(EveCache.CreateCompoundCacheKey(itemTypeId, attributeId)); }
/* Methods */ /// <summary> /// Computes a compound ID for the specified sub-IDs. /// </summary> /// <param name="itemTypeId"> /// The item type ID. /// </param> /// <param name="effectId"> /// The effect ID. /// </param> /// <returns> /// A compound ID combining the two sub-IDs. /// </returns> public static IConvertible CreateCacheKey(int itemTypeId, EffectId effectId) { return(EveCache.CreateCompoundCacheKey(itemTypeId, effectId)); }
/* Methods */ /// <summary> /// Computes a compound ID for the specified sub-IDs. /// </summary> /// <param name="typeId"> /// The ID of the type. /// </param> /// <param name="materialTypeId"> /// The ID of the material type. /// </param> /// <returns> /// A compound ID combining the two sub-IDs. /// </returns> public static IConvertible CreateCacheKey(int typeId, int materialTypeId) { return(EveCache.CreateCompoundCacheKey(typeId, materialTypeId)); }
/// <summary> /// Computes a compound ID for the specified sub-IDs. /// </summary> /// <param name="factionId"> /// The faction ID. /// </param> /// <param name="typeId"> /// The type ID. /// </param> /// <returns> /// A compound ID combining the two sub-IDs. /// </returns> internal static IConvertible CreateCacheKey(long factionId, EveTypeId typeId) { return(EveCache.CreateCompoundCacheKey(factionId, typeId)); }
/* Methods */ /// <summary> /// Computes a compound ID for the specified sub-IDs. /// </summary> /// <param name="typeId"> /// The ID of the type. /// </param> /// <param name="activityId"> /// The ID of the activity. /// </param> /// <param name="requiredTypeId"> /// The ID of the required type. /// </param> /// <returns> /// A compound ID combining the two sub-IDs. /// </returns> public static IConvertible CreateCacheKey(int typeId, ActivityId activityId, int requiredTypeId) { return(EveCache.CreateCompoundCacheKey(typeId, activityId, requiredTypeId)); }
/* Methods */ /// <summary> /// Computes a compound ID for the specified sub-IDs. /// </summary> /// <param name="corporationId"> /// The ID of the corporation. /// </param> /// <param name="divisionId"> /// The ID of the division. /// </param> /// <returns> /// A compound ID combining the two sub-IDs. /// </returns> public static IConvertible CreateCacheKey(long corporationId, DivisionId divisionId) { return(EveCache.CreateCompoundCacheKey(corporationId, divisionId)); }
public Task <object> GetCache <TResult>(string key) { return(Task.FromResult <object>(EveCache.GetCache <T>(key))); }
public void TestRemove() { // Create the cache and register our test types EveCache cache = new EveCache(new MemoryCache("Eve.Tests")); TestItem[] items = new TestItem[10]; TestChildItem[] childItems = new TestChildItem[10]; TestStringItem[] stringItems = new TestStringItem[10]; // Populate with initial items for (int i = 0; i < 10; i++) { items[i] = new TestItem(i); childItems[i] = new TestChildItem(i + 100); // Add 100 to avoid ID collisions stringItems[i] = new TestStringItem("Test" + i.ToString()); cache.GetOrAdd(items[i]); cache.GetOrAdd(childItems[i]); cache.GetOrAdd(stringItems[i]); } // Verify that all test items have been added Assert.AreEqual(((IEnumerable <KeyValuePair <string, object> >)cache.InnerCache).Count(), 30); // Remove the first 10 items foreach (TestItem test1 in items) { var result = cache.Remove <TestItem>(test1.Id); // Verify the return value Assert.AreEqual(result, test1); } // Verify that the objects have been removed Assert.AreEqual(((IEnumerable <KeyValuePair <string, object> >)cache.InnerCache).Count(), 20); // Try to remove a non-existent item var value = cache.Remove <TestItem>("Nonexistent"); Assert.IsNull(value); // Remove the next 10 items foreach (TestChildItem test2 in childItems) { var result = cache.Remove <TestChildItem>(test2.Id); // Verify the return value Assert.AreEqual(result, test2); } // Verify that the objects have been removed Assert.AreEqual(((IEnumerable <KeyValuePair <string, object> >)cache.InnerCache).Count(), 10); // Remove the last 10 items foreach (TestStringItem test3 in stringItems) { var result = cache.Remove <TestStringItem>(test3.Id); // Verify the return value Assert.AreEqual(result, test3); } // Verify that the objects have been removed Assert.AreEqual(((IEnumerable <KeyValuePair <string, object> >)cache.InnerCache).Count(), 0); }
/* Methods */ /// <summary> /// Computes a compound ID for the specified sub-IDs. /// </summary> /// <param name="fromSolarSystemId"> /// The ID of the origin constellation. /// </param> /// <param name="toSolarSystemId"> /// The ID of the destination constellation. /// </param> /// <returns> /// A compound ID combining the two sub-IDs. /// </returns> public static IConvertible CreateCacheKey(SolarSystemId fromSolarSystemId, SolarSystemId toSolarSystemId) { return(EveCache.CreateCompoundCacheKey(fromSolarSystemId, toSolarSystemId)); }
/* Methods */ /// <summary> /// Computes a compound ID for the specified sub-IDs. /// </summary> /// <param name="fromRegionId"> /// The ID of the origin region. /// </param> /// <param name="toRegionId"> /// The ID of the destination region. /// </param> /// <returns> /// A compound ID combining the two sub-IDs. /// </returns> public static IConvertible CreateCacheKey(long fromRegionId, long toRegionId) { return(EveCache.CreateCompoundCacheKey(fromRegionId, toRegionId)); }
/* Methods */ /// <summary> /// Computes a compound ID for the specified sub-IDs. /// </summary> /// <param name="fromConstellationId"> /// The ID of the origin constellation. /// </param> /// <param name="toConstellationId"> /// The ID of the destination constellation. /// </param> /// <returns> /// A compound ID combining the two sub-IDs. /// </returns> public static IConvertible CreateCacheKey(ConstellationId fromConstellationId, ConstellationId toConstellationId) { return(EveCache.CreateCompoundCacheKey(fromConstellationId, toConstellationId)); }
/* Methods */ /// <summary> /// Computes a compound ID for the specified sub-IDs. /// </summary> /// <param name="reactionTypeId"> /// The ID of the reaction type. /// </param> /// <param name="input"> /// Specifies whether the type is required by or produced by the reaction. /// </param> /// <param name="typeId"> /// The ID of the type required or produced by the reaction. /// </param> /// <returns> /// A compound ID combining the sub-IDs. /// </returns> public static IConvertible CreateCacheKey(int reactionTypeId, bool input, int typeId) { return(EveCache.CreateCompoundCacheKey(reactionTypeId, input, typeId)); }
/* Methods */ /// <summary> /// Computes a compound ID for the specified sub-IDs. /// </summary> /// <param name="stationId"> /// The ID of the station. /// </param> /// <param name="assemblyLineTypeId"> /// The ID of the assembly line type. /// </param> /// <returns> /// A compound ID combining the two sub-IDs. /// </returns> public static IConvertible CreateCacheKey(long stationId, byte assemblyLineTypeId) { return(EveCache.CreateCompoundCacheKey(stationId, assemblyLineTypeId)); }
/* Methods */ /// <summary> /// Computes a compound ID for the specified sub-IDs. /// </summary> /// <param name="controlTowerTypeId"> /// The ID of the control tower type. /// </param> /// <param name="resourceTypeId"> /// The ID of the resource type. /// </param> /// <returns> /// A compound ID combining the two sub-IDs. /// </returns> public static IConvertible CreateCacheKey(int controlTowerTypeId, int resourceTypeId) { return(EveCache.CreateCompoundCacheKey(controlTowerTypeId, resourceTypeId)); }
/* Methods */ /// <summary> /// Computes a compound ID for the specified sub-IDs. /// </summary> /// <param name="assemblyLineTypeId"> /// The ID of the assembly line type. /// </param> /// <param name="categoryId"> /// The ID of the category. /// </param> /// <returns> /// A compound ID combining the two sub-IDs. /// </returns> public static IConvertible CreateCacheKey(byte assemblyLineTypeId, CategoryId categoryId) { return(EveCache.CreateCompoundCacheKey(assemblyLineTypeId, categoryId)); }