JAVA中redis各种方法的封装---(List操作)

获取list缓存的内容

    /**
     * lRange命令 获取list缓存的内容
     *
     * @param key   键
     * @param start 开始
     * @param end   结束 0 到 -1 代表所有值
     * @return 获取到的缓存内容
     */
    public List<Object> lRange(String key, long start, long end) {
        try {
            return redisTemplate.opsForList().range(key, start, end);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

获取list缓存的长度

    /**
     * lLen命令 获取list缓存的长度
     *
     * @param key 键
     * @return 获取到的长度
     */
    public long lLen(String key) {
        try {
            return redisTemplate.opsForList().size(key);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

从list的右侧插入值

    /**
     * rPush 从list的右侧插入值
     *
     * @param key   键
     * @param value 值
     * @return 436
     */
    public boolean rPush(String key, Object value) {
        try {
            redisTemplate.opsForList().rightPush(key, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

从list的右侧插入多个值

    /**
     * rPush 从list的右侧插入多个值
     *
     * @param key   键
     * @param value 多个值
     * @return 取出是否成功
     */
    public boolean rPush(String key, List<Object> value) {
        try {
            redisTemplate.opsForList().rightPushAll(key, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

从list的右侧插入多个值,并设置超时时间

    /**
     * rPush 从list的右侧插入多个值,并设置超时时间
     *
     * @param key   键
     * @param value 值
     * @param time  时间(秒)
     * @return 操作是否成功
     */
    public boolean rPush(String key, List<Object> value, long time) {
        try {
            redisTemplate.opsForList().rightPushAll(key, value);
            if (time > 0) {
                expire(key, time);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

从list的左侧插入值

    /**
     * lPush 从list的左侧插入值
     *
     * @param key   键
     * @param value 值
     * @return 436
     */
    public boolean lPush(String key, Object value) {
        try {
            redisTemplate.opsForList().leftPush(key, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

从list的左侧插入值并设置超时时间

    /**
     * rPush 从list的左侧插入值并设置超时时间
     *
     * @param key   键
     * @param value 值
     * @param time  时间(秒)
     * @return 操作是否成功
     */
    public boolean lPush(String key, Object value, long time) {
        try {
            redisTemplate.opsForList().leftPush(key, value);
            if (time > 0) {
                expire(key, time);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

从list的左侧插入多个值

    /**
     * rPush 从list的左侧插入多个值
     *
     * @param key   键
     * @param value 多个值
     * @return 操作是否成功
     */
    public boolean lPushAll(String key, List<Object> value) {
        try {
            redisTemplate.opsForList().leftPushAll(key, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

从list的左侧插入多个值,并设置超时时间

    /**
     * lPush 从list的左侧插入多个值,并设置超时时间
     *
     * @param key   键
     * @param value 值
     * @param time  时间(秒)
     * @return 操作是否成功
     */
    public boolean lPush(String key, List<Object> value, long time) {
        try {
            redisTemplate.opsForList().leftPushAll(key, value);
            if (time > 0) {
                expire(key, time);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

从左侧弹出一个元素(非阻塞,存在线程安全)

    /**
     * lPop命令 从左侧弹出一个元素(非阻塞,存在线程安全)
     *
     * @param key 键
     * @return 弹出的元素
     */
    public Object lPop(String key) {
        try {
            redisTemplate.opsForList().leftPop(key);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

从左侧弹出一个元素(阻塞,设置有阻塞时间)

    /**
     * bLeftPop 从左侧弹出一个元素(阻塞,设置有阻塞时间)
     *
     * @param key     键
     * @param timeout 阻塞时间
     * @return 弹出的数据
     */
    public Object bLeftPop(String key, long timeout) {
        try {
            redisTemplate.opsForList().leftPop(key, timeout, TimeUnit.SECONDS);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

从右侧弹出一个元素(非阻塞,存在线程安全)

    /**
     * rPop命令 从右侧弹出一个元素(非阻塞,存在线程安全)
     *
     * @param key 键
     * @return 弹出的元素
     */
    public Object rPop(String key) {
        try {
            redisTemplate.opsForList().rightPop(key);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

从右侧弹出一个元素(阻塞,设置有阻塞时间)

    /**
     * bRightPop命令 从右侧弹出一个元素(阻塞,设置有阻塞时间)
     *
     * @param key     键
     * @param timeout 阻塞时间
     * @return 弹出的元素
     */
    public Object bRightPop(String key, long timeout) {
        try {
            redisTemplate.opsForList().rightPop(key, timeout, TimeUnit.SECONDS);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

通过索引 获取list中的值

    /**
     * lIndex命令 通过索引 获取list中的值
     *
     * @param key   键
     * @param index 索引 index>=0时, 0 表头,1 第二个元素,依次类推;index<0时,-1,表尾,-2倒数第二个元素,依次类推
     * @return 获取到的数据
     */
    public Object lIndex(String key, long index) {
        try {
            return redisTemplate.opsForList().index(key, index);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

根据索引修改list中的某条数据

    /**
     * lSet命令 根据索引修改list中的某条数据
     *
     * @param key   键
     * @param index 索引
     * @param value 值
     * @return 509
     */
    public boolean lSet(String key, long index, Object value) {
        try {
            redisTemplate.opsForList().set(key, index, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

移除N个值的value

    /**
     * lRem 移除N个值的value
     *
     * @param key   键
     * @param count 移除多少个
     * @param value 值
     * @return 移除的个数
     */
    public long lRem(String key, long count, Object value) {
        try {
            return redisTemplate.opsForList().remove(key, count, value);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

移除 start到end的值

    /**
     * ltrim 命令 移除 start到end的值
     *
     * @param key   键
     * @param start 起始位置
     * @param end   结束位置
     * @return 是否移除成功
     */
    public boolean ltrim(String key, long start, long end) {
        try {
            redisTemplate.opsForList().trim(key, start, end);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

从fromKey右侧取出,由左侧插入destKey中(非阻塞)

    /**
     * rightPopLeftPush 从fromKey右侧取出,由左侧插入destKey中(非阻塞)
     *
     * @param fromKey 来源键
     * @param destKey 目标键
     * @return 是否成功
     */
    public boolean rightPopLeftPush(String fromKey, String destKey) {
        try {
            redisTemplate.opsForList().rightPopAndLeftPush(fromKey, destKey);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

从fromKey右侧取出,destKey(阻塞,设有阻塞时间)

    /**
     * rightPopAndLeftPush 从fromKey右侧取出,destKey(阻塞,设有阻塞时间)
     *
     * @param fromKey 来源键
     * @param destKey 目标键
     * @return 是否取出成功
     */
    public boolean bRightPopLeftPush(String fromKey, String destKey, long timeout) {
        try {
            redisTemplate.opsForList().rightPopAndLeftPush(fromKey, destKey, timeout, TimeUnit.SECONDS);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,以下是一个简单的使用Socket封装Java操作Redis的工具类: ```java import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; import java.util.ArrayList; import java.util.List; public class RedisUtils { private static final String HOST = "127.0.0.1"; private static final int PORT = 6379; private Socket socket; private InputStream input; private OutputStream output; public RedisUtils() { try { this.socket = new Socket(HOST, PORT); this.input = socket.getInputStream(); this.output = socket.getOutputStream(); } catch (IOException e) { e.printStackTrace(); } } public String set(String key, String value) { String result = null; try { output.write(("SET " + key + " " + value + "\r\n").getBytes()); byte[] bytes = new byte[1024]; int length = input.read(bytes); result = new String(bytes, 0, length); } catch (IOException e) { e.printStackTrace(); } return result; } public String get(String key) { String result = null; try { output.write(("GET " + key + "\r\n").getBytes()); byte[] bytes = new byte[1024]; int length = input.read(bytes); result = new String(bytes, 0, length); } catch (IOException e) { e.printStackTrace(); } return result; } public List<String> lrange(String key, int start, int end) { List<String> result = new ArrayList<>(); try { output.write(("LRANGE " + key + " " + start + " " + end + "\r\n").getBytes()); byte[] bytes = new byte[1024]; int length = input.read(bytes); String str = new String(bytes, 0, length); if (str.startsWith("*")) { int count = Integer.parseInt(str.substring(1, str.indexOf("\r\n"))); for (int i = 0; i < count; i++) { length = input.read(bytes); result.add(new String(bytes, 0, length).trim()); } } } catch (IOException e) { e.printStackTrace(); } return result; } public void close() { try { input.close(); output.close(); socket.close(); } catch (IOException e) { e.printStackTrace(); } } } ``` 这个工具类实现了Redis的三个基本操作:SET、GET和LRANGE。其,SET和GET操作都是将字符串拼接成Redis协议格式,然后通过Socket发送给Redis服务端,最后接收Redis服务端返回的数据,转换成Java字符串格式并返回。LRANGE操作稍微复杂一些,因为需要接收Redis服务端返回的多个字符串,所以需要对返回的数据进行解析。最后,还提供了一个close方法,用于关闭Socket连接。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值