tiny_storage 1.2.0 tiny_storage: ^1.2.0 copied to clipboard
This is a simple key-value store based on JSON file. Parallel processing of file I/O using `Isolate`.
tiny_storage #
tiny_storage
is a simple key-value store based on JSON files.
It is also a very small library, so it is easy for anyone to understand how it works.
Features #
- Key-Value Store
- Output as JSON file
- Fast
- Parallel processing of file I/O using
Isolate
. - Multiple write operations are not executed in the same event loop.
- Parallel processing of file I/O using
- Guaranteed write order, even for large data.
Getting started #
import 'package:tiny_storage/tiny_storage.dart';
void main() async {
final storage = await TinyStorage.init('test.txt', path: './tmp');
storage.set('key_1', 'value_1');
storage.set('key_2', 2);
storage.set('key_3', [1, 2, 3]);
final ret = storage.get('key_1');
print(ret);
await storage.dispose();
}
Usage #
Initialize #
Specify the file name to save and initialize.
If the file exists, the loading process will run.
In the case of Flutter, it is also necessary to specify the destination path using path_provider.
final storage = await TinyStorage.init('test.txt', path: './tmp');
If you do not want to increase the number of threads when opening multiple files, specify a TinyStorage object to be shared in union
. It will work on the same thread.
final storage = await TinyStorage.init('test1.txt', path: './tmp');
final storage2 = await TinyStorage.init('test2.txt', path: './tmp', union: storage);
Registration and Retrieval #
Registers and retrieves an Object using String as a key.
The value is immediately held in memory and written to disk before the next event loop.
storage.set('key_1', 'value_1');
final ret = storage.get('key_1');
Clear #
Discard all data and the file.
storage.clear();
Dispose #
Use dispose
to destroy it when it is no longer needed.
storage.dispose();
Note #
The web version has not been implemented yet.
tiny_locator #
Since tiny_storage
runs the file reading process at the time of init
,
When using it in multiple classes, it is recommended to share the instance using tiny_locator etc.
Future<void> A() async {
// Registration
final storage = await TinyStorage.init('test.txt', path: './tmp');
locator.add<TinyStorage>(() => storage);
}
void B() {
// Acquisition
final storage = locator.get<TinyStorage>();
final ret = storage.get('key_1');
print(ret);
}