Skip to content

Instantly share code, notes, and snippets.

@tomliversidge
Created April 21, 2017 06:50
Show Gist options
  • Save tomliversidge/0258c5870cb1b663fe168e67db7b2087 to your computer and use it in GitHub Desktop.
Save tomliversidge/0258c5870cb1b663fe168e67db7b2087 to your computer and use it in GitHub Desktop.
example strategies
public interface ISnapshotStrategy
{
bool ShouldTakeSnapshot(long index);
}
public class EventsCountStrategy : ISnapshotStrategy
{
private readonly int _eventsPerSnapshot;
public EventsCountStrategy(int eventsPerSnapshot)
{
_eventsPerSnapshot = eventsPerSnapshot;
}
public bool ShouldTakeSnapshot(long index)
{
return index % _eventsPerSnapshot == 0;
}
}
public class NoSnapshots : ISnapshotStrategy {
public bool ShouldTakeSnapshot(long index)
{
return false;
}
}
public class TimeStrategy : ISnapshotStrategy
{
private readonly TimeSpan _interval;
private readonly Func<DateTime> _getNow;
private DateTime _lastTaken = DateTime.MinValue;
public TimeStrategy(TimeSpan interval, Func<DateTime> getNow = null)
{
_interval = interval;
_getNow = getNow ?? (() => DateTime.Now);
}
public bool ShouldTakeSnapshot(long index)
{
return _lastTaken.Add(_interval) >= _getNow();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment