/// <summary> /// Ensures each post in the collection is in the database /// </summary> /// <returns>The original values, if any</returns> public IEnumerable <T> Upsert(IEnumerable <T> things) { var updatedThings = new List <T>(); foreach (T thing in things) { // If an object in the cache matches fully, do nothing and return if (_cache.Contains(thing, new CacheAllPropertiesComparer <T>())) { return(updatedThings); } // If an object in the cache matches by the default comparer, // then this upsert is updating something so remove it // and capture the updated object if (_cache.Contains(thing)) { updatedThings.Add(GetById(thing.Id)); _cache.Remove(thing); } // Add and upsert the item _cache.Add(thing); UpsertIntoCollection(thing); } return(updatedThings); }
public void AddWorksAsExpectedBeforeReachingCapacity() { DatabaseCache <int> cache = new DatabaseCache <int>(10); for (int i = 0; i < 10; ++i) { Assert.IsFalse(cache.Contains(i)); cache.Add(i); Assert.IsTrue(cache.Contains(i)); } }
public void AddWorksAsExpectedBeforeReachingCapacity() { DatabaseCache <RedditThing> cache = new DatabaseCache <RedditThing>(10); for (int i = 0; i < 10; ++i) { var item = new IntThing { Id = i.ToString(), Index = i }; Assert.IsFalse(cache.Contains(item)); cache.Add(item); Assert.IsTrue(cache.Contains(item)); } }
public void ObjectEqualityOnDatabaseObjectsWorksAsExpected() { using (DatabaseCache <RedditComment> cache = new DatabaseCache <RedditComment>(5)) { for (int i = 0; i < 5; ++i) { RedditComment comment = new RedditComment() { Body = $"info: {i}" }; Assert.IsFalse(cache.Contains(comment)); cache.Add(comment); Assert.IsTrue(cache.Contains(comment)); } Assert.IsTrue(cache.Contains(new RedditComment() { Body = $"info: 0" })); } using (DatabaseCache <RedditPost> postCache = new DatabaseCache <RedditPost>(15)) { for (int i = 0; i < 20; ++i) { RedditPost post = new RedditPost() { Title = $"info: {i}" }; Assert.IsFalse(postCache.Contains(post)); postCache.Add(post); Assert.IsTrue(postCache.Contains(post)); } Assert.IsTrue(postCache.Contains(new RedditPost() { Title = $"info: 10" })); Assert.IsFalse(postCache.Contains(new RedditPost() { Title = $"info: 0" })); } }
public void ObjectEqualityOnDatabaseObjectsWorksAsExpected() { using (DatabaseCache <Comment> cache = new DatabaseCache <Comment>(5)) { for (int i = 0; i < 5; ++i) { Comment comment = new Comment() { Id = i.ToString(), Body = $"info: {i}" }; Assert.IsFalse(cache.Contains(comment)); cache.Add(comment); Assert.IsTrue(cache.Contains(comment)); } Assert.IsTrue(cache.Contains(new Comment() { Id = "0", Body = $"info: 0" })); } using (DatabaseCache <Post> postCache = new DatabaseCache <Post>(15)) { for (int i = 0; i < 20; ++i) { Post post = new Post() { Id = i.ToString(), Title = $"info: {i}" }; Assert.IsFalse(postCache.Contains(post)); postCache.Add(post); Assert.IsTrue(postCache.Contains(post)); } Assert.IsTrue(postCache.Contains(new Post() { Id = "10", Title = $"info: 10" })); Assert.IsFalse(postCache.Contains(new Post() { Id = "0", Title = $"info: 0" })); } }
public void DatabaseCacheWorksWithDatabaseObjects() { DatabaseCache <RedditComment> cache = new DatabaseCache <RedditComment>(5); for (int i = 0; i < 10; ++i) { RedditComment comment = new RedditComment() { Body = $"info: {i}" }; Assert.IsFalse(cache.Contains(comment)); cache.Add(comment); Assert.IsTrue(cache.Contains(comment)); } Assert.IsFalse(cache.Contains(new RedditComment() { Body = $"info: 0" })); }
public void DatabaseCacheWorksWithDatabaseObjects() { DatabaseCache <Comment> cache = new DatabaseCache <Comment>(5); for (int i = 0; i < 10; ++i) { Comment comment = new Comment() { Id = i.ToString(), Body = $"info: {i}" }; Assert.IsFalse(cache.Contains(comment)); cache.Add(comment); Assert.IsTrue(cache.Contains(comment)); } Assert.IsFalse(cache.Contains(new Comment() { Id = "777", Body = $"info: 0" })); }
public void AddWorksAsExpectedAfterReachingCapacity() { DatabaseCache <RedditThing> cache = new DatabaseCache <RedditThing>(5); for (int i = 0; i < 10; ++i) { var item = new IntThing { Id = i.ToString(), Index = i }; Assert.IsFalse(cache.Contains(item)); cache.Add(item); Assert.IsTrue(cache.Contains(item)); } var newItem = new IntThing { Id = "777", Index = int.MaxValue }; Assert.IsFalse(cache.Contains(newItem)); }
/// <summary> /// Ensures each comment in the collection is in the database /// </summary> public void EnsureCommentsInDatabase(IEnumerable <RedditComment> comments) { foreach (RedditComment comment in comments) { if (commentCache.Contains(comment)) { continue; } commentCache.Add(comment); UpsertIntoCollection(comment); Console.WriteLine($"Wrote comment id {comment.Id} by {comment.Author} to database"); } }
public void EnsurePostsInDatabase(IEnumerable <RedditPost> posts) { foreach (RedditPost post in posts) { if (postCache.Contains(post)) { continue; } postCache.Add(post); UpsertIntoCollection(post); Console.WriteLine($"Wrote post id {post.Id} by {post.Author} to database"); } }
public void DatabaseCacheAllAroundTest() { _testName = MethodInfo.GetCurrentMethod().Name.GetHashCode().ToString(); Cleanup(); var seed = new Seed32(); var cleanEntities = TestResourceFactory.GetMockClassAObjects(500); var dirtyEntities = TestResourceFactory.GetMockClassAObjects(500); cleanEntities.ToList().ForEach(c => c.Id = seed.Increment()); dirtyEntities.ToList().ForEach(d => d.Id = seed.Increment()); var db = new DatabaseCache<int, MockClassA>(false, 2048, new BinConverter32()); db.CacheItem(cleanEntities.First().Id); //with manual caching foreach (var e in cleanEntities) db.UpdateCache(e.Id, e, false, false); Assert.AreEqual(1, db.Count); Assert.IsTrue(db.Contains(cleanEntities.First().Id)); Assert.AreEqual(cleanEntities.First().Name, db.GetFromCache(cleanEntities.First().Id).Name); //with autoCache foreach (var e in cleanEntities) db.UpdateCache(e.Id, e, true, false); Assert.AreEqual(500, db.Count); db.Detach(cleanEntities.First().Id); Assert.AreEqual(499, db.Count); Assert.IsFalse(db.Contains(cleanEntities.First().Id)); Assert.IsNull(db.GetFromCache(cleanEntities.First().Id)); db.CacheSize = 400; db.UpdateCache(cleanEntities.First().Id, cleanEntities.First(), true, false); Assert.AreEqual(201, db.Count); Assert.IsFalse(db.IsNew(cleanEntities.First().Id)); db.ClearCache(); Assert.AreEqual(0, db.Count); }