Skip to content

Commit

Permalink
♻️ Refactor caching to AlfredWorkflow
Browse files Browse the repository at this point in the history
  • Loading branch information
techouse committed Mar 13, 2022
1 parent fddde31 commit 5541f12
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 72 deletions.
60 changes: 5 additions & 55 deletions bin/main.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'dart:io' show Platform, exitCode, stdout;
import 'dart:io' show exitCode, stdout;

import 'package:alfred_workflow/alfred_workflow.dart'
show
Expand All @@ -11,44 +11,15 @@ import 'package:algolia/algolia.dart' show AlgoliaQuerySnapshot;
import 'package:args/args.dart' show ArgParser, ArgResults;
import 'package:collection/collection.dart' show IterableExtension;
import 'package:html_unescape/html_unescape.dart' show HtmlUnescape;
import 'package:path/path.dart' show dirname;
import 'package:stash/stash_api.dart'
show
Cache,
CacheEntryCreatedEvent,
CacheEntryEvictedEvent,
CacheEntryExpiredEvent,
CacheEntryRemovedEvent,
CacheEntryUpdatedEvent,
CreatedExpiryPolicy,
EventListenerMode,
LruEvictionPolicy,
CacheExtension;
import 'package:stash_file/stash_file.dart'
show FileCacheStore, newFileLocalCacheStore;

import 'src/constants/config.dart' show Config;
import 'src/extensions/string_helpers.dart' show StringHelpers;
import 'src/models/search_result.dart' show SearchResult;
import 'src/services/algolia_search.dart' show AlgoliaSearch;

final HtmlUnescape unescape = HtmlUnescape();

final AlfredWorkflow workflow = AlfredWorkflow();

final FileCacheStore store = newFileLocalCacheStore(
path: dirname(Platform.script.toFilePath()),
fromEncodable: (Map<String, dynamic> json) => AlfredItems.fromJson(json),
);

final Cache cache = store.cache<AlfredItems>(
name: 'query_cache',
maxEntries: 10,
eventListenerMode: EventListenerMode.synchronous,
evictionPolicy: const LruEvictionPolicy(),
expiryPolicy: const CreatedExpiryPolicy(Duration(minutes: 1)),
);

bool verbose = false;

void main(List<String> arguments) async {
Expand All @@ -75,19 +46,13 @@ void main(List<String> arguments) async {
}
final String queryString = query.join(' ').trim();

if (verbose) {
stdout.writeln('Query: "$queryString"');
_cacheVerbosity();
}
if (verbose) stdout.writeln('Query: "$queryString"');

if (queryString.isEmpty) {
_showPlaceholder();
} else {
final AlfredItems? cachedItem =
await cache.get('${queryString}_${version}'.md5hex);
if (cachedItem != null) {
workflow.addItems(cachedItem.items);
} else {
workflow.cacheKey = '${queryString}_${version}';
if (await workflow.getItems() == null) {
await _performSearch(queryString, version: version);
}
}
Expand All @@ -103,20 +68,6 @@ void main(List<String> arguments) async {
}
}

void _cacheVerbosity() {
cache
..on<CacheEntryCreatedEvent<AlfredItems>>()
.listen((event) => print('Key "${event.entry.key}" added'))
..on<CacheEntryUpdatedEvent<AlfredItems>>()
.listen((event) => print('Key "${event.newEntry.key}" updated'))
..on<CacheEntryRemovedEvent<AlfredItems>>()
.listen((event) => print('Key "${event.entry.key}" removed'))
..on<CacheEntryExpiredEvent<AlfredItems>>()
.listen((event) => print('Key "${event.entry.key}" expired'))
..on<CacheEntryEvictedEvent<AlfredItems>>()
.listen((event) => print('Key "${event.entry.key}" evicted'));
}

void _showPlaceholder() {
workflow.addItem(
const AlfredItem(
Expand All @@ -134,7 +85,7 @@ Future<void> _performSearch(String query, {String? version}) async {

if (snapshot.nbHits > 0) {
final AlfredItems items = AlfredItems(
items: snapshot.hits
snapshot.hits
.map((snapshot) => SearchResult.fromJson(snapshot.data))
.map((result) {
final int level = int.tryParse(result.type.substring(3)) ?? 0;
Expand All @@ -158,7 +109,6 @@ Future<void> _performSearch(String query, {String? version}) async {
);
}).toList(),
);
cache.putIfAbsent('${query}_${version}'.md5hex, items);
workflow.addItems(items.items);
} else {
final Uri url =
Expand Down
7 changes: 0 additions & 7 deletions bin/src/extensions/string_helpers.dart

This file was deleted.

2 changes: 1 addition & 1 deletion info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
<key>variablesdontexport</key>
<array/>
<key>version</key>
<string>2.0.3</string>
<string>2.0.4</string>
<key>webaddress</key>
<string>https://github.com/techouse</string>
</dict>
Expand Down
10 changes: 5 additions & 5 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ packages:
description:
path: "."
ref: master
resolved-ref: f94dac49b7cbae5a73590f6db5772edd5defdba2
resolved-ref: ce134bbe1e10a8313f251b2c6ca97512d98dcb2d
url: "git@github.com:techouse/alfred_workflow.git"
source: git
version: "0.0.2"
version: "0.0.3"
algolia:
dependency: "direct main"
description:
Expand Down Expand Up @@ -88,7 +88,7 @@ packages:
source: hosted
version: "3.0.1"
crypto:
dependency: "direct main"
dependency: transitive
description:
name: crypto
url: "https://pub.dartlang.org"
Expand Down Expand Up @@ -193,7 +193,7 @@ packages:
source: hosted
version: "2.0.2"
path:
dependency: "direct main"
dependency: transitive
description:
name: path
url: "https://pub.dartlang.org"
Expand Down Expand Up @@ -221,7 +221,7 @@ packages:
source: hosted
version: "4.0.1"
stash_file:
dependency: "direct main"
dependency: transitive
description:
name: stash_file
url: "https://pub.dartlang.org"
Expand Down
3 changes: 0 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ dependencies:
url: git@github.com:techouse/alfred_workflow.git
ref: master
collection: ^1.16.0
stash_file: ^4.0.1
path: ^1.8.1
crypto: ^3.0.1

dev_dependencies:
lints: ^1.0.0
Expand Down
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.0.3
2.0.4

0 comments on commit 5541f12

Please sign in to comment.