Skip to content

Instantly share code, notes, and snippets.

@JamieDixon
Created January 7, 2014 10:14
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 JamieDixon/8297357 to your computer and use it in GitHub Desktop.
Save JamieDixon/8297357 to your computer and use it in GitHub Desktop.
A collection that returns items added in the last 60 seconds
void Main()
{
var timeStorage = new TimeStorage();
var item = "Hello World";
timeStorage.Add(item);
var items = timeStorage.GetItems();
items.Dump();
}
public class TimeStorage
{
private BindingList<TimeStorageItem> items;
private const int TimeoutMilliseconds = 60000;
public TimeStorage()
{
items = new BindingList<TimeStorageItem>();
items.ListChanged += (object sender, ListChangedEventArgs e) => {
var item = (BindingList<TimeStorageItem>)sender;
// Decide when and if to do a cleanup
};
}
public void Add(object item)
{
items.Add(new TimeStorageItem(item));
}
public IEnumerable<TimeStorageItem> GetItems()
{
return items
.Where(x => DateTime.Now.Subtract(x.AddedOn).TotalMilliseconds < TimeoutMilliseconds);
}
}
public class TimeStorageItem
{
public DateTime AddedOn{get; private set;}
public object Item{get; private set; }
public TimeStorageItem(object item)
{
this.Item = item;
this.AddedOn = DateTime.Now;
}
}
// Define other methods and classes here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment