public void StringCacheRemembersItemsUniquely() { StringCache cache = new StringCache(2000000, 10); cache.Add(key0, value0); cache.Add(key1, value1); cache.Add(key2, value2); cache.Add(key3, value3); Assert.AreEqual(value0, cache.Get(key0)); Assert.AreEqual(value1, cache.Get(key1)); Assert.AreEqual(value2, cache.Get(key2)); Assert.AreEqual(value3, cache.Get(key3)); // Test unmapped inputs Assert.IsNull(cache.Get(null)); Assert.IsNull(cache.Get("")); Assert.IsNull(cache.Get("I Do Not Exist as a Key")); // Test that null can be used as a key or as a value cache.Add(null, "I'm with null"); cache.Add("I have a null value", null); Assert.AreEqual("I'm with null", cache.Get(null)); Assert.IsNull(cache.Get("I have a null value")); // Test that null can be used as a key and as a value cache.Add(null, null); Assert.IsNull(cache.Get(null)); // Test that adding a key after failing to retrieve it causes correct behavior cache.Add("", "I am a new value"); Assert.AreEqual("I am a new value", cache.Get("")); }
/// <summary> /// Insert a ingle row /// </summary> /// <typeparam name="T"></typeparam> /// <param name="connection"></param> /// <param name="param">object</param> /// <param name="table">Optional table name</param> /// <param name="commandTimeout">commandTimeout</param> /// <param name="transaction">transaction</param> /// <returns>Numbers of rows affected</returns> public static int InsertSingle <T>(this IDbConnection connection, T param, int?commandTimeout = null, IDbTransaction transaction = null) { if (param == null) { throw new ArgumentNullException("param can not be null."); } if (param is IEnumerable) { throw new ArgumentException("param can not be a IEnumerable. Call InsertMany instead."); } var type = typeof(T); string cachedCommand; var value = StringCache.TryGetCommand(type, Operation.Insert, out cachedCommand); if (string.IsNullOrEmpty(cachedCommand)) { cachedCommand = InsertGenerator.GenerateSingle(param); StringCache.Add(type, Operation.Insert, cachedCommand); } return(connection.Execute(cachedCommand, param, commandTimeout: commandTimeout, transaction: transaction)); }
public void Adds_and_retrieves_short_content_safely() { const string input = "This will not compress"; var cache = new StringCache(); var added = cache.Add("short", input); Assert.IsTrue(added); var retrieved = cache.Get("short"); Assert.AreEqual(input, retrieved); }
public void StringCacheEnforcesAgeLimit() { // Test correct normal behavior with large limit StringCache cache = new StringCache(2000000, 10); cache.Add(key0, value0); Assert.AreEqual(value0, cache.Get(key0)); // Test that an expiration time of 0 minutes immediately expires everything. cache = new StringCache(2000000, 0); cache.Add(key0, value0); Assert.IsNull(cache.Get(key0)); cache.Add(key1, value1); Assert.IsNull(cache.Get(key1)); cache.Add(key2, value2); cache.Add(key3, value3); Assert.IsNull(cache.Get(key2)); Assert.IsNull(cache.Get(key3)); }
public void AddCommand() { var type = typeof(Foo); StringCache.Add(type, Operation.Insert, "insert 1"); StringCache.Add(type, Operation.Insert, "insert 2"); string cachedCommand; var value = StringCache.TryGetCommand(type, Operation.Insert, out cachedCommand); Assert.AreEqual(true, value); Assert.AreEqual("insert 1", cachedCommand); }
public void UpdateText(string text) { if (text != Text) { Text = text; StringCache.Clear(); for (int i = 0; i < Text.Length; i++) { //get teh size of the character StringCache.Add(text[i].ToString()); } } }
public void Adds_and_retrieves_long_content_safely() { var sb = new StringBuilder(); for(var i = 0; i < 1000; i++) { sb.Append("I will not write loops in unit tests."); } var input = sb.ToString(); var cache = new StringCache(); var added = cache.Add("long", input); Assert.IsTrue(added); var retrieved = cache.Get("long"); Assert.AreEqual(input, retrieved); }
public void StringCacheEnforcesSizeLimit() { // Test correct normal behavior with large limit StringCache cache = new StringCache(2000000, 10); cache.Add(key2, value2); Assert.AreEqual(value2, cache.Get(key2)); // Edge case: cache size limit is exactly the calculated size of the cached item (requires knowledge of the implementation details of the cache). // In this case, we are using a reference type for key and value, which means 3 references will be counted. // We also have one reference for the CacheItem used to wrap around the Value, and 8 bytes for the DateTime used to calculate its age. int overhead = (ObjectSize.ReferenceSize * 4) + 8; int requiredSize = overhead + (int)ObjectSize.SizeOf(key2) + (int)ObjectSize.SizeOf(value2); cache = new StringCache(requiredSize, 10); cache.Add(key2, value2); Assert.AreEqual(value2, cache.Get(key2)); // Edge case: cache size limit one byte smaller than required cache = new StringCache(requiredSize - 1, 10); cache.Add(key2, value2); Assert.IsNull(cache.Get(key2)); // Value should fail to load from the cache, because it should have been removed for being over the limit. cache.Add(key0, value0); // Adding a shorter value should work Assert.AreEqual(value0, cache.Get(key0)); cache.Add(key0, value0); // Adding the same key again with the same value should change nothing Assert.AreEqual(value0, cache.Get(key0)); cache.Add(key0, value1); // Adding the same key again with a DIFFERENT but equal length value should work Assert.AreEqual(value1, cache.Get(key0)); cache.Add(key2, value2); // Adding the oversized value again should cause the cache to be empty. Assert.IsNull(cache.Get(key0)); // Value should fail to load from the cache, because it should have been removed for being over the limit. Assert.IsNull(cache.Get(key2)); // Value should fail to load from the cache, because it should have been removed for being over the limit. }
private static async Task DownloadBingDailyImage(string saveDir) { var jsonHttpClient = BuildJsonHttpClient(); var metadataTask = jsonHttpClient.GetStringAsync($"{BaseUrl}/{ImageMetadataUrl}"); var metadataStr = await metadataTask; var metadata = JsonSerializer.Deserialize <RootObject>(metadataStr, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); var imageHttpClient = BuildImageHttpClient(); if (metadata.Images.Length == 0) { return; } var cache = new StringCache(Path.Combine(CurrentExeDirectory, "cache.txt")); cache.Load(); try { foreach (var metadataImage in metadata.Images) { var uri = new Uri(BaseUrl + metadataImage.Url); var queryDictionary = HttpUtility.ParseQueryString(uri.Query); var id = queryDictionary["id"]; if (cache.Contains(id)) { continue; } var imgStream = await imageHttpClient.GetStreamAsync(uri); var directoryInfo = Directory.CreateDirectory(saveDir); await using var fileStream = File.Create(Path.Combine(directoryInfo.FullName, id)); await imgStream.CopyToAsync(fileStream); cache.Add(id); } } finally { cache.Flush(); } }
public void Adds_and_retrieves_long_content_safely() { var sb = new StringBuilder(); for (var i = 0; i < 1000; i++) { sb.Append("I will not write loops in unit tests."); } var input = sb.ToString(); var cache = new StringCache(); var added = cache.Add("long", input); Assert.IsTrue(added); var retrieved = cache.Get("long"); Assert.AreEqual(input, retrieved); }