springboot的redis工具类编写(采用RedisTemplate)(简单的取值,取多个值)。

首先:
redis存在于内存中,因此非常高效
存储特点是key-value
其还有许多高效的数据结构,能同时读写几万数据量。

在springboot中运用redis做一些验证存储非常管用,
同时因为redis的高效性,缓存等,对项目速度有益处

对于工具类:
添加相关 springboot依赖:

<!--        redis依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <version>2.2.2.RELEASE</version>
        </dependency>

编写相关util:
RedisTemplate采用自动装配的方法。
使用时也是自动装配

package com.j.ssm.tool;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * redis基本操作封装
 * @author joker
 */
@Component
public class RedisUtil {
    @Autowired
    public  RedisTemplate redisTemplate;// 通过构造方法注入该对象

    /**
     *普通存值
     * @param key
     * @param value
     */
    public  void setValue(String key, Object value) {
        redisTemplate.opsForValue().set(key, value);
    }

    /**
     * 普通取值
     * @param key
     * @return
     */
    public  Object getValue(String key) {
        return redisTemplate.opsForValue().get(key);
    }
    /**
     * 存值并,设置超时时间
     * @param key
     * @param value
     * @param time
     * @param unit
     */
    public  void setValue(String key, Object value, int time, TimeUnit unit) {
        redisTemplate.opsForValue().set(key, value, time, unit);
    }

    /**
     * 存一个MAP--多值
     * @param map
     */
    public void multiSet(Map<String, Object> map) {
        redisTemplate.opsForValue().multiSet(map);
    }

    /**
     * 取多个值
     * @param keys
     * @return list
     */
    public  List<Object> multiGet(Collection<String> keys) {
        return redisTemplate.opsForValue().multiGet(keys);
    }

    /**
     * 取号
     * @param key
     * @param delta
     * @return
     */
    public long incr(String key, long delta) {
        return redisTemplate.opsForValue().increment(key, delta);
    }



}

测试类:

package com.j.ssm.tool;

import com.j.ssm.SsmApplication;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.*;

import static org.junit.jupiter.api.Assertions.*;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SsmApplication.class)
class RedisUtilTest {
    @Autowired
    RedisUtil redisUtil;
    @Test
    void Value() {
        redisUtil.setValue("myKey","6666");
        System.out.println(redisUtil.getValue("myKey"));
    }


    @Test
    void multi() {
        String[] strings={"1","2","33"};
        Map<String,Object> X=new HashMap<>();
        Collection<String> c=new LinkedList<>();
        for (String string:strings) {
            X.put(string,string);
            c.add(string);
        }
        redisUtil.multiSet(X);
        List<Object> X1=redisUtil.multiGet(c);
        for (Object O:X1) {
            System.out.println((String) O);
        }
    }



    @Test
    void incr() {
       System.out.println( redisUtil.incr("6666",new Date().getTime()));
    }
}

测试结果:
在这里插入图片描述
OK

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值