本文主要是介绍redis,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
package com.luban.redis;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class LubanSocket {
private Socket socket;
private InputStream inputStream;
private OutputStream outputStream;
public LubanSocket(String ip,int prot) {
try {
if(!isCon()){
socket=new Socket(ip,prot);
inputStream=socket.getInputStream();
outputStream=socket.getOutputStream();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void send(String str){
System.out.println(str);
try {
outputStream.write(str.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
public String read(){
byte[] b=new byte[1024];
int count=0;
try {
count= inputStream.read(b);
} catch (IOException e) {
e.printStackTrace();
}
return new String(b,0,count);
}
public boolean isCon(){
return socket!=null && !socket.isClosed() && socket.isConnected();
}
public void close(){
if(outputStream!=null){
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(inputStream!=null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(socket!=null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package com.luban.redis;
import redis.clients.jedis.Jedis;
public class RedisClient {
private LubanSocket lubanSocket;
public RedisClient(String ip,int prot) {
this.lubanSocket=new LubanSocket(ip,prot);
}
public String set(String key, String value){
lubanSocket.send(commandStrUtil(Resp.command.SET,key.getBytes(),value.getBytes()));
return lubanSocket.read();
}
public void close(){
lubanSocket.close();
}
public String get(String key){
lubanSocket.send(commandStrUtil(Resp.command.GET,key.getBytes()));
return lubanSocket.read();
}
public String incr(String key){
lubanSocket.send(commandStrUtil(Resp.command.INCR,key.getBytes()));
return lubanSocket.read();
}
public String commandStrUtil(Resp.command command, byte[]... bytes){
StringBuilder stringBuilder=new StringBuilder();
stringBuilder.append(Resp.star).append(1+bytes.length).append(Resp.crlf);
stringBuilder.append(Resp.lengthStart).append(command.toString().getBytes().length).append(Resp.crlf);
stringBuilder.append(command.toString()).append(Resp.crlf);
for (byte[] aByte : bytes) {
stringBuilder.append(Resp.lengthStart).append(aByte.length).append(Resp.crlf);
stringBuilder.append(new String(aByte)).append(Resp.crlf);
}
return stringBuilder.toString();
}
public static void main(String[] args) {
// RedisClient redisClient=new RedisClient("192.168.204.188",6379);
Jedis redisClient=new Jedis("127.0.0.1",9999);
System.out.println(redisClient.set("taibai", "123456"));
System.out.println(redisClient.get("taibai"));
System.out.println(redisClient.incr("lock"));
redisClient.close();
}
}
package com.luban.redis;
public class Resp {
public static final String star="*";
public static final String crlf="\r\n";
public static final String lengthStart="$";
public static enum command{
SET,GET,INCR
}
}
这篇关于redis的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!