flutter_cache는 Flutter 애플리케이션에서 공유 설정을 사용하는 것을 간편하게 만드는 가벼운 패키지입니다. 공유 설정 주위에 래퍼 역할을 하며, 앱 재시작 후에도 데이터를 지속적으로 저장하고 검색할 수 있는 사용하기 쉬운 API를 제공합니다. 단순성과 효율성을 고려해 설계되었으며, Android, iOS, Web, macOS, Windows, Linux 등 주요 플랫폼을 모두 지원합니다. 일반적인 캐싱 작업을 추상화하여 사용자 설정, 토큰, 캐시된 응답과 같은 소량의 지속 데이터 관리에 이상적입니다.
사용 사례
사용자 선호 사항 저장
API 응답 데이터 캐싱
로그인 토큰 유지
앱 상태 저장
구성 설정 관리
주요 기능
간단한 API
공유 설정 래퍼
크로스 플랫폼 지원
간편한 데이터 직렬화
가볍고 빠름
적합한 대상
초보자 플러터 개발자
기본적인 지속성을 필요로 하는 앱
소규모 데이터 저장
빠른 프로토타이핑
간단한 캐싱 요구 사항
고려 사항
대규모 데이터 세트에 적합하지 않습니다
키-값 저장에 제한됨
내장된 만기 메커니즘이 없음
공유된 선호도 성능에 의존합니다
복잡한 쿼리를 지원하지 않습니다
원본 README
연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.
flutter_cache
A simple cache package for flutter. This package is a wrapper for shared preference and makes working with shared preference easier. Once it has been installed, you can do these things.
import'package:flutter_cache/flutter_cache.dart'as cache;
// create new cache.
cache.remember('key', 'data');
cache.write('key', 'data');
// add Cache lifetime on create
cache.remember('key', 'data', 120);
cache.write('key', 'data', 120);
// load Cache by key
cache.load('key'); // This will load the cache data.// return `defaultValue` if key not exists
cache.load('key', 'defaultValue')
// destroy single cache by key
cache.destroy('key');
// destroy all cache
cache.clear();
Getting Started
Installation
First include the package dependency in your project's pubspec.yaml file
Cache String, Map, List<String> and List<Map> for forever or for a limited time.
Load the cache you've cached.
Clear All Cache.
Clear Single Cache.
Usage
Cache Fetch Data from API
If data already exist, then it will use the data in the cache. If it's not, It will fetch the data. You can also set Cache lifetime so your app would fetch again everytime the Cache dies.
import'dart:convert';
import'package:http/http.dart'as http;
import'package:flutter_cache/flutter_cache.dart'as cache;
var employees = await cache.remember('employees', () async {
var response = await http.get('http://dummy.restapiexample.com/api/v1/employees');
returnjsonDecode(response.body)['data'].cast<Map>();
}, 120); // cache for 2 mins// or // cache for 2 minsvar employees = await cache.remember(
'servers',
() async => jsonDecode(
(await http.get( 'http://dummy.restapiexample.com/api/v1/employees' )
).body )['data'].cast<Map>()
, 120);
Saved data for limited time
The data will be destroyed when it reached the time you set.
cache.remember('key', 'data', 120); // saved for 2 mins or 120 seconds
cache.write('key', 'data', 120);
Cache multipe datatype
You can cache multiple datatype. Supported datatype for now are String, Map, List<String> and List<Map>. When you use cache.load() to get back the data, it will return the data in the original datatype.
cache.remember('key', {
'name' : 'Ashraf Kamarudin',
'depth2' : {
'name' : 'depth2',
'depth3' : {
'name': 'depth3'
}
}
});
cache.load('key'); // will return data in map datatype.