Skip to content
3 changes: 3 additions & 0 deletions example/fetch_tray_cache_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'dart:io';

import 'package:fetch_tray/fetch_tray.dart';
import 'package:fetch_tray_cache_plugin/fetch_tray_cache.dart';
import 'package:logger/logger.dart';

import 'models/post.dart';
import 'requests/get_all_posts_request.dart';
Expand All @@ -15,6 +16,8 @@ void main() async {
TrayCachePlugin(
cacheStoreType: TrayCacheStoreType.memory,
cacheDuration: const Duration(seconds: 2),
logLevel: Level.debug,
// cacheDirectory: (await getTemporaryDirectory()).path,
),
],
);
Expand Down
6 changes: 5 additions & 1 deletion lib/src/fetch_tray_cache_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import 'package:dio/dio.dart';
import 'package:dio_cache_interceptor/dio_cache_interceptor.dart';
import 'package:dio_cache_interceptor_hive_store/dio_cache_interceptor_hive_store.dart';
import 'package:fetch_tray/fetch_tray.dart';
import 'package:logger/logger.dart';

import '../fetch_tray_cache.dart';
import 'interceptors/cache_interceptor.dart';

/// Type of cache store
enum TrayCacheStoreType {
Expand All @@ -29,11 +29,14 @@ class TrayCachePluginKeys {
class TrayCachePlugin implements TrayPlugin {
final TrayCacheStoreType cacheStoreType;
final Duration cacheDuration;

final Level logLevel;
late final CacheStore store;

TrayCachePlugin({
this.cacheStoreType = TrayCacheStoreType.memory,
this.cacheDuration = const Duration(days: 7),
this.logLevel = Level.error,
String? cacheDirectory,
}) {
switch (cacheStoreType) {
Expand All @@ -51,6 +54,7 @@ class TrayCachePlugin implements TrayPlugin {
TrayCacheInterceptor(
cacheOptions: cacheOptions,
maxAge: cacheDuration,
logLevel: logLevel,
),
DioCacheInterceptor(
options: cacheOptions,
Expand Down
31 changes: 19 additions & 12 deletions lib/src/interceptors/cache_interceptor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,24 @@ import 'package:logger/logger.dart';
class TrayCacheInterceptor extends Interceptor {
final Duration maxAge;
final CacheOptions cacheOptions;
final Logger logger = Logger(
printer: PrettyPrinter(
methodCount: 0,
printTime: true,
),
);
final Level logLevel;

get logger => Logger(
printer: PrettyPrinter(
methodCount: 0,
printTime: true,
),
level: logLevel);

TrayCacheInterceptor({
required this.cacheOptions,
required this.maxAge,
this.logLevel = Level.error,
});

@override
void onRequest(
RequestOptions options,
RequestInterceptorHandler handler,
) async {
RequestOptions options, RequestInterceptorHandler handler) async {
// TODO: implement filter for logger
/* final requestShouldLog =
options.extra[TrayCachePluginKeys.requestShouldLog] as bool? ?? false; */
Expand All @@ -61,21 +62,27 @@ class TrayCacheInterceptor extends Interceptor {
final cacheDuration = requestCacheDuration ?? maxAge;

if (difference <= cacheDuration) {
logger.i('Cache hit, returning cached response', logPrefix);
logger.i(
'$logPrefix Cache hit, returning cached response',
);
return handler.resolve(
cache.toResponse(
options,
fromNetwork: false,
),
);
} else {
logger.i('Cache too old, deleting...', logPrefix);
logger.i(
'$logPrefix Cache too old, deleting...',
);
await store.delete(key);
}
}
}

logger.i('Sending request over network', logPrefix);
logger.i(
'$logPrefix Sending request over network',
);

handler.next(options);
}
Expand Down
14 changes: 6 additions & 8 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
name: fetch_tray_cache_plugin
description: A caching plugin for fetch_tray.
version: 0.0.2
version: 0.0.4
publish_to: none
# repository: https://github.com/my_org/my_repo

environment:
sdk: ^3.0.2
sdk: ">=3.0.0 <4.0.0"

# Add regular dependencies here.
dependencies:
fetch_tray:
git:
url: https://github.com/marqably/fetch_tray
ref: next
fetch_tray: ^0.3.3
dio: ^5.2.1+1
dio_cache_interceptor: ^3.4.2
dio_cache_interceptor_hive_store: ^3.2.1
logger: ^1.4.0
logger: ^2.0.2+1

dev_dependencies:
lints: ^2.0.0
lints: ^3.0.0
test: ^1.21.0