Created
January 7, 2014 10:14
-
-
Save JamieDixon/8297357 to your computer and use it in GitHub Desktop.
A collection that returns items added in the last 60 seconds
This file contains 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
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