-
-
Save andrewdavey/1208346 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Global : System.Web.HttpApplication { | |
protected void Application_Start(object sender, EventArgs e) { | |
var store = new SmartMessageStore(); | |
DependencyResolver.Register(typeof(IMessageStore), () => store); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SmartMessageStore : IMessageStore | |
{ | |
private readonly IMessageStore _store = new InProcessMessageStore(); | |
public Task<IEnumerable<Message>> GetAll(string key) | |
{ | |
lock (_store) | |
{ | |
return _store.GetAll(key); | |
} | |
} | |
public Task<IEnumerable<Message>> GetAllSince(string key, long id) | |
{ | |
return GetLastId().ContinueWith(t => | |
{ | |
lock (_store) | |
{ | |
if (t.Result.HasValue) | |
{ | |
if (id > t.Result.Value) | |
{ | |
var recent = _store.GetAllSince(key, t.Result.Value - 1); | |
var idOffset = id - t.Result.Value - 1; | |
return recent.ContinueWith(messagesTask => | |
messagesTask.Result.Select(m => OffsetMessageId(m, idOffset)) | |
); | |
} | |
} | |
return _store.GetAllSince(key, id); | |
} | |
}).Unwrap(); | |
} | |
Message OffsetMessageId(Message message, long idOffset) | |
{ | |
return new Message(message.SignalKey, message.Id + idOffset, message.Value, message.Created); | |
} | |
public Task<long?> GetLastId() | |
{ | |
lock (_store) | |
{ | |
return _store.GetLastId(); | |
} | |
} | |
public Task Save(string key, object value) | |
{ | |
lock (_store) | |
{ | |
return _store.Save(key, value); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment