コード例 #1
0
        public static async Task <List <string> > GetServicesWithCache(this IDiscoveryClient discoveryClient, string serviceName, IDistributedCache distributedCache = null, DistributedCacheEntryOptions cacheEntryOptions = null)
        {
            if (distributedCache != null)
            {
                var cacheData = await distributedCache.GetAsync(serviceName);

                if (cacheData != null && cacheData.Length > 0)
                {
                    return(DeserializeFromCache <List <string> >(cacheData));
                }
            }

            var services = await discoveryClient.GetHealthServices(serviceName);

            if (distributedCache != null && services != null && services.Count > 0)
            {
                byte[] obj = SerializeForCache(services);
                await distributedCache.SetAsync(serviceName, obj, cacheEntryOptions ?? new DistributedCacheEntryOptions {
                    AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(30)
                });
            }

            return(services);
        }