# StorageService

Call the **storageService** SPI to store and manage key-value pairs.

# Method signature

```java
public interface StorageService {
    void setValue(String key, String value);

    String getValue(String key);

    boolean removeValue(String key);

    boolean clear();
}
```

# Request parameters

| **Item** | **Type** | **Parameters** | **Description** | **Required** |
| --- | --- | --- | --- | --- |
| setValue(key, value) | Function | String key String value | Saves a value in storage associated with the specified key. | Required |
| String getValue(key) | Function | String key | Retrieves the value associated with the specified key. | Required |
| boolean removeValue(key) | Function | String key | Removes the value associated with the specified key. | Required |
| boolean clear() | Function | / | Deletes all key-value pairs from storage. | Required |

# Response parameters

| **Item** | **Type** | **Description** | **Required** |
| --- | --- | --- | --- |
| String getValue(key) | String | The value of the given key. | Required |
| boolean removeValue(key) | Boolean | Whether the value removal is successful. Valid values are: - `true`: successful - `false`: failed | Required |
| boolean clear() | Boolean | Whether the storage clearing is successful. Valid values are: - `true`: successful - `false`: failed | Required |

# Sample

```kotlin

class StorageServiceImpl(context: Context?) : StorageService {
    private val sharedPreferences: SharedPreferences
    private val logger: LoggerProxy? = ConfigCenter.getPlugin(LoggerProxy::class.java) as LoggerProxy?

    init {
        if (context == null) {
            logger?.e(TAG, "Context should not be null")
        }
        sharedPreferences = context!!.getSharedPreferences(SP_PREFIX, Context.MODE_PRIVATE)
    }

    override fun setValue(key: String, value: String) {
        if (TextUtils.isEmpty(key)) {
            logger?.e(TAG, "key should not be null, saveValue fail for key: $key, value: $value")
            return
        }
        try {
            sharedPreferences.edit().putString(key, value).apply()
        } catch (exception: Exception) {
            logger?.e(TAG, "saveValue exception: $exception")
        }
    }

    override fun getValue(key: String): String? {
        if (TextUtils.isEmpty(key)) {
            logger?.e(TAG, "key should not be null, return null")
            return null
        }
        return try {
            if (!sharedPreferences.contains(key)) {
                logger?.e(
                    TAG,
                    "value of key " + key + " does not exist, return " +
                            "null"
                )
                return null
            }
            sharedPreferences.getString(key, "")
        } catch (exception: Exception) {
            logger?.e(TAG, "getValue exception: $exception")
            null
        }
    }

    override fun removeValue(key: String): Boolean {
        if (TextUtils.isEmpty(key)) {
            logger?.e(
                TAG,
                "removeValue, key should not be null, removeValue failed."
            )
            return false
        }
        return try {
            sharedPreferences.edit().remove(key).apply()
            true
        } catch (exception: Exception) {
            logger?.e(TAG, "removeValue exception: $exception")
            false
        }
    }

    override fun clear(): Boolean {
        return try {
            sharedPreferences.edit().clear().apply()
            true
        } catch (exception: Exception) {
            logger?.e(TAG, "clear exception: $exception")
            false
        }
    }

    companion object {
        private const val TAG = "StorageService"
        private const val SP_PREFIX = "NFC_sp"
    }
}

```