Skip to content

Instantly share code, notes, and snippets.

@acarolsf
Last active December 26, 2023 04:16
Show Gist options
  • Save acarolsf/056aa8afce517e26f49e5a6e2cc8fdd8 to your computer and use it in GitHub Desktop.
Save acarolsf/056aa8afce517e26f49e5a6e2cc8fdd8 to your computer and use it in GitHub Desktop.
Cache Manager using Flutter
import 'package:get_storage/get_storage.dart';
class CacheValue {
dynamic value;
DateTime? lastSaved;
CacheValue({required this.value, this.lastSaved});
Map<String, dynamic> toJson() => {
'value': value,
'lastSaved': lastSaved.toString()
};
CacheValue.fromJson(Map<String, dynamic> json) :
value = json['value'],
lastSaved = DateTime.parse(json['lastSaved'].toString());
}
mixin CacheManager {
// Save values locally
Future<bool> saveValue(dynamic value, CacheManagerKey onKey) async {
final box = GetStorage();
final valueToCache = CacheValue(value: value, lastSaved: DateTime.now());
await box.write(onKey.toString(), valueToCache.toJson());
return true;
}
// Read value by key
CacheValue? getValueFrom(CacheManagerKey key) {
final box = GetStorage();
final savedValue = box.read(key.toString());
return savedValue == null ? null : CacheValue.fromJson(savedValue);
}
// Remove value by key
Future<void> removeValueFrom(CacheManagerKey key) async {
final box = GetStorage();
await box.remove(key.toString());
}
// Remove all values saved locally
Future<void> removeAllCacheData() async {
final box = GetStorage();
await box.erase();
}
// Validate based on expiration time
bool isValid(ExpirationTime expiration, int time, DateTime lastTime) {
switch (expiration) {
case ExpirationTime.inDays:
return (lastTime.difference(DateTime.now()).inDays > time);
case ExpirationTime.inHours:
return (lastTime.difference(DateTime.now()).inHours > time);
case ExpirationTime.inSeconds:
return (lastTime.difference(DateTime.now()).inSeconds > time);
}
}
}
enum ExpirationTime {
inDays,
inHours,
inSeconds;
}
enum CacheManagerKey {
userToken,
userData
}
@acarolsf
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment