From 76e90c8424e65a894892097aa906a82aef4cc77f Mon Sep 17 00:00:00 2001 From: Jason Johnston Date: Wed, 23 May 2018 11:00:14 -0400 Subject: [PATCH] Updated SubscriptionCache to use System.Runtime.Caching To be consistent with the rest of the project --- .../Controllers/SubscriptionController.cs | 1 - GraphWebhooks/Helpers/SubscriptionCache.cs | 29 ++++++++------- GraphWebhooks/Helpers/SubscriptionHelper.cs | 35 +------------------ 3 files changed, 15 insertions(+), 50 deletions(-) diff --git a/GraphWebhooks/Controllers/SubscriptionController.cs b/GraphWebhooks/Controllers/SubscriptionController.cs index 8516257..bb48860 100644 --- a/GraphWebhooks/Controllers/SubscriptionController.cs +++ b/GraphWebhooks/Controllers/SubscriptionController.cs @@ -7,7 +7,6 @@ using GraphWebhooks.Models; using Microsoft.Identity.Client; using System; -using System.Security.Claims; using System.Threading.Tasks; using System.Web.Mvc; diff --git a/GraphWebhooks/Helpers/SubscriptionCache.cs b/GraphWebhooks/Helpers/SubscriptionCache.cs index 9b36af8..067478b 100644 --- a/GraphWebhooks/Helpers/SubscriptionCache.cs +++ b/GraphWebhooks/Helpers/SubscriptionCache.cs @@ -1,7 +1,6 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; +using System.Runtime.Caching; using System.Timers; -using System.Web; namespace GraphWebhooks.Helpers { @@ -9,6 +8,9 @@ public class SubscriptionCache { static SubscriptionCache cache = null; + private static ObjectCache objCache = MemoryCache.Default; + private static CacheItemPolicy defaultPolicy = new CacheItemPolicy(); + Timer timer; private SubscriptionCache() { @@ -19,7 +21,7 @@ private SubscriptionCache() }; renewalTimer.Elapsed += OnRenewal; renewalTimer.Start(); - this.timer = renewalTimer; + timer = renewalTimer; } public static SubscriptionCache GetSubscriptionCache() @@ -36,7 +38,7 @@ public static SubscriptionCache GetSubscriptionCache() private async void OnRenewal(object sender, ElapsedEventArgs e) { - Dictionary subscriptionstore = HttpRuntime.Cache.Get("subscription_store") as Dictionary; + var subscriptionstore = objCache.Get("subscription_store") as Dictionary; foreach (var item in subscriptionstore) { @@ -53,38 +55,35 @@ private async void OnRenewal(object sender, ElapsedEventArgs e) timer.Start(); } - // This sample temporarily stores the current subscription ID, client state, user object ID, and tenant ID. // This info is required so the NotificationController can retrieve an access token from the cache and validate the subscription. // Production apps typically use some method of persistent storage. public void SaveSubscriptionInfo(SubscriptionDetails subscriptionDetails) { - if (HttpRuntime.Cache["subscription_store"] == null) + if (objCache["subscription_store"] == null) { - Dictionary subscriptionstore = new Dictionary(); + var subscriptionstore = new Dictionary(); subscriptionstore.Add(subscriptionDetails.SubscriptionId, subscriptionDetails); - HttpRuntime.Cache.Add("subscription_store", - subscriptionstore, - null, DateTime.MaxValue, new TimeSpan(24, 0, 0), System.Web.Caching.CacheItemPriority.NotRemovable, null); + + objCache.Set(new CacheItem("subscription_store", subscriptionstore), defaultPolicy); } else { - Dictionary subscriptionstore = HttpRuntime.Cache.Get("subscription_store") as Dictionary; + var subscriptionstore = objCache.Get("subscription_store") as Dictionary; subscriptionstore.Add(subscriptionDetails.SubscriptionId, subscriptionDetails); } } public SubscriptionDetails GetSubscriptionInfo(string subscriptionId) { - Dictionary subscriptionstore = HttpRuntime.Cache.Get("subscription_store") as Dictionary; + var subscriptionstore = objCache.Get("subscription_store") as Dictionary; return subscriptionstore[subscriptionId]; } public Dictionary DeleteAllSubscriptions() { - return HttpRuntime.Cache.Remove("subscription_store") as Dictionary; - + return objCache.Remove("subscription_store") as Dictionary; } } } \ No newline at end of file diff --git a/GraphWebhooks/Helpers/SubscriptionHelper.cs b/GraphWebhooks/Helpers/SubscriptionHelper.cs index 06facf8..97c59f5 100644 --- a/GraphWebhooks/Helpers/SubscriptionHelper.cs +++ b/GraphWebhooks/Helpers/SubscriptionHelper.cs @@ -1,14 +1,8 @@ -using GraphWebhooks.Models; -using Microsoft.Graph; -using Newtonsoft.Json; +using Microsoft.Graph; using System; -using System.Collections.Generic; using System.Configuration; -using System.Linq; -using System.Net.Http; using System.Security.Claims; using System.Threading.Tasks; -using System.Web; namespace GraphWebhooks.Helpers { @@ -50,33 +44,6 @@ internal static async Task CreateSubscription(string baseUrl, stri SubscriptionCache.GetSubscriptionCache().SaveSubscriptionInfo(subscriptionDetails); return newSubscription; - - //SubscriptionStore.SaveSubscriptionInfo(newSubscription.Id, - // newSubscription.ClientState, - // ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier")?.Value); - - //// Send the `POST subscriptions` request and parse the response. - //HttpResponseMessage response = await HttpHelper.SendAsync(subscriptionsEndpoint, HttpMethod.Post, subscription); - - //if (!response.IsSuccessStatusCode) - //{ - // return response; - //} - - //string stringResult = await response.Content.ReadAsStringAsync(); - //var createdSubscription = JsonConvert.DeserializeObject(stringResult); - //var subscriptionDetails = new SubscriptionDetails( - // createdSubscription.Id, - // createdSubscription.ClientState, - // ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier")?.Value, - // ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/tenantid")?.Value); - - //// This sample temporarily stores the current subscription ID, client state, user object ID, and tenant ID. - //// This info is required so the NotificationController, which is not authenticated, can retrieve an access token from the cache and validate the subscription. - //// Production apps typically use some method of persistent storage. - //SubscriptionCache.GetSubscriptionCache().SaveSubscriptionInfo(subscriptionDetails); - - //return response; }