A feasible caching package for Flutter. Save any data encrypted.
Inspired by CacheX for Android
Caching is just a simple key-value pair data saving procedure. CacheX follows the same approach. CacheX uses SharedPreference as storage for caching data. Since we really can't just save the original data because of security issues. CacheX uses AES encryption & decryption behind the scene when you are caching data or fetching data from the cache. |
---|
Screenshot from the Example Project after using CacheX
First you have to add the package dependency in pubspec.yml
cache_x: ^latest
Get the version from pub.dev
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// Initialize password
// Passowrd length must be 128/192/256 bits
// you can use the password of 16 character,24 character or 32 character.
String password = 'CBoaDQIQAgceGg8dFAkMDBEOECEZCxg=';
// Initialize CacheX with the password
await CacheXCore().init(password: password, debug: false);
// Do your apps essential works
// Then anywhere in your app use it to save string
await CacheXCore.instance.saveString(key: 'ft1', value: 'Tanvir');
// Get String
await CacheXCore.instance.getString(key: 'ft1');
}
Also you can save the instance in a variable then use the variable to save or get any data.
// CacheXCore type of variable cacheX
final cacheX = CacheXCore();
// Init the CacheX
await cacheX.init(password: password);
// Save bool
cacheX.saveBool(key: 'dark', value: true);
// Get bool
cacheX.getBool(
key: 'dark',
defaultValue: false,
);
// Get File from storage
File newFile = File('file/path/of/your/file/newFile.jpeg');
// Save file
cacheX.saveFile(bytes: newFile.readAsBytesSync(), key: "file");
//get File
Uint8List? file1 = cacheX.getFile(key: 'file');
//convart to image
Image image = Image.memory(file1);
⚠ WARNING: If you forget or lost the specified password all the data encrypted with the password will be unusable. |
---|
If you are using parse_server_sdk_flutter in your project you can use CacheX as default coreStore.
Once you have the library added to your project, when you initialize parse_server_sdk_flutter add the CoreStoreCacheXImpl in coreStore...
import 'package:cache_x/core_store_cacheX_impl.dart';
await Parse().initialize(
appId,
serverUrl,
clientKey: clientKey,
liveQueryUrl: serverUrl,
coreStore: await CoreStoreCacheXImpl.getInstance(
password: 'u0TCHVOIiEFVwOqqDo1OnLfd3Kx7yaRt',
),
debug: true,
autoSendSessionId: true,
);
N.B If you are using CacheX as default coreStore and already initialized parse with CoreStoreCacheXImpl
then you don't have to initialize CacheX again to use CacheX as local storage. You can start saving and getting data.
// Then anywhere in your app use it to save
await CacheXCore.instance.saveString(key: 'ft1', value: 'Tanvir');
// Get
await CacheXCore.instance.getString(key: 'ft1');
➣ saveBool(String key, bool value)
➣ saveDouble(String key, double value)
➣ saveInt(String key, int value)
➣ saveString(String key, String value)
➣ saveStringList(String key, List<String> value)
➣ saveFile(String key, List<int> bytes)
➣ getBool(String key)
➣ getDouble(String key)
➣ getInt(String key)
➣ getString(String key)
➣ getStringList(String key)
➣ getKeys()
➣ containsKey(String key)
➣ getFile(String key)
➣ remove(String key)
➣ clear()
- The password must be 128/192/256 bits. It means the password only can be
16
character,24
character or32
character in length. Otherwise it will throw exception. - Provide
defaultValue
in the get methods or else the default value will be null. If the key is not found or the data is not the right type it return thedefaultValue
. - All the
save
methods will returnFuture<bool>
so to get the value you have to useawait
- It will throw exception if there any.
- CacheXException
- If there any exception in the CacheXCore or the CacheXCore is not initialized properly it will throw
CacheXException
exception. The exception message starts withCacheXException:
.
- If there any exception in the CacheXCore or the CacheXCore is not initialized properly it will throw
- StorageException
- If there any exception saving or getting data it will throw
StorageException
. The exception message starts with StringStorageException:
.
- If there any exception saving or getting data it will throw
- EncryptionException
- If there any exception encrypting or decrypting the data it will throw
EncryptionException
. The exception message will start with StringEncryptionException:
.
- If there any exception encrypting or decrypting the data it will throw