0 purchases
dart basic cache
DartBasicCache #
DartBasicCache is a simple caching package for Dart that allows you to easily cache data in your Dart applications.
Installation #
Add the following line to your pubspec.yaml file:
dependencies:
dart_basic_cache: ^1.0.0
copied to clipboard
Features #
A simple and easy-to-use caching package.
Caches and stores data based on unique URLs.
Automatically invalidates cached data after a specified duration.
Provides fast access to cached data, avoiding redundant network requests.
Supports optional caching to memory and/or file storage.
Suitable for usage in both Flutter and Dart projects.
Usage #
Import the dart_basic_cache library:
import 'package:dart_basic_cache/dart_basic_cache.dart';
copied to clipboard
Create an instance of DartBasicCache:
var cache = DartBasicCache();
copied to clipboard
Initialize the cache (Optional):
await cache.init(cacheFileName: 'my_cache', cacheTimeHour: 2);
copied to clipboard
This will initialize the cache with a file name of "my_cache" and a cache duration of 2 hours.
Cache data:
await cache.cacheData('https://api.example.com/data', jsonData);
copied to clipboard
This will cache the jsonData with the specified URL as the unique identifier.
Retrieve cached data:
var data = await cache.getDataFromCache('https://api.example.com/data');
if (data != null) {
// Use the cached data
} else {
// Data not found in cache
}
copied to clipboard
This will retrieve the cached data for the specified URL. If the data is available and not expired, it will be returned. Otherwise, it will return null.
Delete cached data:
await cache.deleteCachedData('https://api.example.com/data');
copied to clipboard
This will delete the cached data for the specified URL.
Example #
Here's a complete example of using DartBasicCache to cache and retrieve data from an API:
import 'package:flutter/material.dart';
import 'package:dart_basic_cache/dart_basic_cache.dart';
import 'package:http/http.dart' as http;
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await DartBasicCache().init();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'DartBasicCache Example',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final cache = DartBasicCache();
final apiUrl = 'https://api.example.com/data';
String? cachedData;
@override
void initState() {
super.initState();
fetchData();
}
Future<void> fetchData() async {
setState(() {
cachedData = null;
});
final data = await cache.getDataFromCache(apiUrl);
if (data != null) {
setState(() {
cachedData = data;
});
} else {
final response = await http.get(Uri.parse(apiUrl));
if (response.statusCode == 200) {
final fetchedData = response.body;
await cache.cacheData(apiUrl, fetchedData);
setState(() {
cachedData = fetchedData;
});
}
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('DartBasicCache Example'),
),
body: Center(
child: cachedData != null
? Text(
'Cached Data:\n$cachedData',
textAlign: TextAlign.center,
)
: CircularProgressIndicator(),
),
);
}
}
copied to clipboard
Buy Me A Coffee ☕️ #
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.