public async Task Get_when_cache_state_is_invalid_does_not_call_deserialize(CacheEntryState invalidEntityState) { var cancellationToken = CancellationToken.None; var testCacheModel = new TestCacheModel(); var cachedByteResults = new byte[32]; Array.Fill <byte>(cachedByteResults, 255); distributedCacheMock .Setup(distributedCache => distributedCache.GetAsync(nameof(TestCacheModel), cancellationToken)) .Returns(Task.FromResult(cachedByteResults)).Verifiable(); messagePackServiceMock.Setup(messagePackService => messagePackService .Deserialise <TestCacheModel>(cachedByteResults, sut._messagePackOptions)) .Returns(Task.FromResult(testCacheModel)) .Verifiable(); cacheEntryTrackerMock.Setup(cacheEntryTrackerMock => cacheEntryTrackerMock .GetState(It.IsAny <string>(), cancellationToken)) .Returns(Task.FromResult(invalidEntityState)); var result = await sut.Get <TestCacheModel>(nameof(TestCacheModel), cancellationToken); distributedCacheMock.Verify(distributedCache => distributedCache .GetAsync(nameof(TestCacheModel), cancellationToken), Times.Once); messagePackServiceMock.Verify(messagePackService => messagePackService .Deserialise <TestCacheModel>(cachedByteResults, sut._messagePackOptions), Times.Never); }
public void UpdateCacheItemTest() { _pool.Register <TestTable>(); CacheIndex index = new CacheIndex() { IndexName = "主键测试", IndexType = IndexType.唯一索引, GetIndexKey = GetKey }; _pool.Get <TestTable>().AddIndex(index); List <ICacheIndex> indexs = _pool.Get <TestTable>().GetAllIndexs(); TestCacheModel model = new TestCacheModel() { ID = 1, Name = "测试" }; _pool.Get <TestTable>().Add(model); TestCacheModel updateModel = new TestCacheModel() { ID = 1, Name = "测试1" }; _pool.Get <TestTable>().Update(updateModel); List <ICacheItem> item = _pool.Get <TestTable>().Get(model.ID.ToString()); TestCacheModel queryRlt = item[0] as TestCacheModel; Assert.AreEqual(updateModel.Name, queryRlt.Name); }
public async Task Set_when_parameter_null_does_not_call_Serialize() { var cancellationToken = CancellationToken.None; TestCacheModel testCacheModel = default; var cachedByteResults = new byte[32]; Array.Fill <byte>(cachedByteResults, 255); distributedCacheMock .Setup(distributedCache => distributedCache.SetAsync(nameof(TestCacheModel), cachedByteResults, It.IsAny <DistributedCacheEntryOptions>(), cancellationToken)) .Returns(Task.CompletedTask) .Verifiable(); messagePackServiceMock.Setup(messagePackService => messagePackService .Serialise(testCacheModel, sut._messagePackOptions)) .Returns(Task.FromResult(cachedByteResults.AsEnumerable())) .Verifiable(); await sut.Set(nameof(TestCacheModel), testCacheModel, cancellationToken); distributedCacheMock.Verify(distributedCache => distributedCache.SetAsync(nameof(TestCacheModel), cachedByteResults, It.IsAny <DistributedCacheEntryOptions>(), cancellationToken), Times.Never); messagePackServiceMock.Verify(messagePackService => messagePackService .Serialise(testCacheModel, sut._messagePackOptions), Times.Never); }
/// <summary> /// 复制 /// </summary> /// <param name="des"></param> public override void Copy(ICacheItem des) { base.Copy(des); TestCacheModel model = des as TestCacheModel; model.ID = this.ID; model.Name = this.Name; }
private void SaveTestResult(TestCacheModel currentTest, string userId, double result) { Score score = new Score() { UserId = userId, TestId = currentTest.Id, Result = result }; this.scores.Add(score); }
public ActionResult Solve(int selectedAnswerId, int testId, int question) { string currentUserId = User.Identity.GetUserId(); string currentTestCacheKey = currentUserId + testId; TestCacheModel currentTest = (TestCacheModel)this.HttpContext.Cache[currentTestCacheKey]; if (currentTest == null) { return(RedirectToAction("Logoff", "Account")); } QuestionCacheModel currentQuestion = currentTest.Questions[question]; if (currentQuestion.CorrectAnswerId == selectedAnswerId) { currentQuestion.Guessed = true; } else { currentQuestion.Guessed = false; } if (question == currentTest.Questions.Count - 1) { double result = CalculateTestResult(currentTest); SaveTestResult(currentTest, currentUserId, result); this.HttpContext.Cache.Remove(currentTestCacheKey); return(RedirectToAction("Index", "Tests")); } this.HttpContext.Cache.Insert( currentTestCacheKey, currentTest, null, DateTime.Now.AddDays(1), TimeSpan.Zero, CacheItemPriority.Default, null); currentTest.QuestionIndex = question + 1; return(RedirectToAction("Solve", new RouteValueDictionary(new { testId = testId, question = currentTest.QuestionIndex }))); }
public ActionResult Solve(int testId, int question) { string currentUserId = User.Identity.GetUserId(); string currentTestCacheKey = currentUserId + testId; TestCacheModel currentTest = (TestCacheModel)this.HttpContext.Cache[currentTestCacheKey]; var currentQuestion = currentTest.Questions[question]; QuestionViewModel currentQuestionView = new QuestionViewModel() { Answers = currentQuestion.Answers, Description = currentQuestion.Description, Index = question, Text = currentQuestion.Text, TestId = testId, IsFirst = question == 0 ? true : false, IsLast = question == currentTest.Questions.Count - 1 ? true : false }; return(View(currentQuestionView)); }
public ActionResult SelectTest(int id) { Test currentTest = this.tests.GetById(id); TestCacheModel testCache = new TestCacheModel() { Id = id, Name = currentTest.Name, Questions = currentTest.Questions.Select(x => new QuestionCacheModel { Id = x.Id, Text = x.Text, Description = x.Description, Answers = x.Answers, CorrectAnswerId = x.CorrectAnswerId }).ToList(), QuestionIndex = 0 }; string currentUserId = User.Identity.GetUserId(); string testCacheKey = currentUserId + currentTest.Id; TestStartViewModel tsvm = new TestStartViewModel() { Id = currentTest.Id, Name = currentTest.Name, Question = 0 }; this.HttpContext.Cache.Insert( testCacheKey, testCache, null, DateTime.Now.AddDays(1), TimeSpan.Zero, CacheItemPriority.Default, null); return(View(tsvm)); }
public async Task Get_when_null_does_not_call_deserialize() { var cancellationToken = CancellationToken.None; var testCacheModel = new TestCacheModel(); var cachedByteResults = Array.Empty <byte>(); distributedCacheMock .Setup(distributedCache => distributedCache.GetAsync(nameof(TestCacheModel), cancellationToken)) .Returns(Task.FromResult(cachedByteResults)).Verifiable(); messagePackServiceMock.Setup(messagePackService => messagePackService .Deserialise <TestCacheModel>(cachedByteResults, sut._messagePackOptions)) .Returns(Task.FromResult(default(TestCacheModel))) .Verifiable(); var result = await sut.Get <TestCacheModel>(nameof(TestCacheModel), cancellationToken); distributedCacheMock.Verify(distributedCache => distributedCache .GetAsync(nameof(TestCacheModel), cancellationToken), Times.Once); messagePackServiceMock.Verify(messagePackService => messagePackService .Deserialise <TestCacheModel>(cachedByteResults, sut._messagePackOptions), Times.Never); Assert.IsNull(result); }
public void RemoveCacheItemTest() { _pool.Register <TestTable>(); CacheIndex index = new CacheIndex() { IndexName = "主键测试", IndexType = IndexType.唯一索引, GetIndexKey = GetKey }; _pool.Get <TestTable>().AddIndex(index); List <ICacheIndex> indexs = _pool.Get <TestTable>().GetAllIndexs(); TestCacheModel model = new TestCacheModel() { ID = 1, Name = "测试" }; _pool.Get <TestTable>().Add(model); _pool.Get <TestTable>().Remove(model); List <ICacheItem> list = _pool.Get <TestTable>().Get(model.ID.ToString()); Assert.IsTrue(list.Count == 0); }
private double CalculateTestResult(TestCacheModel currentTest) { int correctAnswers = currentTest.Questions.Where(q => q.Guessed).Count(); return(((double)correctAnswers / currentTest.Questions.Count) * 100); }
private string GetKey(ICacheItem cache) { TestCacheModel model = cache as TestCacheModel; return(model.ID.ToString()); }