An efficient key-value storage library.
416
stars
158
commits
Java
primary language
Jul 27, 2025
updated
FastKV是用Java编写的高效可靠的key-value存储库,专为Android平台优化。
dependencies {
implementation 'io.github.billywei01:fastkv:3.0.1'
}
备注:
在2.x版本,FastKV支持多进程存储,但3.x版本开始不再支持多进程。
如需多进程存储,请使用MPFastKV
// 可选:设置全局配置
FastKVConfig.setLogger(FastKVLogger)
FastKVConfig.setExecutor(Dispatchers.IO.asExecutor())
初始化可以按需设置日志接口和Executor:
// 使用Context构造(推荐)
FastKV kv = new FastKV.Builder(context, "user_data").build();
// 或使用自定义路径
FastKV kv = new FastKV.Builder(path, "user_data").build();
// 基本读写操作
if (!kv.getBoolean("first_launch")) {
kv.putBoolean("first_launch", true);
}
int count = kv.getInt("launch_count");
kv.putInt("launch_count", count + 1);
// 支持链式调用
kv.putString("user_name", "张三")
.putInt("user_age", 25)
.putFloat("user_score", 89.5f);
FastKV kv = new FastKV.Builder(context, "secure_data")
.encoder(new FastEncoder[]{CustomObjectEncoder.INSTANCE}) // 自定义编码器
.cipher(new AESCipher()) // 数据加密
.blocking() // 同步阻塞模式
.build();
// 1. 实现FastEncoder接口
public class UserEncoder implements FastEncoder<User> {
@Override
public String tag() {
return "User";
}
@Override
public byte[] encode(User user) {
// 序列化逻辑
return userToBytes(user);
}
@Override
public User decode(byte[] bytes, int offset, int length) {
// 反序列化逻辑
return bytesToUser(bytes, offset, length);
}
}
// 2. 注册编码器并使用
FastEncoder<?>[] encoders = {new UserEncoder()};
FastKV kv = new FastKV.Builder(context, "user_data")
.encoder(encoders)
.build();
// 3. 存储和读取对象
User user = new User("张三", 25);
kv.putObject("current_user", user, new UserEncoder());
User savedUser = kv.getObject("current_user");
推荐使用Packable框架进行对象序列化。
// 实现FastCipher接口
public class AESCipher implements FastCipher {
@Override
public byte[] encrypt(byte[] src) {
// 加密实现
return encryptWithAES(src);
}
@Override
public byte[] decrypt(byte[] dst) {
// 解密实现
return decryptWithAES(dst);
}
// 其他加密方法...
}
// 使用加密
FastKV kv = new FastKV.Builder(context, "secure_data")
.cipher(new AESCipher())
.build();
// 批量写入
Map<String, Object> data = new HashMap<>();
data.put("name", "张三");
data.put("age", 25);
data.put("score", 89.5f);
data.put("active", true);
kv.putAll(data);
// 批量读取
Map<String, Object> allData = kv.getAll();
// 事务控制(阻塞模式)
kv.disableAutoCommit();
kv.putString("key1", "value1");
kv.putString("key2", "value2");
kv.commit(); // 一次性提交所有更改
public class SpCase {
public static final String NAME = "common_store";
// 替换原有的SharedPreferences获取方式
// public static final SharedPreferences preferences = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
// 使用FastKV并自动迁移数据
public static final SharedPreferences preferences = FastKV.adapt(context, NAME);
}
kv.registerOnSharedPreferenceChangeListener(new SharedPreferences.OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
// 处理数据变化
System.out.println("Key changed: " + key);
}
});
Kotlin是兼容Java的,所以Kotlin下也可以直接用FastKV或者SharedPreferences的API。
此外,Kotlin还提供了“委托属性”这一语法糖,可以用于改进key-value API访问。
可参考:KVData
具体细节可参考: 一种好用的KV存储封装方案
写入性能(毫秒):
| 数据量 | 25 | 50 | 100 | 200 | 400 | 600 |
|---|---|---|---|---|---|---|
| SP-commit | 114 | 172 | 411 | 666 | 2556 | 5344 |
| DataStore | 231 | 625 | 1717 | 4421 | 7629 | 13639 |
| SQLiteKV | 192 | 382 | 1025 | 1565 | 4279 | 5034 |
| SP-apply | 3 | 9 | 35 | 118 | 344 | 516 |
| MMKV | 4 | 8 | 5 | 8 | 10 | 9 |
| FastKV | 3 | 6 | 4 | 6 | 8 | 10 |
读取性能(毫秒):
| 数据量 | 25 | 50 | 100 | 200 | 400 | 600 |
|---|---|---|---|---|---|---|
| SP-commit | 1 | 3 | 2 | 1 | 2 | 3 |
| DataStore | 57 | 76 | 115 | 117 | 170 | 216 |
| SQLiteKV | 96 | 161 | 265 | 417 | 767 | 1038 |
| SP-apply | 0 | 1 | 0 | 1 | 3 | 3 |
| MMKV | 0 | 1 | 1 | 5 | 8 | 11 |
| FastKV | 0 | 1 | 1 | 3 | 3 | 1 |
如果对 FastKV 的实现原理感兴趣,可参考ARCHITECTURE.md。
See the LICENSE file for license rights and limitations.
158 commits
Java
71.6%
Kotlin
28.4%
An efficient key-value storage library.
416
stars
158
commits
Java
primary language
Jul 27, 2025
updated
FastKV是用Java编写的高效可靠的key-value存储库,专为Android平台优化。
dependencies {
implementation 'io.github.billywei01:fastkv:3.0.1'
}
备注:
在2.x版本,FastKV支持多进程存储,但3.x版本开始不再支持多进程。
如需多进程存储,请使用MPFastKV
// 可选:设置全局配置
FastKVConfig.setLogger(FastKVLogger)
FastKVConfig.setExecutor(Dispatchers.IO.asExecutor())
初始化可以按需设置日志接口和Executor:
// 使用Context构造(推荐)
FastKV kv = new FastKV.Builder(context, "user_data").build();
// 或使用自定义路径
FastKV kv = new FastKV.Builder(path, "user_data").build();
// 基本读写操作
if (!kv.getBoolean("first_launch")) {
kv.putBoolean("first_launch", true);
}
int count = kv.getInt("launch_count");
kv.putInt("launch_count", count + 1);
// 支持链式调用
kv.putString("user_name", "张三")
.putInt("user_age", 25)
.putFloat("user_score", 89.5f);
FastKV kv = new FastKV.Builder(context, "secure_data")
.encoder(new FastEncoder[]{CustomObjectEncoder.INSTANCE}) // 自定义编码器
.cipher(new AESCipher()) // 数据加密
.blocking() // 同步阻塞模式
.build();
// 1. 实现FastEncoder接口
public class UserEncoder implements FastEncoder<User> {
@Override
public String tag() {
return "User";
}
@Override
public byte[] encode(User user) {
// 序列化逻辑
return userToBytes(user);
}
@Override
public User decode(byte[] bytes, int offset, int length) {
// 反序列化逻辑
return bytesToUser(bytes, offset, length);
}
}
// 2. 注册编码器并使用
FastEncoder<?>[] encoders = {new UserEncoder()};
FastKV kv = new FastKV.Builder(context, "user_data")
.encoder(encoders)
.build();
// 3. 存储和读取对象
User user = new User("张三", 25);
kv.putObject("current_user", user, new UserEncoder());
User savedUser = kv.getObject("current_user");
推荐使用Packable框架进行对象序列化。
// 实现FastCipher接口
public class AESCipher implements FastCipher {
@Override
public byte[] encrypt(byte[] src) {
// 加密实现
return encryptWithAES(src);
}
@Override
public byte[] decrypt(byte[] dst) {
// 解密实现
return decryptWithAES(dst);
}
// 其他加密方法...
}
// 使用加密
FastKV kv = new FastKV.Builder(context, "secure_data")
.cipher(new AESCipher())
.build();
// 批量写入
Map<String, Object> data = new HashMap<>();
data.put("name", "张三");
data.put("age", 25);
data.put("score", 89.5f);
data.put("active", true);
kv.putAll(data);
// 批量读取
Map<String, Object> allData = kv.getAll();
// 事务控制(阻塞模式)
kv.disableAutoCommit();
kv.putString("key1", "value1");
kv.putString("key2", "value2");
kv.commit(); // 一次性提交所有更改
public class SpCase {
public static final String NAME = "common_store";
// 替换原有的SharedPreferences获取方式
// public static final SharedPreferences preferences = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
// 使用FastKV并自动迁移数据
public static final SharedPreferences preferences = FastKV.adapt(context, NAME);
}
kv.registerOnSharedPreferenceChangeListener(new SharedPreferences.OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
// 处理数据变化
System.out.println("Key changed: " + key);
}
});
Kotlin是兼容Java的,所以Kotlin下也可以直接用FastKV或者SharedPreferences的API。
此外,Kotlin还提供了“委托属性”这一语法糖,可以用于改进key-value API访问。
可参考:KVData
具体细节可参考: 一种好用的KV存储封装方案
写入性能(毫秒):
| 数据量 | 25 | 50 | 100 | 200 | 400 | 600 |
|---|---|---|---|---|---|---|
| SP-commit | 114 | 172 | 411 | 666 | 2556 | 5344 |
| DataStore | 231 | 625 | 1717 | 4421 | 7629 | 13639 |
| SQLiteKV | 192 | 382 | 1025 | 1565 | 4279 | 5034 |
| SP-apply | 3 | 9 | 35 | 118 | 344 | 516 |
| MMKV | 4 | 8 | 5 | 8 | 10 | 9 |
| FastKV | 3 | 6 | 4 | 6 | 8 | 10 |
读取性能(毫秒):
| 数据量 | 25 | 50 | 100 | 200 | 400 | 600 |
|---|---|---|---|---|---|---|
| SP-commit | 1 | 3 | 2 | 1 | 2 | 3 |
| DataStore | 57 | 76 | 115 | 117 | 170 | 216 |
| SQLiteKV | 96 | 161 | 265 | 417 | 767 | 1038 |
| SP-apply | 0 | 1 | 0 | 1 | 3 | 3 |
| MMKV | 0 | 1 | 1 | 5 | 8 | 11 |
| FastKV | 0 | 1 | 1 | 3 | 3 | 1 |
如果对 FastKV 的实现原理感兴趣,可参考ARCHITECTURE.md。
See the LICENSE file for license rights and limitations.
158 commits
Java
71.6%
Kotlin
28.4%