-
Notifications
You must be signed in to change notification settings - Fork 16
Update RequestCache
#112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
happy-san
wants to merge
13
commits into
master
Choose a base branch
from
lru_cache
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Update RequestCache
#112
Changes from 6 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
47f9cba
Update tests
happy-san 886f995
revert typo
happy-san a85a83d
Add test for LRU eviction.
happy-san bb8c34b
add: tests for LRU (harisarang#1)
harisarang 4eaef8a
add: lru cache
harisarang 85aeeca
add: cache (#113)
harisarang ec4aecd
update: type `dynamic` to `String`
harisarang ce4176c
update: `Cache` types
harisarang 37f7563
Update ApiCall to match getResponse
happy-san 7995247
refactor: `send` inside `getResponse()`
harisarang f6272e8
refactor: `send` inside `getResponse()`
harisarang ea8bd06
update: apicall
harisarang c771725
add: `_cacheTimestamp`
harisarang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,33 @@ | ||
import 'dart:collection'; | ||
import 'package:dcache/dcache.dart'; | ||
import 'package:http/http.dart'; | ||
|
||
import 'package:http/http.dart' as http; | ||
|
||
import '../models/node.dart'; | ||
import 'typedefs.dart' as defs; | ||
|
||
/// Cache store which uses a [HashMap] internally to serve requests. | ||
class RequestCache { | ||
final _cachedResponses = HashMap<int, _Cache>(); | ||
Cache _cachedResponses; | ||
final Duration timeToUse; | ||
final int size; | ||
final defs.Send<Map<String, dynamic>> send; | ||
|
||
RequestCache(this.size, this.timeToUse, this.send) { | ||
_cachedResponses = LruCache<dynamic, Response>(storage: InMemoryStorage(size)); | ||
} | ||
|
||
// TODO(harisarang): rename this function to getResponse | ||
/// Caches the response of the [request], identified by [key]. The cached | ||
/// response is valid till [cacheTTL]. | ||
Future<Map<String, dynamic>> cache( | ||
Future<Map<String, dynamic>> getResponse( | ||
int key, | ||
harisarang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Future<Map<String, dynamic>> Function(Future<http.Response> Function(Node)) | ||
send, | ||
Future<http.Response> Function(Node) request, | ||
Duration cacheTTL, | ||
defs.Request request, | ||
) async { | ||
if (_cachedResponses.containsKey(key)) { | ||
if (_isCacheValid(_cachedResponses[key], cacheTTL)) { | ||
harisarang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Cache entry is still valid, return it | ||
return Future.value(_cachedResponses[key].data); | ||
} else { | ||
// Cache entry has expired, so delete it explicitly | ||
_cachedResponses.remove(key); | ||
} | ||
return send(_cachedResponses.get(key)); | ||
harisarang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
final response = await send(request); | ||
_cachedResponses[key] = _Cache(response, DateTime.now()); | ||
var response = await send(request); | ||
_cachedResponses.set(key, response); | ||
return response; | ||
} | ||
|
||
bool _isCacheValid(_Cache cache, Duration cacheTTL) => | ||
DateTime.now().difference(cache.creationTime) < cacheTTL; | ||
} | ||
|
||
class _Cache { | ||
final DateTime creationTime; | ||
final Map<String, dynamic> data; | ||
|
||
const _Cache(this.data, this.creationTime); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import 'package:http/http.dart' as http; | ||
|
||
import '../models/node.dart'; | ||
|
||
typedef Request = Future<http.Response> Function(Node); | ||
typedef Send<R> = Future<R> Function(Request); |
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
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.