ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

redis

2022-05-10 08:01:41  阅读:155  来源: 互联网

标签:String Resp redis lubanSocket public append socket


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
}





}

标签:String,Resp,redis,lubanSocket,public,append,socket
来源: https://www.cnblogs.com/mfk11/p/16251992.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有