Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

Commit

Permalink
Updated SubscriptionCache to use System.Runtime.Caching
Browse files Browse the repository at this point in the history
To be consistent with the rest of the project
  • Loading branch information
jasonjoh committed May 23, 2018
1 parent 79dcfdf commit 76e90c8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 50 deletions.
1 change: 0 additions & 1 deletion GraphWebhooks/Controllers/SubscriptionController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
29 changes: 14 additions & 15 deletions GraphWebhooks/Helpers/SubscriptionCache.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Runtime.Caching;
using System.Timers;
using System.Web;

namespace GraphWebhooks.Helpers
{
public class SubscriptionCache
{
static SubscriptionCache cache = null;

private static ObjectCache objCache = MemoryCache.Default;
private static CacheItemPolicy defaultPolicy = new CacheItemPolicy();

Timer timer;
private SubscriptionCache()
{
Expand All @@ -19,7 +21,7 @@ private SubscriptionCache()
};
renewalTimer.Elapsed += OnRenewal;
renewalTimer.Start();
this.timer = renewalTimer;
timer = renewalTimer;
}

public static SubscriptionCache GetSubscriptionCache()
Expand All @@ -36,7 +38,7 @@ public static SubscriptionCache GetSubscriptionCache()

private async void OnRenewal(object sender, ElapsedEventArgs e)
{
Dictionary<string, SubscriptionDetails> subscriptionstore = HttpRuntime.Cache.Get("subscription_store") as Dictionary<string, SubscriptionDetails>;
var subscriptionstore = objCache.Get("subscription_store") as Dictionary<string, SubscriptionDetails>;

foreach (var item in subscriptionstore)
{
Expand All @@ -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<string, SubscriptionDetails> subscriptionstore = new Dictionary<string, SubscriptionDetails>();
var subscriptionstore = new Dictionary<string, SubscriptionDetails>();
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<string, SubscriptionDetails> subscriptionstore = HttpRuntime.Cache.Get("subscription_store") as Dictionary<string, SubscriptionDetails>;
var subscriptionstore = objCache.Get("subscription_store") as Dictionary<string, SubscriptionDetails>;
subscriptionstore.Add(subscriptionDetails.SubscriptionId, subscriptionDetails);
}
}

public SubscriptionDetails GetSubscriptionInfo(string subscriptionId)
{
Dictionary<string, SubscriptionDetails> subscriptionstore = HttpRuntime.Cache.Get("subscription_store") as Dictionary<string, SubscriptionDetails>;
var subscriptionstore = objCache.Get("subscription_store") as Dictionary<string, SubscriptionDetails>;
return subscriptionstore[subscriptionId];
}

public Dictionary<string, SubscriptionDetails> DeleteAllSubscriptions()
{
return HttpRuntime.Cache.Remove("subscription_store") as Dictionary<string, SubscriptionDetails>;

return objCache.Remove("subscription_store") as Dictionary<string, SubscriptionDetails>;
}
}
}
35 changes: 1 addition & 34 deletions GraphWebhooks/Helpers/SubscriptionHelper.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -50,33 +44,6 @@ internal static async Task<Subscription> 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<Subscription>(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;
}


Expand Down

0 comments on commit 76e90c8

Please sign in to comment.