-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from polRk/main
Add ready for use RRule instance with caching
- Loading branch information
Showing
5 changed files
with
232 additions
and
2 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import 'package:meta/meta.dart'; | ||
|
||
import 'utils.dart'; | ||
|
||
@immutable | ||
class CacheKey { | ||
const CacheKey({ | ||
required this.start, | ||
this.after, | ||
this.includeAfter = false, | ||
this.before, | ||
this.includeBefore = false, | ||
}); | ||
|
||
final DateTime start; | ||
final DateTime? after; | ||
final bool includeAfter; | ||
final DateTime? before; | ||
final bool includeBefore; | ||
|
||
@override | ||
int get hashCode { | ||
return hashList([ | ||
start, | ||
after, | ||
includeAfter, | ||
before, | ||
includeBefore, | ||
]); | ||
} | ||
|
||
@override | ||
bool operator ==(Object other) { | ||
if (identical(this, other)) return true; | ||
if (other.runtimeType != runtimeType) return false; | ||
|
||
return other is CacheKey && | ||
other.after == after && | ||
other.includeAfter == includeAfter && | ||
other.before == before && | ||
other.includeBefore == includeBefore; | ||
} | ||
} | ||
|
||
class Cache { | ||
final Map<CacheKey, List<DateTime>> _results = {}; | ||
|
||
@visibleForTesting | ||
Map<CacheKey, List<DateTime>> get results => _results; | ||
|
||
void add(CacheKey key, List<DateTime> data) { | ||
_results[key]= data; | ||
} | ||
|
||
List<DateTime>? get(CacheKey key) { | ||
return _results[key]; | ||
} | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import 'package:rrule/src/cache.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
test('should add data to the cache', () { | ||
final cache = Cache(); | ||
final key = CacheKey(start: DateTime.now()); | ||
final results = <DateTime>[]; | ||
cache.add(key, results); | ||
|
||
expect(cache.results, containsPair(key, results)); | ||
}); | ||
|
||
test('should return data from the cache by key', () { | ||
final cache = Cache(); | ||
final key = CacheKey(start: DateTime.now()); | ||
final results = <DateTime>[]; | ||
cache.add(key, results); | ||
|
||
expect(cache.get(key), results); | ||
}); | ||
} |
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