zoukankan      html  css  js  c++  java
  • redis 模拟jedis 操作string类型数据

    一.思路分析

    redis数据传输遵循resp协议,只需要按照resp协议并通过socket传递数据到redis服务器即可

    resp数据格式:

     

    二.具体实现

    package com.ahd.jedis;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.Socket;
    
    /***
     * 模拟jedis对redis进行操作
     */
    public class MyJedis {
        private Socket socket;
    
        public MyJedis(String url,Integer port) throws IOException {
            socket=new Socket(url,port);
        }
    
        public static void main(String[] args) throws IOException {
            MyJedis myJedis=new MyJedis("127.0.0.1",6379);
    
            //myJedis.set("sex","sdf");
            //myJedis.get("sex");
            myJedis.del("sex");
        }
    
        /***
         * 模拟set string类型
         * @param key
         * @param value
         */
        public void set(String key,String value) throws IOException {
            StringBuffer stringBuffer=new StringBuffer();
            //getBytes 是因为key和value可能是中文,直接获取长度会出错
            stringBuffer.append("*3"+"
    ")
                    .append("$3"+"
    ").append("SET"+"
    ")
                    .append("$"+key.getBytes().length+"
    ").append(key+"
    ")
                    .append("$"+value.getBytes().length+"
    ").append(value+"
    ");
            OutputStream outputStream = socket.getOutputStream();
    
            System.out.println(stringBuffer);
            outputStream.write(stringBuffer.toString().getBytes());
    
        }
    
        /***
         * 模拟get String
         * @param key
         * @return
         */
        public String get(String key) throws IOException {
            StringBuffer stringBuffer=new StringBuffer();
            //getBytes 是因为key和value可能是中文,直接获取长度会出错
            stringBuffer.append("*2"+"
    ")
                    .append("$3"+"
    ").append("get"+"
    ")
                    .append("$"+key.getBytes().length+"
    ").append(key+"
    ");
            //1. 将命令传递到redis
            OutputStream outputStream = socket.getOutputStream();
            outputStream.write(stringBuffer.toString().getBytes());
    
            //2. 接收redis的响应结果
            InputStream inputStream = socket.getInputStream();
            byte[] b=new byte[2048];
            inputStream.read(b);
    
            return new String(b);
        }
    
        /***
         * 指定key 删除
         * @param key
         * @return
         * @throws IOException
         */
        public void del(String key) throws IOException {
            StringBuffer stringBuffer=new StringBuffer();
            //getBytes 是因为key和value可能是中文,直接获取长度会出错
            stringBuffer.append("*2"+"
    ")
                    .append("$3"+"
    ").append("del"+"
    ")
                    .append("$"+key.getBytes().length+"
    ").append(key+"
    ");
            //1. 将命令传递到redis
            OutputStream outputStream = socket.getOutputStream();
            outputStream.write(stringBuffer.toString().getBytes());
            outputStream.flush();
        }
    
    }
    
     

    三.运行结果

     

  • 相关阅读:
    学习笔记
    django中嵌入百度editor插件
    定位屡试不爽
    django忘记管理员账号和密码处理
    linux上配置java环境
    python3学习问题汇总
    Android系统框架
    python深复制和浅复制
    装饰器原理和装饰器参数使用
    小白神器
  • 原文地址:https://www.cnblogs.com/aihuadung/p/11604938.html
Copyright © 2011-2022 走看看