From 06c123c88ebdd36c9bbfa0c0188e891d54f4e0ea Mon Sep 17 00:00:00 2001 From: Pravakar Garnayak Date: Mon, 29 Apr 2019 15:47:39 -0700 Subject: [PATCH] Load cache during in first call (#158) * Load cache during in first call --- ServerlessLibraryAPI/CacheService.cs | 43 +++++++++++++++++------- ServerlessLibraryAPI/FileLibraryStore.cs | 36 -------------------- 2 files changed, 31 insertions(+), 48 deletions(-) delete mode 100644 ServerlessLibraryAPI/FileLibraryStore.cs diff --git a/ServerlessLibraryAPI/CacheService.cs b/ServerlessLibraryAPI/CacheService.cs index 2e3664b..c290924 100644 --- a/ServerlessLibraryAPI/CacheService.cs +++ b/ServerlessLibraryAPI/CacheService.cs @@ -10,22 +10,21 @@ using System.Threading.Tasks; namespace ServerlessLibrary { - public interface ICacheService { + public interface ICacheService + { IList GetCachedItems(); } //https://stackoverflow.com/questions/44723017/in-memory-caching-with-auto-regeneration-on-asp-net-core - public class CacheService:ICacheService + public class CacheService : ICacheService { protected readonly IMemoryCache _cache; - private IHostingEnvironment _env; private readonly ILibraryStore libraryStore; private readonly ILogger logger; - public CacheService(IMemoryCache cache, IHostingEnvironment env, ILibraryStore libraryStore, ILogger logger) + public CacheService(IMemoryCache cache, ILibraryStore libraryStore, ILogger logger) { this._cache = cache; - this._env = env; this.libraryStore = libraryStore; this.logger = logger; InitTimer(); @@ -41,13 +40,32 @@ namespace ServerlessLibrary public Task LoadingTask = Task.CompletedTask; public Timer Timer { get; set; } public bool LoadingBusy = false; + private bool isCacheLoadedOnce = false; - public IList GetCachedItems() { + public IList GetCachedItems() + { + + // Make a blocking call to load cache on first time call. + if (!isCacheLoadedOnce) + { + try + { + IList items = this.ConstructCache().Result; + _cache.Set(ServerlessLibrarySettings.CACHE_ENTRY, new LibraryItemsResult() { Result = items, IsBusy = false }); + } + catch (Exception ex) + { + this.logger.LogError(ex, "Failed to load cache in first call"); + } + } + + isCacheLoadedOnce = true; return _cache.Get(ServerlessLibrarySettings.CACHE_ENTRY).Result; } + private async void TimerTickAsync(object state) { - if (LoadingBusy) return; + if (!isCacheLoadedOnce || LoadingBusy) return; try { LoadingBusy = true; @@ -70,7 +88,7 @@ namespace ServerlessLibrary var items = await ConstructCache(); _cache.Set(ServerlessLibrarySettings.CACHE_ENTRY, new LibraryItemsResult() { Result = items, IsBusy = false }); } - catch(Exception ex) + catch (Exception ex) { this.logger.LogError(ex, "Failed to load cache"); } @@ -79,8 +97,8 @@ namespace ServerlessLibrary { IList libraryItems; IList libraryItemsWithStats = new List(); - libraryItems = await this.libraryStore.GetAllItems(); - var stats = await StorageHelper.getSLItemRecordsAsync(); + libraryItems = await this.libraryStore.GetAllItems(); + var stats = await StorageHelper.getSLItemRecordsAsync(); foreach (var storeItem in libraryItems) { var item = storeItem.ConvertTo(); @@ -95,9 +113,10 @@ namespace ServerlessLibrary } } - public class LibraryItemsResult { + public class LibraryItemsResult + { public IList Result { get; set; } - public bool IsBusy{ get; set; } + public bool IsBusy { get; set; } } } diff --git a/ServerlessLibraryAPI/FileLibraryStore.cs b/ServerlessLibraryAPI/FileLibraryStore.cs deleted file mode 100644 index 0ee8441..0000000 --- a/ServerlessLibraryAPI/FileLibraryStore.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Hosting; -using Newtonsoft.Json; -using ServerlessLibrary.Models; - -namespace ServerlessLibrary -{ - /// - /// File library store. This storewill be useful for dev environement as well as untill cosmos db is onboarded - /// - - public class FileLibraryStore : ILibraryStore - { - private IHostingEnvironment _env; - - public FileLibraryStore(IHostingEnvironment env) - { - _env = env; - } - - public Task Add(LibraryItem libraryItem) - { - throw new NotImplementedException(); - } - - public async Task> GetAllItems() - { - var webRoot = _env.WebRootPath; - var file = System.IO.Path.Combine(webRoot, "items.json"); - var fileContent = JsonConvert.DeserializeObject>(await System.IO.File.ReadAllTextAsync(file)); - return fileContent; - } - } -} \ No newline at end of file