Skip to content

Instantly share code, notes, and snippets.

@a-h
Created April 19, 2015 14:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save a-h/3660a6729c3450b38731 to your computer and use it in GitHub Desktop.
Save a-h/3660a6729c3450b38731 to your computer and use it in GitHub Desktop.
Caching Data in a SignalR Hub and Limiting Publisher Rights by Using a Password
namespace SparkLines.Hubs
{
public class SparklinesHub : Hub
{
static ConcurrentDictionary<string, List<int>> Values = new ConcurrentDictionary<string, List<int>>();
static int maximumValues = 49;
public void PublishMetric(string name, int value)
{
if (IsAuthorised())
{
// Update the dictionary with new data.
Values.AddOrUpdate(name, new List<int>() { value }, (k, v) =>
{
int skip = v.Count > maximumValues ? v.Count - maximumValues : 0;
return v.Skip(skip).Concat(new int[] { value }).ToList();
});
var valuesToSend = new List<int>() { };
Values.TryGetValue(name, out valuesToSend);
// Publish the metrics data to connected clients.
Clients.All.metricsUpdated(name, valuesToSend);
}
}
private bool IsAuthorised()
{
return base.Context.Headers["PublisherKey"] == "xy123123";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment