Practical Uses of LevelDB: Temporary Storage, Hidden Credential Management, Stateful Services, and Historical Data
This article introduces LevelDB, a Google‑open‑source key‑value store, and illustrates four practical use cases—temporary token storage, hidden credential management, stateful Java services, and lightweight historical data archiving, accompanied by Groovy/Java code examples for implementation.
LevelDB is an open‑source persistent key‑value store from Google that can serve as a local alternative to Redis for various scenarios.
Part 1 – Temporary storage : Demonstrates storing a login token in LevelDB with Groovy code, allowing automatic refresh when the token expires.
package com.funtest.groovytest
import com.alibaba.fastjson.JSONObject
import com.funtester.db.leveldb.LevelBase
class Demo {
static def token;
static {
token = LevelBase.Instance().get("FunTester_token")
}
/**
* 登录
*/
def login() {
String url = "/login"
params.name = "FunTester"
params.pwd = "123456"
def response = getResponse(url, params)
token = response.getString("token")
LevelBase.Instance().put("FunTester_token", token)
}
/**
* 查询
*/
def select() {
String url = "/select"
params.key = "FunTester"
def response = getResponse(url, params)
if (response.getCode == 401) {
login()
response = getResponse(url, params)
}
dosomething()
}
/**
* 获取请求响应
* @param url
* @param params
* @return
*/
def getResponse(String url, JSONObject params) {
def request = getRequest(url, params)
request.addHeader("token", token)
def httpresponse = getHttpresponse(request)
httpresponse
}
}Part 2 – Hidden credential storage : Shows how to keep usernames and passwords out of source code by persisting them in LevelDB, similar to encrypted Redis entries.
class Demo {
static String name
static String pwd
static {
name = LevelBase.Instance().get("FunTester_name")
pwd = LevelBase.Instance().get("FunTester_pwd")
}
}Part 3 – Stateful services : Explains using LevelDB to record per‑node state for long‑running tasks in Java services, enabling restart‑safe execution without heavy relational databases.
Part 4 – Historical data : Describes archiving monitoring data in LevelDB files alongside code, useful for hourly statistics and lightweight history tracking when full‑scale time‑series databases are overkill.
The article concludes with an invitation to follow further content from the author.
FunTester
10k followers, 1k articles | completely useless
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.