public async Task Should_be_valid_when_job_is_requested_to_update_cache_and_cache_is_not_empty() { _cache.Set(CacheKeys.Portfolio, Array.Empty <byte>()); _updateCacheJob = new UpdateCacheJob(_cache, _mockPortfolio.Object); _mockPortfolio.Setup(x => x.GetAsync()).ReturnsAsync(InvestmentsResponse); await _updateCacheJob.UpdateAsync(); _cache.Get(CacheKeys.Portfolio).Should().NotBeNull(); }
private void SetAuth(DateTime expiryTime) { _intendedToken = new JwtSecurityToken("localhost", "core_api", new List <Claim>(), DateTime.UtcNow.AddHours(-2), expiryTime); var handler = new JwtSecurityTokenHandler(); _cache.Set(CacheKeyForToken, Encoding.UTF8.GetBytes(handler.WriteToken(_intendedToken)), new DistributedCacheEntryOptions { AbsoluteExpiration = expiryTime }); }
public void ExampleTestMethod() { var expectedData = new byte[] { 100, 200 }; var opts = Options.Create <MemoryDistributedCacheOptions>(new MemoryDistributedCacheOptions()); IDistributedCache cache = new MemoryDistributedCache(opts); cache.Set("key1", expectedData); var cachedData = cache.Get("key1"); Assert.AreEqual(expectedData, cachedData); //Use the variable cache as an input to any class which expects IDistributedCache }
public void Get_WithLocalCachedItem_Null_Distributed_NotNull() { var memoryCache = new MemoryCache(new Microsoft.Extensions.Options.OptionsWrapper <MemoryCacheOptions>( new MemoryCacheOptions())); memoryCache.Set("UserByName_admin$Generation$", (ulong)641427502); memoryCache.Set("UserGenerationKey", (ulong)641427502); memoryCache.Set <UserDefinition>("UserByName_admin", null); var distributedCache = new MemoryDistributedCache( new Microsoft.Extensions.Options.OptionsWrapper <MemoryDistributedCacheOptions>( new MemoryDistributedCacheOptions())); distributedCache.Set("UserByName_admin$Generation$", BitConverter.GetBytes((ulong)641427502)); distributedCache.SetAutoJson("UserByName_admin", new UserDefinition { Username = "******", UserId = 1 }); distributedCache.Set("UserGenerationKey", BitConverter.GetBytes((ulong)641427502)); var cache = new TwoLevelCache(memoryCache, distributedCache); var loadCount = 0; var user = GetUserByName(cache, "admin", ref loadCount); Assert.Equal("admin", user?.Username); Assert.Equal(0, loadCount); }
public async Task Should_be_return_portfolio_using_cache() { var investmentResponseBytes = System.Text.Json.JsonSerializer.SerializeToUtf8Bytes(InvestmentsResponse); _cache.Set(CacheKeys.Portfolio, investmentResponseBytes); var handler = new InvestimentsHandler(_mockPortfolio.Object, _cache, _mockTracer.Object); var response = await handler.Handle(new InvestmentsRequest(), CancellationToken.None); _mockPortfolio.Verify(x => x.GetAsync(), Times.Never); response.Result.Should().NotBeNull(); response.Valid.Should().BeTrue(); response.Result.Should().BeOfType <InvestmentsResponse>(); }
public MoveControllerTests() { var game = HiveFactory.Create(new[] { "player1", "player2" }); game.Move(new Move(game.Players[0].Tiles.First(), new Coords(1, 0))); game.Move(new Move(game.Players[1].Tiles.First(), new Coords(2, 0))); var gameState = new GameState(game.Players, game.Cells, TestHelpers.ExistingGameId, GameStatus.NewGame); var jsonOptions = TestHelpers.CreateJsonOptions(); _memoryCache = TestHelpers.CreateTestMemoryCache(); _memoryCache.Set(TestHelpers.ExistingGameId, TestHelpers.GetSerializedBytes(gameState, jsonOptions)); _hubMock = new Mock <IHubContext <GameHub> >(); _hubMock.Setup( m => m.Clients.Group(It.IsAny <string>()) .SendCoreAsync(It.IsAny <string>(), It.IsAny <object[]>(), It.IsAny <CancellationToken>()) ) .Returns(() => Task.CompletedTask); _controller = new MoveController(_hubMock.Object, Options.Create(jsonOptions), _memoryCache); }
public void GivenCustomerIdWhenGetCustomerThenReadFromCache() { // Arrange var opts = Options.Create(new MemoryDistributedCacheOptions()); var cache = new MemoryDistributedCache(opts); var options = new DbContextOptionsBuilder <CustomerDbContext>() .UseInMemoryDatabase(databaseName: "customerdatabse") .Options; var expect = new Customer { City = "Chennai", CustomerId = Guid.NewGuid(), }; cache.Set($"{expect.CustomerId}", expect); var customerDbContext = new CustomerDbContext(options); var customerRedisContext = new CustomerRedisContext(cache, customerDbContext); // Act var response = customerRedisContext.GetCustomer(expect.CustomerId); // Assert Assert.Equal(expect.City, response.City); }
public void Set(string key, byte[] value, DistributedCacheEntryOptions options) => _cache.Set(key, value, options);