Closed salahamassi closed 2 years ago
[X] get from cache
[X] save to cache
[x] store If Not Present
[x] storing Items Forever
[x] check cache
[X] shared prefs cache driver
[x] cache driver config
[x] update auth module with new cache class
[x] remeber & rember forever
[x] removing Items From The Cache
[x] increment & decrement
[x] retrieve & Delete
[x] clear the entire cache
[x] accessing Multiple Cache Stores
set default cache driver
class CacheConfig { static var defaultStore = 'shared_preference'; static var stores = { 'shared_preference': { 'driver': 'shared_preference', 'class': SharedPreferencesCacheDriver, }, } ; }
store user id:
Cache.put('userId', 334);
store user id for 10 days:
Cache.put('userId', 334, expiredAfter: const Duration(days: 10));
store If Not Present for 10 minutes:
Cache.add('quantity', 32, expiredAfter: const Duration(minutes: 10));
storing Items Forever:
Cache.forever('userId', 32);
check user id:
final bool hasUser = await Cache.has('userId');
use raw type as a default value:
final int userId = await Cache.get('userId', 0);
use function as a fallback:
final int userId = await Cache.get('userId', () async { return api.getUser().id; });
store custom object (the object must have toJson method otherwise jsonEncode will throw UnsupportedError):
toJson
jsonEncode
UnsupportedError
Cache.put('war', War(id: 3, name: 'russia'));
retrieve custom object (you should pass the from JSON factory,):
final War warObject = await Cache.get( 'war', fromJsonFactory: War.fromJson, );
retrieve custom object as a map:
final Map<String, dynamic> warMap = await Cache.get('war');
removing Items From The Cache:
Cache.forget('war');
remember for a custom duration:
Cache.remember('user_profile', const Duration(hours: 24), () { final ApiClient apiClient = sl<ApiClient>(); return apiClient.get(Api.profile(), headers: Api.headers()); });
remember forever:
Cache.rememberForever('user_profile', () { final ApiClient apiClient = sl<ApiClient>(); return apiClient.get(Api.profile(), headers: Api.headers()); });
increment & decrement values:
await Cache.put('quantity', 1); await Cache.increment('quantity'); // quantity = 2 await Cache.increment('quantity', 20); // quantity = 22 await Cache.decrement('quantity'); // quantity = 21 await Cache.decrement('quantity', 20); // quantity = 1
retrieve & Delete:
final int quantity = await Cache.pull('quantity');
clear the entire cache:
Cache.flush();
accessing Multiple Cache Stores:
await Cache.store('in_memory').put('user_id', 32); final int userId = await Cache.store('in_memory').get('user_id');
[X] get from cache
[X] save to cache
[x] store If Not Present
[x] storing Items Forever
[x] check cache
[X] shared prefs cache driver
[x] cache driver config
[x] update auth module with new cache class
[x] remeber & rember forever
[x] removing Items From The Cache
[x] increment & decrement
[x] retrieve & Delete
[x] clear the entire cache
[x] accessing Multiple Cache Stores
set default cache driver
store user id:
store user id for 10 days:
store If Not Present for 10 minutes:
storing Items Forever:
check user id:
use raw type as a default value:
use function as a fallback:
store custom object (the object must have
toJson
method otherwisejsonEncode
will throwUnsupportedError
):retrieve custom object (you should pass the from JSON factory,):
retrieve custom object as a map:
removing Items From The Cache:
remember for a custom duration:
remember forever:
increment & decrement values:
retrieve & Delete:
clear the entire cache:
accessing Multiple Cache Stores: