Redis实战--SpringBoot中对Redis数据类型String的基本操作示例

echo编辑整理,欢迎转载,转载请声明文章来源。欢迎添加echo微信(微信号:t2421499075)交流学习。 百战不败,依不自称常胜,百败不颓,依能奋力前行。——这才是真正的堪称强大!!!


该文章是接上一篇文章《Redis整合SpringBoot示例》的后续,操作用例代码比较多,这里展示核心代码所占篇幅很多,所以单独抽出来写

String类型在SpringBoot中的使用代码如下

方法对应的redis基本操作都比较简单,这里不做详细解释,每一个方法都有对应的注释。

package com.example.echo.redis;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.IntStream;

/**
 * @author XLecho
 * Date 2019/11/9 0009
 * Time 10:28
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisTypeStringUseTest {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 存入String类型
     */
    @Test
    public void testSetString() {
        redisTemplate.opsForValue().set("testString", "testString");
        System.out.println(redisTemplate.opsForValue().get("testString"));
    }

    /**
     * 获取存入的String类型
     */
    @Test
    public void testGetString() {
        String string = redisTemplate.opsForValue().get("testString");
        System.out.println(string);
    }

    /**
     * 删除存入的String类型
     */
    @Test
    public void testDelString() {
        redisTemplate.delete("testString");
        System.out.println(redisTemplate.opsForValue().get("testString"));
    }

    /**
     * 对存入的值进行incr递增
     */
    @Test
    public void testIncrString() {
        redisTemplate.opsForValue().set("value", "10");
        redisTemplate.opsForValue().increment("value", 1.1);
        System.out.println(redisTemplate.opsForValue().get("value"));
    }

    /**
     * 对存入的值进行decr递减
     */
    @Test
    public void testDecrString() {
        redisTemplate.opsForValue().set("value", "10");
        redisTemplate.opsForValue().increment("value", -1);
        System.out.println(redisTemplate.opsForValue().get("value"));
    }

    /**
     * 对存入的值进行append拼接
     */
    @Test
    public void testAppendString() {
        redisTemplate.opsForValue().set("value", "string");
        redisTemplate.opsForValue().append("value", "s");
        System.out.println(redisTemplate.opsForValue().get("value"));
    }

    /**
     * 获取指定key对应的索引位置的字符
     */
    @Test
    public void testGetRangeString() {
        redisTemplate.opsForValue().set("value", "abcdefgh");
        String value = redisTemplate.opsForValue().get("value", 1, 5);
        System.out.println(value);
    }

    /**
     * 在指定key对应的索引位置后面添加字符666
     * 注意:该方法会从指定索引后面添加666,但是如果指定索引后面刚好有还有3个字符,那么那3个字符会被替换
     */
    @Test
    public void testSetRangeString() {
        redisTemplate.opsForValue().set("value", "666", 2);
        System.out.println(redisTemplate.opsForValue().get("value"));
    }

    /**
     * 获取指定key的长度
     */
    @Test
    public void testStrlenString() {
        System.out.println(redisTemplate.opsForValue().size("value"));
    }

    /**
     * 同时set多个key
     */
    @Test
    public void testMsetString() {
        Map<String, String> map = new HashMap<>();
        map.put("a", "1");
        map.put("b", "2");
        redisTemplate.opsForValue().multiSet(map);
        System.out.println(redisTemplate.opsForValue().get("a"));
        System.out.println(redisTemplate.opsForValue().get("b"));
    }

    /**
     * 同时get多个值
     */
    @Test
    public void testMgetString() {
        List<String> list = Arrays.asList("a", "b");
        System.out.println(redisTemplate.opsForValue().multiGet(list));
    }

    /**
     * setnx的使用
     */
    @Test
    public void testSetnxString() {
        // 使用循环插入多次,校验存在的时候是否能够插入
        IntStream.range(0, 10).forEach(it -> {
            Boolean email = redisTemplate.opsForValue().setIfAbsent("email", "xl_echo@163.com");
            System.out.println(email);
        });
    }
}

项目源码地址:https://coding.net/u/xlsorry/p/SpringBootRedis/git

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xlecho

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值