コード例 #1
0
        /// <inheritdoc/>
        public IStorageContainerClientSleeve GetBlobContainerSleeveForUri(Uri uri, StorageClientProviderContext context)
        {
            if (uri == null)
            {
                throw new ArgumentNullException(nameof(uri));
            }

            if (context == null)
            {
                throw new ArgumentNullException(
                          nameof(context),
                          $"Use {nameof(StorageClientProviderContext.None)} instead of null for 'empty' context");
            }

            // Cache policy notes -- see commentary in Gridwich.Core/Models/BlobBaseClientProvider.cs

            var cacheKey = GetCacheKeyForBlobContainerClient(uri, context);
            var sleeve   = _blobContainerClientCache.Get <IStorageContainerClientSleeve>(cacheKey);

            if (sleeve != null)
            {
                // found one, fix up it's context values to the new ones.
                // Note that the fact that we found one means the ClientRequestID is already correct, but
                // we should update the ETag & flag information, thus the ResetTo().
                sleeve.Context.ResetTo(context);
            }
            else
            {
                sleeve = CreateBlobContainerClientForUri(uri, context);
                _blobContainerClientCache.Add(cacheKey, sleeve, ClientExpirationTime);
            }

            return(sleeve);
        }
コード例 #2
0
        public SiteMapModule(IAppCache appCache)
        {
            cache = appCache;
            cache.GetOrAdd("sitemap1.xml", () => getSiteMap("https://localhost:10623/sitemap1.xml"));
            cache.GetOrAdd("sitemap2.xml", () => getSiteMap("https://localhost:10623/sitemap2.xml"));
            cache.GetOrAdd("sitemap3.xml", () => getSiteMap("https://localhost:10623/sitemap3.xml"));
            cache.GetOrAdd("sitemap.xml", () => getSiteMapIndex("https://localhost:10623/sitemap.xml"));
            var list = new List <string>();

            list.AddRange(new [] { "sitemap1.xml", "sitemap2.xml", "sitemap3.xml", "sitemap.xml" });
            foreach (var item in list)
            {
                Get($"/{item}", async(req, resp) =>
                {
                    if (item.Equals("sitemap.xml"))
                    {
                        var ms           = await cache.Get <Extensions.SiteMapIndex.SiteMapIndex>("sitemap.xml").Parse();
                        resp.ContentType = "application/xml";
                        resp.StatusCode  = 200;
                        await ms.CopyToAsync(resp.Body);
                        return;
                    }

                    var smap         = cache.Get <SiteMap>(item);
                    var ms2          = await smap.Parse();
                    resp.ContentType = "application/xml";
                    resp.StatusCode  = 200;
                    await ms2.CopyToAsync(resp.Body);
                    return;
                });
            }
        }
コード例 #3
0
        public virtual void AddThenGet_Guid_ReturnsExpectedResult()
        {
            var cacheEntryKey  = Fixture.Create <string>();
            var expectedResult = Fixture.Create <Guid>();

            CachingService.Add(cacheEntryKey, expectedResult);
            var actualResult = CachingService.Get <Guid>(cacheEntryKey);

            Assert.That(actualResult, Is.EqualTo(expectedResult));
        }
コード例 #4
0
        protected Extensions.SiteMapIndex.SiteMapIndex getSiteMapIndex(string path)
        {
            var sm1  = cache.Get <SiteMap>("sitemap1.xml");
            var sm2  = cache.Get <SiteMap>("sitemap2.xml");
            var sm3  = cache.Get <SiteMap>("sitemap3.xml");
            var smap = new Extensions.SiteMapIndex.SiteMapIndex(new Uri(path));

            smap.SiteMaps.AddRange(new [] { sm1, sm2, sm3 });
            return(smap);
        }
コード例 #5
0
        internal string GetAuthToken()
        {
            const string key   = nameof(GetAuthToken);
            var          value = _cache.Get <AccessTokenResponse>(key);

            if (value == null)
            {
                var tokenData = FetchAuthTokenFromServer();
                _cache.Add(key, tokenData, DateTimeOffset.Now.AddSeconds(tokenData.expires_in - 60));
            }

            return(_cache.Get <AccessTokenResponse>(key).access_token);
        }
コード例 #6
0
        public async Task <List <Book> > GetAll()
        {
            var books = _cache.Get <List <Book> >("books_in_cache");

            if (books == null)
            {
                books = await _repository.GetAll().ConfigureAwait(false);

                _cache.Add("books_in_cache", books);
            }

            return(await Task.FromResult(books).ConfigureAwait(false));
        }
コード例 #7
0
        ///<inheritdoc/>
        public async Task <ContainerRegistryInfo> GetContainerRegistryInfoAsync(string imageName)
        {
            var containerRegistryInfo = cache.Get <ContainerRegistryInfo>(imageName);

            if (containerRegistryInfo == null)
            {
                containerRegistryInfo = await azureProxy.GetContainerRegistryInfoAsync(imageName);

                if (containerRegistryInfo != null)
                {
                    cache.Add(imageName, containerRegistryInfo, DateTimeOffset.Now.AddHours(1));
                }
            }

            return(containerRegistryInfo);
        }
コード例 #8
0
        public virtual DateTime GetLastTradingDate()
        {
            var lastTradingDate = _cache.Get <DateTime>(CacheNames.LastTradingDateCacheKey);

            if (lastTradingDate.Date != DateTime.Now.Date)
            {
                lastTradingDate = DateTime.Now;

                if (lastTradingDate.DayOfWeek == DayOfWeek.Sunday)
                {
                    lastTradingDate = lastTradingDate.AddDays(-2);
                }
                if (lastTradingDate.DayOfWeek == DayOfWeek.Saturday)
                {
                    lastTradingDate = lastTradingDate.AddDays(-1);
                }

                //consider holiday

                _cache.Add(CacheNames.LastTradingDateCacheKey, lastTradingDate.Date, NextClosingDateTime());
            }


            return(lastTradingDate.Date);
        }
コード例 #9
0
    public static T?GetCacheItem <T>(this IAppCache provider, string cacheKey)
    {
        var result = provider.Get(cacheKey);

        if (result == null)
        {
            return(default);
コード例 #10
0
 public static void SafeRemove <T>(this IAppCache cache, string cacheKey)
 {
     if (cache.Get <T>(cacheKey) != null)
     {
         cache.Remove(cacheKey);
     }
 }
コード例 #11
0
        /// <inheritdoc/>
        public async Task <ContainerRegistryInfo> GetContainerRegistryInfoAsync(string imageName)
        {
            var containerRegistryInfo = cache.Get <ContainerRegistryInfo>(imageName);

            if (containerRegistryInfo is null)
            {
                containerRegistryInfo = await asyncRetryPolicy.ExecuteAsync(() => azureProxy.GetContainerRegistryInfoAsync(imageName));

                if (containerRegistryInfo is not null)
                {
                    cache.Add(imageName, containerRegistryInfo, DateTimeOffset.Now.AddHours(1));
                }
            }

            return(containerRegistryInfo);
        }
コード例 #12
0
        private void GetSolarValues()
        {
            var cache = _cache.Get <ConcurrentDictionary <long, string> >(SolarPowerCache.CacheKey);

            _logger.Information("Fronius clearing {Count} {Average}", cache.Count, cache.Values.FirstOrDefault());
            cache.Clear();
        }
コード例 #13
0
        public async Task <int> GetUserId()
        {
            if (_appCache.Contains(UserIdKey))
            {
                return(_appCache.Get <int>(UserIdKey));
            }

            int userId;
            var user = await _userService.GetByUserNameAsync(UserName);

            if (user == null)
            {
                // No user exists so let's create one
                user = new Data.Models.User
                {
                    Name     = UserName,
                    UserName = UserName
                };
                userId = await _userService.CreateAsync(user /*, _appConfigHelper.SystemUserId*/);
            }
            else
            {
                userId = user.Id;
            }

            _appCache.Add(UserIdKey, userId);

            return(userId);
        }
コード例 #14
0
 private CacheValues GetCacheValues(IAppCache cache)
 {
     if (cache == null) // no cache, don't cache
     {
         return(new CacheValues());
     }
     return((CacheValues)cache.Get(ValuesCacheKey, () => new CacheValues()));
 }
コード例 #15
0
        public static async Task <string> GetImageAsync(string url)
        {
            var base64 = cache.Get <string>(url);

            if (!string.IsNullOrEmpty(base64))
            {
                return(base64);
            }

            using (WebClient webClient = new WebClient())
            {
                byte[] dataArr = webClient.DownloadData(url);
                //save file to local
                base64 = Convert.ToBase64String(dataArr);
                cache.Add <string>(url, base64);
                return(base64);
            }
        }
コード例 #16
0
        public static T GetCacheItem <T>(this IAppCache provider, string cacheKey, Func <T> getCacheItem)
        {
            var result = provider.Get(cacheKey, () => getCacheItem());

            if (result == null)
            {
                return(default(T));
            }
            return(result.TryConvertTo <T>().Result);
        }
コード例 #17
0
        /// <inheritdoc/>
        public IStorageBlobClientSleeve GetBlobClientSleeveForUri(Uri blobUri, StorageClientProviderContext context)
        {
            if (blobUri == null)
            {
                throw new ArgumentNullException(nameof(blobUri));
            }

            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            // Cache policy
            //
            // We need to support sequences of related storage operations.  For example, in Gridwich,
            // deleting a blob requires two sequential steps: retrieving metadata and then deleting the blob. If
            // we permit caching keyed solely on blob URI, there arise issues with "crossing the streams" -- i.e.,
            // two request streams both happen to target the same blob at the overlapping times.  Specifically,
            // retrieving a client primed for a different Operation Context/ETag combination causes failures.
            //
            // Another consideration is that the Storage Service, the consumer of the cache items, is not
            // structured to pass clients from method to method.   In general, if one storage service method
            // calls another (e.g., DeleteBlob calls GetMetadata), it passed only the URI and context, not the client.
            // Thus it is important that it be possible for those two to be sufficient for each method to retrieve
            // a "hot" client instance from the cache (or maybe a fresh one, the first time), avoiding the need for
            // using a fresh client each time and incurring authentication, connection establishment, etc.
            //
            // For those reasons, two policies/conventions:
            //    1. The client cache will always be keyed solely on the blobURI/Context combination.
            //       Should some valid circumstance arise where a context is not available, code falls back to using
            //       StorageClientProviderContext.NO_CONTEXT.
            //    2. The cache item expiry time will be dropped to a much lower time span than the original 10 minutes.
            //       Cache items should persist for as close to the duration of a request.  Since Storage service
            //       methods are unaware of how callers are sequencing them, it is not clear when it is "safe" for
            //       a method to remove cache items.  Thus, we will have to depend on cache item expiry to keep
            //       the cache size down.

            var cacheKey = GetCacheKeyForBlobBaseClient(blobUri, context);
            var sleeve   = _blobBaseClientCache.Get <IStorageBlobClientSleeve>(cacheKey);

            if (sleeve != null)
            {
                // found one, fix up it's context values to the new ones.
                // Note that the fact that we found one means the ClientRequestID is already correct, but
                // we should update the ETag & flag information, thus the ResetTo().
                sleeve.Context.ResetTo(context);
            }
            else
            {
                sleeve = CreateBlobClientSleeveForUri(blobUri, context);
                _blobBaseClientCache.Add(cacheKey, sleeve, ClientExpirationTime);
            }

            return(sleeve);
        }
コード例 #18
0
        public override async Task <LatestStockMarketActivityVm> GetLatestStockMarketActivity()
        {
            var cacheKey = string.Format(CACHE_LATEST_MARKET_ACTIVITY, $"{DateTime.Now:MMdd}");

            //if within trading day and time, always fetch the data
            if (IsTradingHours())
            {
                var data = await base.GetLatestStockMarketActivity();

                if (_cache.Get <LatestStockMarketActivityVm>(cacheKey) != null)
                {
                    _cache.Remove(cacheKey);
                }

                return(data);
            }


            return(await _cache.GetOrAddAsync(cacheKey, () => base.GetLatestStockMarketActivity(), _marketStatusRepository.NextClosingDateTime()));
        }
コード例 #19
0
        public static bool TryGetValue(
            string key,
            out string value)
        {
            value = _cache.Get <string>(key);

            if (value != null)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
コード例 #20
0
        /// <summary>
        /// Gets an IPublishedContent corresponding to an Xml cache node.
        /// </summary>
        /// <param name="node">The Xml node.</param>
        /// <param name="isPreviewing">A value indicating whether we are previewing or not.</param>
        /// <param name="appCache">A cache.</param>
        /// <param name="contentTypeCache">A content type cache.</param>
        /// <param name="umbracoContextAccessor">A umbraco context accessor</param>
        /// <returns>The IPublishedContent corresponding to the Xml cache node.</returns>
        /// <remarks>Maintains a per-request cache of IPublishedContent items in order to make
        /// sure that we create only one instance of each for the duration of a request. The
        /// returned IPublishedContent is a model, if models are enabled.</remarks>
        public static IPublishedContent Get(XmlNode node, bool isPreviewing, IAppCache appCache,
                                            PublishedContentTypeCache contentTypeCache)
        {
            // only 1 per request

            var attrs = node.Attributes;
            var id    = attrs?.GetNamedItem("id").Value;

            if (id.IsNullOrWhiteSpace())
            {
                throw new InvalidOperationException("Node has no ID attribute.");
            }
            var key = CacheKeyPrefix + id; // dont bother with preview, wont change during request in Xml cache

            return((IPublishedContent)appCache.Get(key, () => (new XmlPublishedContent(node, isPreviewing, appCache, contentTypeCache)).CreateModel()));
        }
コード例 #21
0
        internal static IAppCache SetUpCacheEntryGet <T>(this IAppCache mockedCachingService, string cacheEntryKey, T cacheEntryValue)
        {
            EnsureArgument.IsNotNull(mockedCachingService, nameof(mockedCachingService));
            EnsureArgument.IsNotNullOrEmpty(cacheEntryKey, nameof(cacheEntryKey));

            Logger.LogDebug("Setting up cache entry Get/GetOrAdd for '{cacheEntryKey}' (type: '{type}'; value: '{cacheEntryValue}')", cacheEntryKey, typeof(T), cacheEntryValue);

            mockedCachingService.Get <T>(Arg.Is <string>(s => s.Equals(cacheEntryKey))).Returns(cacheEntryValue).AndDoes(x => Logger.LogDebug("Cache Get invoked"));

            mockedCachingService.GetOrAdd(Arg.Is <string>(s => s.Equals(cacheEntryKey)), Arg.Any <Func <ICacheEntry, T> >())
            .Returns(cacheEntryValue)
            .AndDoes(x => Logger.LogDebug("Cache GetOrAdd invoked"));

            //Backwards compatibility
            if (ProjectReflectionShortcuts.GetOrAddWithMemoryCacheEntryOptionsMethod != null)
            {
                Logger.LogDebug("Setting up GetOrAddWithMemoryCacheEntryOptionsMethod");

                ProjectReflectionShortcuts.GetOrAddWithMemoryCacheEntryOptionsMethod.MakeGenericMethod(typeof(T))
                .Invoke(mockedCachingService.Configure(),
                        new object[] { Arg.Is <string>(s => s.Equals(cacheEntryKey)), Arg.Any <Func <ICacheEntry, T> >(), Arg.Any <MemoryCacheEntryOptions>() })
                .Returns(cacheEntryValue)
                .AndDoes(x => Logger.LogDebug("Cache GetOrAdd invoked"));
            }

            mockedCachingService.GetAsync <T>(Arg.Is <string>(s => s.Equals(cacheEntryKey)))
            .Returns(Task.FromResult(cacheEntryValue))
            .AndDoes(x => Logger.LogDebug("Cache GetAsync invoked"));

            mockedCachingService.GetOrAddAsync(Arg.Is <string>(s => s.Equals(cacheEntryKey)), Arg.Any <Func <ICacheEntry, Task <T> > >())
            .Returns(Task.FromResult(cacheEntryValue))
            .AndDoes(x => Logger.LogDebug("Cache GetOrAddAsync invoked"));

            //Backwards compatibility
            if (ProjectReflectionShortcuts.GetOrAddAsyncWithMemoryCacheEntryOptionsMethod != null)
            {
                Logger.LogDebug("Setting up GetOrAddAsyncWithMemoryCacheEntryOptionsMethod");

                ProjectReflectionShortcuts.GetOrAddAsyncWithMemoryCacheEntryOptionsMethod.MakeGenericMethod(typeof(T))
                .Invoke(mockedCachingService.Configure(),
                        new object[] { Arg.Is <string>(s => s.Equals(cacheEntryKey)), Arg.Any <Func <ICacheEntry, Task <T> > >(), Arg.Any <MemoryCacheEntryOptions>() })
                .Returns(Task.FromResult(cacheEntryValue))
                .AndDoes(x => Logger.LogDebug("Cache GetOrAddAsync invoked"));
            }

            return(mockedCachingService);
        }
コード例 #22
0
        private Dictionary <long, decimal> GetSolarValues(long lowerBound, long upperBoundExclusive)
        {
            var newCache = new Dictionary <long, decimal>();
            var cache    = _cache.Get <ConcurrentDictionary <long, string> >(SolarPowerCache.CacheKey);

            for (var i = lowerBound; i < upperBoundExclusive; i++)
            {
                if (!cache.TryRemove(i, out var value))
                {
                    continue;
                }
                if (!decimal.TryParse(value, out decimal decimalValue))
                {
                    continue;
                }

                newCache.Add(i, decimalValue);
            }
            return(newCache);
        }
コード例 #23
0
        public async Task <IActionResult> Post([FromBody] Update update, [FromQuery] string token)
        {
            var isOldMessage = update.Message?.Date < _startupOptions.StartDateTime;

            if (isOldMessage)
            {
                return(Ok());
            }

            var existingUpdateId = _cache.Get <bool>(update.Id.ToString());

            if (existingUpdateId)
            {
                return(Ok());
            }

            _cache.Add(update.Id.ToString(), true, DateTimeOffset.UtcNow.AddMinutes(5));

            await _updateService.Update(update);

            return(Ok());
        }
コード例 #24
0
        public async Task <TmdbConfigurationModelResult> GetTmdbConfiguration(int retryCount = 0, int delayMilliseconds = 1000, bool fromCache = true)
        {
            string key = "$" + nameof(GetTmdbConfiguration);

            if (!fromCache)
            {
                _cache.Remove(key);
            }

            var result = _cache.Get <TmdbConfigurationModelResult>(key) ?? await _networkClient.GetTmdbConfiguration(retryCount, delayMilliseconds);

            if (result.HttpStatusCode.IsSuccessCode())
            {
                _cache.Add(key, result, System.TimeSpan.FromDays(1));
            }

            return(result);
        }
コード例 #25
0
 public static T GetItem(string keyName)
 {
     LogHandler.Info($"Cache - Getting {keyName}");
     return(cache.Get <T>(keyName));
 }
コード例 #26
0
ファイル: MovieService.cs プロジェクト: maranmaran/NextMovie
        public async Task <PagedResult <Movie> > GetRecommendations(int page = 1, CancellationToken cancellationToken = default)
        {
            var likedMovies = await _context.Movies.ToListAsync(cancellationToken);

            if (likedMovies == null || likedMovies.Count == 0)
            {
                throw new Exception("No movies");
            }

            var likedMoviesHash = new HashSet <int>(likedMovies.Select(x => x.Id));

            var results = new Dictionary <int, Movie>();

            var recommendationsPage = (int)Math.Ceiling((double)page / likedMovies.Count);

            var cachedEntry = _cache.Get <PagedResult <Movie> >(ResolveRecommendationsCacheKey(recommendationsPage, likedMovies.Count));

            if (cachedEntry != null)
            {
                var cachedResult = new PagedResult <Movie>()
                {
                    Page       = page,
                    TotalPages = cachedEntry.TotalPages,
                    Results    = cachedEntry.Results.Skip((page - 1) * _pageSize).Take(_pageSize).ToList()
                };

                return(cachedResult);
            }

            var totalPages = 0;

            while (results.Values.Count < page * _pageSize)
            {
                foreach (var likedMovie in likedMovies)
                {
                    var recommendationsList = await _tmdbMovieService.GetRecommendations(likedMovie.Id, recommendationsPage, cancellationToken : cancellationToken);

                    foreach (var recommendation in recommendationsList.Results)
                    {
                        if (!results.ContainsKey(recommendation.Id) && !likedMoviesHash.Contains(recommendation.Id))
                        {
                            results.Add(recommendation.Id, recommendation);
                        }
                    }

                    totalPages += recommendationsList.TotalPages;
                }
            }

            // Sort by vote counts - amount of user votes
            // Sort by vote average - average grade of user votes
            // Sort by popularity - best user rated movies...most popular ones first
            var recommendedMovies = results.Values.ToList();

            recommendedMovies = recommendedMovies
                                .OrderByDescending(x => x.VoteCount)
                                .ThenBy(x => x.VoteAverage)
                                .ThenBy(x => x.Popularity)
                                .ToList();

            var entryToCache = new PagedResult <Movie>()
            {
                TotalPages = totalPages,
                Results    = recommendedMovies
            };

            _cache.Add(ResolveRecommendationsCacheKey(recommendationsPage, likedMovies.Count), entryToCache);

            return(new PagedResult <Movie>()
            {
                Page = 1,
                TotalPages = totalPages,
                Results = recommendedMovies.Skip(page - 1 * 20).Take(_pageSize).ToList(),
            });

            ;
        }
コード例 #27
0
        public void AddComplexObjectThenGetGenericReturnsCachedObject()
        {
            testObject.SomeItems.Add("Another");
            testObject.SomeMessage = "changed-it-up";
            sut.Add(TestKey, testObject);
            var actual   = sut.Get <ComplexTestObject>(TestKey);
            var expected = testObject;

            Assert.NotNull(actual);
            Assert.AreEqual(expected, actual);
            testObject.SomeItems.Should().Contain("Another");
            testObject.SomeMessage.Should().Be("changed-it-up");
        }
コード例 #28
0
ファイル: AppService.cs プロジェクト: yangyanze0428/project
 public AppDto GetAppCache(string appKey)
 {
     return(_appCache.Get(appKey));
 }
コード例 #29
0
 public T Get <T>(string key)
 {
     return(_cache.Get <T>(key));
 }
コード例 #30
0
 public static List <string> Get(string cacheItem)
 {
     return(cache.Get <List <string> >(cacheItem));
 }