FireflyKV API
FireflyKV 對象,主要(yào / yāo)用于(yú) KV 存儲的(de)初始化,存、取等操作,是(shì)主要(yào / yāo)的(de)類。
初始化
FireFlyKV需調用init方法進行初始化,返回FireFlyKV實例對象,進而(ér)完成讀寫等操作,下文增删改查等操作,默認已初始化完成,
返回FireFlyKV的(de)實例對象爲(wéi / wèi)fireFlyKV。
默認初始化
FireFlyKV init(Context context);
參數:
屬性 |
類型 |
說(shuō)明 |
context |
Context |
上(shàng)下文 |
示例:
FireFlyKV fireFlyKV = FireFlyKV.init(this);
加密時(shí)初始化
FireFlyKV init(Context context, String path, int encryptType,String key, int mode);
參數:
屬性 |
類型 |
說(shuō)明 |
context |
Context |
上(shàng)下文 |
path |
String |
KV 存儲庫路徑 |
encryptType |
int |
加密類型:0-不(bù)加密;1-chacha20加密 |
key |
String |
秘鑰 |
mode |
Int |
是(shì)否開啓多進程訪問:0-不(bù)開啓;1-開啓 |
示例:
public static final int NONE_ENCRYPT = 0;
public static final int CC20_ENCRYPT = 1;
String path = context.getFilesDir().getAbsolutePath() + "/ffkv";
String key = "chacha20";
FireFlyKV fireFlyKV = FireFlyKV.init(path, CC20_ENCRYPT, key);
存儲數據
存儲 String 類型數值
以(yǐ) key 作爲(wéi / wèi)鍵存儲 String 類型數值。
boolean putString(String key, String value);
參數:
屬性 |
類型 |
說(shuō)明 |
key |
String |
key 值 |
value |
String |
value 鍵 |
返回值:
類型 |
說(shuō)明 |
boolean |
插入數據是(shì)否成功 |
示例:
boolean result = fireFlyKV.putString("key", "value");
存儲 byte 類型數值
以(yǐ) key 作爲(wéi / wèi)鍵存儲 byte 類型數值。
boolean putByte(String key, byte value);
參數:
屬性 |
類型 |
說(shuō)明 |
key |
String |
key 值 |
value |
byte |
value 鍵 |
返回值:
類型 |
說(shuō)明 |
boolean |
添加數據是(shì)否成功 |
示例:
boolean result = fireFlyKV.putByte("byte", 127);
存儲 short 類型數值
以(yǐ) key 作爲(wéi / wèi)鍵存儲 short 類型數值。
boolean putShort(String key, short value);
參數:
屬性 |
類型 |
說(shuō)明 |
key |
String |
key 值 |
value |
short |
value 鍵 |
返回值:
類型 |
說(shuō)明 |
boolean |
添加數據是(shì)否成功 |
示例:
boolean result = fireFlyKV.putShort("short", 130);
存儲 int 類型數值
以(yǐ) key 作爲(wéi / wèi)鍵存儲 int 類型數值。
boolean putInt(String key, int value);
參數:
屬性 |
類型 |
說(shuō)明 |
key |
String |
key 值 |
value |
int |
value 鍵 |
返回值:
類型 |
說(shuō)明 |
boolean |
添加數據是(shì)否成功 |
示例:
boolean result = fireFlyKV.putInt("int", 300);
存儲 long 類型數值
以(yǐ) key 作爲(wéi / wèi)鍵存儲 long 類型數值。
boolean putLong(String key, long value);
參數:
屬性 |
類型 |
說(shuō)明 |
key |
String |
key 值 |
value |
long |
value 鍵 |
返回值:
類型 |
說(shuō)明 |
boolean |
添加數據是(shì)否成功 |
示例:
boolean result = fireFlyKV.putLong("long", 323432423423423L);
存儲 float 類型數值
以(yǐ) key 作爲(wéi / wèi)鍵存儲 float 類型數值。
boolean putFloat(String key, float value);
參數:
屬性 |
類型 |
說(shuō)明 |
key |
String |
key 值 |
value |
float |
value 鍵 |
返回值:
類型 |
說(shuō)明 |
boolean |
添加數據是(shì)否成功 |
示例:
boolean result = fireFlyKV.putFloat("float", 1.2f);
存儲 double 類型數值
以(yǐ) key 作爲(wéi / wèi)鍵存儲 double 類型數值。
boolean putDouble(String key, double value);
參數:
屬性 |
類型 |
說(shuō)明 |
key |
String |
key 值 |
value |
double |
value 鍵 |
返回值:
類型 |
說(shuō)明 |
boolean |
添加數據是(shì)否成功 |
示例:
boolean result = fireFlyKV.putDouble("double", 1234132123);
存儲 char 類型數值
以(yǐ) key 作爲(wéi / wèi)鍵存儲 char 類型數值。
boolean putChar(String key, char value);
參數:
屬性 |
類型 |
說(shuō)明 |
key |
String |
key 值 |
value |
char |
value 鍵 |
返回值:
類型 |
說(shuō)明 |
boolean |
添加數據是(shì)否成功 |
示例:
boolean result = fireFlyKV.putChar("char", 'c');
存儲 boolean 類型數值
以(yǐ) key 作爲(wéi / wèi)鍵存儲 boolean 類型數值。
boolean putBoolean(String key, boolean value);
參數:
屬性 |
類型 |
說(shuō)明 |
key |
String |
key 值 |
value |
boolean |
value 鍵 |
返回值:
類型 |
說(shuō)明 |
boolean |
添加數據是(shì)否成功 |
示例:
boolean result = fireFlyKV.putBoolean("boolean", false);
獲取數據
獲取 String 類型數值
通過 key 取 String 值,默認是(shì) null
普通
String getString(String key);
參數:
屬性 |
類型 |
說(shuō)明 |
key |
String |
key 鍵 |
示例:
String str = fireFlyKV.getString("key");
帶默認值
String getString(String key, String defaultValue);
參數:
屬性 |
類型 |
說(shuō)明 |
key |
String |
key 鍵 |
defaultValue |
String |
默認值 |
返回值:
示例:
String str = fireFlyKV.getString("key", "FireFlyKV");
獲取 byte 類型數值
通過 key 取 byte 值,默認是(shì) 0
普通
String getByte(String key);
參數:
屬性 |
類型 |
說(shuō)明 |
key |
String |
key 鍵 |
示例:
byte bRet = fireFlyKV.getByte("byte");
帶默認值
byte getByte(String key, byte defaultValue);
參數:
屬性 |
類型 |
說(shuō)明 |
key |
String |
key 鍵 |
defaultValue |
byte |
默認值 |
返回值:
示例:
byte bRet = fireFlyKV.getByte("byte", 1);
獲取 short 類型數值
通過 key 取 short 值,默認是(shì) 0
普通
short getShort(String key);
參數:
屬性 |
類型 |
說(shuō)明 |
key |
String |
key 鍵 |
示例:
short sRet = fireFlyKV.getShort("short");
帶默認值
short getShort(String key, short defaultValue);
參數:
屬性 |
類型 |
說(shuō)明 |
key |
String |
key 鍵 |
defaultValue |
short |
默認值 |
返回值:
示例:
short sRet = fireFlyKV.getShort("short", 1);
獲取 int 類型數值
通過 key 取 int 值,默認是(shì) 0
普通
int getInt(String key);
參數:
屬性 |
類型 |
說(shuō)明 |
key |
String |
key 鍵 |
示例:
int iRet = fireFlyKV.getInt("int");
帶默認值
int getInt(String key, int defaultValue);
參數:
屬性 |
類型 |
說(shuō)明 |
key |
String |
key 鍵 |
defaultValue |
int |
默認值 |
返回值:
示例:
int iRet = fireFlyKV.getInt("int", 1);
獲取 long 類型數值
通過 key 取 long 值,默認是(shì) 0L
普通
long getLong(String key);
參數:
屬性 |
類型 |
說(shuō)明 |
key |
String |
key 鍵 |
示例:
long lRet = fireFlyKV.getLong("long");
帶默認值
long getLong(String key, long defaultValue);
參數:
屬性 |
類型 |
說(shuō)明 |
key |
String |
key 鍵 |
defaultValue |
long |
默認值 |
返回值:
示例:
long lRet = fireFlyKV.getLong("long", 1L);
獲取 float 類型數值
通過 key 取 float 值,默認是(shì) 0f
普通
float getFloat(String key);
參數:
屬性 |
類型 |
說(shuō)明 |
key |
String |
key 鍵 |
示例:
float fRet = fireFlyKV.getFloat("float");
帶默認值
float getFloat(String key, float defaultValue);
參數:
屬性 |
類型 |
說(shuō)明 |
key |
String |
key 鍵 |
defaultValue |
float |
默認值 |
返回值:
示例:
float fRet = fireFlyKV.getFloat("float", 1.2f);
獲取 double 類型數值
通過 key 取 double 值,默認是(shì) 0.00
普通
double getDouble(String key);
參數:
屬性 |
類型 |
說(shuō)明 |
key |
String |
key 鍵 |
示例:
double dRet = fireFlyKV.getDouble("double");
帶默認值
double getDouble(String key, double defaultValue);
參數:
屬性 |
類型 |
說(shuō)明 |
key |
String |
key 鍵 |
defaultValue |
double |
默認值 |
返回值:
示例:
double dRet = fireFlyKV.getDouble("double", 1.23);
獲取 char 類型數值
通過 key 取 char 值,默認是(shì) 0
普通
char getChar(String key);
參數:
屬性 |
類型 |
說(shuō)明 |
key |
String |
key 鍵 |
示例:
char cRet = fireFlyKV.getChar("char");
帶默認值
char getChar(String key, char defaultValue);
參數:
屬性 |
類型 |
說(shuō)明 |
key |
String |
key 鍵 |
defaultValue |
char |
默認值 |
返回值:
示例:
char cRet = fireFlyKV.getChar("char", 'a');
獲取 boolean 類型數值
通過 key 取 boolean 值,默認是(shì) false
普通
boolean getBoolean(String key);
參數:
屬性 |
類型 |
說(shuō)明 |
key |
String |
key 鍵 |
示例:
boolean boolRet = fireFlyKV.getBoolean("boolean");
帶默認值
boolean getBoolean(String key, boolean defaultValue);
參數:
屬性 |
類型 |
說(shuō)明 |
key |
String |
key 鍵 |
defaultValue |
boolean |
默認值 |
返回值:
示例:
boolean boolRet = fireFlyKV.getBoolean("boolean", false);
是(shì)否包含 key 值
boolean containsKey(String key);
參數
屬性 |
類型 |
說(shuō)明 |
key |
String |
key 鍵 |
返回值
示例:
boolean containsKey = fireFlyKV.containsKey("name");
删除 key 值
移除 KV 庫中的(de)爲(wéi / wèi) key 的(de)數據
boolean removeValueForKey(String key);
參數
屬性 |
類型 |
說(shuō)明 |
key |
String |
key 鍵 |
返回值
示例:
boolean removeSuccess = fireFlyKV.removeValueForKey("key");
清除 KV 庫數據
void clearAll();
參數
無
返回值
無
示例:
fireFlyKV.clearAll();
多進程訪問
處理多個(gè)進程間的(de)數據同步問題,需要(yào / yāo)在(zài)FireflyKV初始化時(shí),指定爲(wéi / wèi)多進程模式即可。
FireFlyKV init(Context context, String path, int encryptType,String key, int mode);
參數:
屬性 |
類型 |
說(shuō)明 |
context |
Context |
上(shàng)下文 |
path |
String |
KV 存儲庫路徑 |
encryptType |
int |
加密類型:0-不(bù)加密;1-chacha20加密 |
key |
String |
秘鑰 |
mode |
Int |
是(shì)否開啓多進程訪問:0-不(bù)開啓;1-開啓 |
示例:
public static final int SINGLE_PROCESS_MODE = 0;
public static final int MULTI_PROCESS_MODE = 1;
FireFlyKV fireFlyKV = FireFlyKV.init(this, FireFlyKV.MULTI_PROCESS_MODE);
fireFlyKV.putString("key", "value");
String str = fireFlyKV.getString("key");