Created
April 21, 2017 06:50
-
-
Save tomliversidge/0258c5870cb1b663fe168e67db7b2087 to your computer and use it in GitHub Desktop.
example strategies
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 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