其他
周末撸了一个简版的 Redis,轻松搞定高性能的 key-value 服务
rocksdb
特征
专为希望在本地或远程存储系统上存储多达数TB数据的应用程序服务器而设计。 优化用于在快速存储 - 闪存设备或内存中存储中小尺寸键值 它适用于具有多个内核的处理器
RestExpress
实现kedis
/**
* @author: kl @kailing.pub
* @date: 2019/4/12
*/
public class Main {
public static void main(String[] args) {
Configs configs = new Configs();
configs.fromArgs(args);
RestExpress server = new RestExpress()
.setName("kedis-server")
.setBaseUrl("http://localhost:" +configs.getPort());
KedisCore core =new KedisCore(configs.getDbPath());
Routes.define(server,core);
server.bind(configs.getPort());
server.awaitShutdown();
}
}
/**
* @author: kl @kailing.pub
* @date: 2019/4/12
*/
public class KedisCore {
private RocksDB db;
public KedisCore(String path) {
RocksDB.loadLibrary();
try {
final Options options = new Options().setCreateIfMissing(true);
this.db = RocksDB.open(options, path);
} catch (RocksDBException ex) {
ex.printStackTrace();
}
}
public String put(Request request, Response response) throws Exception {
Map<String, String> map = request.getQueryStringMap();
String key = map.get("key");
String value = map.get("value");
db.put(key.getBytes(), value.getBytes());
return "ok";
}
public String get(Request request, Response response) throws Exception {
Map<String, String> map = request.getQueryStringMap();
String key = map.get("key");
byte[] values = db.get(key.getBytes());
if(values != null){
return new String(values,"utf-8");
}else {
return null;
}
}
}
/**
* @author: kl @kailing.pub
* @date: 2019/4/12
*/
public abstract class Routes {
public static void define(RestExpress server,KedisCore core){
server.uri("/put", core).action("put", HttpMethod.GET).noSerialization();
server.uri("/get", core).action("get", HttpMethod.GET).noSerialization();
}
}
java -jar kedis-1.0.jar --port 8081
curl http://localhost:8081/put?key=name&value=ckl
curl http://localhost:8081/get?key=name
结语
往期推荐