Skip to content

Instantly share code, notes, and snippets.

@andrewdavey
Forked from davidfowl/MessageStoreUse.cs
Created September 10, 2011 14:12
Show Gist options
  • Save andrewdavey/1208346 to your computer and use it in GitHub Desktop.
Save andrewdavey/1208346 to your computer and use it in GitHub Desktop.
public class Global : System.Web.HttpApplication {
protected void Application_Start(object sender, EventArgs e) {
var store = new SmartMessageStore();
DependencyResolver.Register(typeof(IMessageStore), () => store);
}
}
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