https://github.com/simpleton/yasp
Yet Another Shared Preference
https://github.com/simpleton/yasp
android kvstore sharedpreferences
Last synced: about 1 year ago
JSON representation
Yet Another Shared Preference
- Host: GitHub
- URL: https://github.com/simpleton/yasp
- Owner: simpleton
- License: apache-2.0
- Created: 2018-03-31T22:41:27.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-05-06T23:34:08.000Z (about 8 years ago)
- Last Synced: 2025-03-30T10:11:12.531Z (about 1 year ago)
- Topics: android, kvstore, sharedpreferences
- Language: Java
- Homepage:
- Size: 563 KB
- Stars: 16
- Watchers: 4
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Yet Another SharedPreference
[  ](https://bintray.com/simsun/maven/yasp-leveldb/_latestVersion)
[](https://travis-ci.org/simpleton/Yasp)
This library try to introduce a high performance K-V store in Android development to instead of [SharedPreference](https://developer.android.com/reference/android/content/SharedPreferences.html).
## Why
If you only need to persist simple values and your application runs in a single process SharedPreferences is probably enough for you. It is a good default option.
There are some situations where SharedPreferences are not suitable for store KV data:
1. Performance: Your data is complex or there is a lot of it
2. Multiple thread accessing the data: invoke `editor.appy()` or `editor.commit()` multiple time, even [`apply`](http://aosp.opersys.com/xref/android-8.1.0_r18/xref/frameworks/base/core/java/android/app/SharedPreferencesImpl.java#410) will submit work to other thread.
3. Multiple processes accessing the data: You have widgets or remote services that run in their own processes and require synchronized data
## Yasp-leveldb
This library is wrapped [LevelDB in java](https://github.com/dain/leveldb) with [SharedPreference](https://developer.android.com/reference/android/content/SharedPreferences.html) interface.
### Download
**Use Gradle:**
```gradle
implementation 'com.simsun.yasp:yasp-leveldb:0.0.3'
```
### API Usage
Almost compat with Android [SharedPreference](https://developer.android.com/reference/android/content/SharedPreferences.html), but there is a little different during initializing.
Yasp will **NOT** load whole file when you access one parameter. Feel free to cache yasp instance in your Application.
#### Init
```java
SharedPreferences sp = YASPContext.with(Context ct).getSharedPreferences(String name, int mode);
```
**Do NOT support multiple processes currently**
#### Get
As same as Android [SharedPreference](https://developer.android.com/reference/android/content/SharedPreferences.html)
```java
sp.getString(String key, String defaultVal);
```
#### Editor
As same as Android [SharedPreference](https://developer.android.com/reference/android/content/SharedPreferences.html)
```java
sp.edit()
.putString(String key, String value)
.putInt(String key, int value)
.apply();
```
## Benchmark
// TODO
## Reference
1. [Best practices in Android development](https://github.com/futurice/android-best-practices#data-storage)
2. [请不要滥用SharedPreference](http://weishu.me/2016/10/13/sharedpreference-advices/)