コード例 #1
0
        public async Task GetPokemon()
        {
            var pokemon = await service.GetPokemonAsync(Name);

            Assert.AreEqual(Name, pokemon.Name);
            Assert.AreEqual(Flavor, pokemon.RawFlavorText);
            Assert.AreEqual(Translation, pokemon.ShakespeareFlavorText);
        }
コード例 #2
0
        public async Task GetKnownPokemon()
        {
            string name   = "charizard";
            string flavor =
                "Charizard flies around the sky in search of powerful opponents.\n" +
                "It breathes fire of such great heat that it melts anything.\n" +
                "However, it never turns its fiery breath on any opponent\n" +
                "weaker than itself.";
            string shakespeare = "MOCK SHAKESPEARE - " + flavor;

            var pokemon = await service.GetPokemonAsync(name);

            Assert.AreEqual(name, pokemon.Name);
            Assert.AreEqual(flavor, pokemon.RawFlavorText);
            Assert.AreEqual(shakespeare, pokemon.ShakespeareFlavorText);
        }
コード例 #3
0
ファイル: PokemonController.cs プロジェクト: agmt76/pok-shak
        public async Task <Pokemon> Get([FromRoute] string name)
        {
            // MemoryCache is thread safe
            // There is a race condition, where 2 simultaneous requests get a cache miss and both populate the cache
            // This will do no harm, just waste a bit of cpu and network traffic
            // Locking would solve this, with a slight overhead on every cache miss

            bool exists = memoryCache.TryGetValue(name, out Pokemon pokemon);

            if (!exists)
            {
                pokemon = await service.GetPokemonAsync(name);

                memoryCache.Set(name, pokemon, TimeSpan.FromHours(1));
            }

            return(pokemon);
        }