SpringBoot中使用Redis

win安装redis客户端

https://github.com/tporadowski/redis/releases下载zip文件解压
redis-server.exe运行redis

pom添加redis依赖

<dependency>
 <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

properties配置redis

spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=
#数据库索引默认0
spring.redis.database=0

redis使用示例

import com.alibaba.fastjson.JSON;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * redis操作五种数据类型:String、List、Hash、Set、Sorted Set
 */
@SpringBootTest
public class RedisTests {

    @Autowired
    StringRedisTemplate template;

    @Test
    public void addData() {
        //添加字符串数据
        template.opsForValue().set("kstr1", "str1");
        template.opsForValue().set("kstr2", "str2");
        template.opsForValue().append("kstr2", " append");
        //添加列表数据[leftPush添加到当前集合左边]
        template.opsForList().leftPush("klist", "akali");
        template.opsForList().leftPush("klist", "annie");
        template.opsForList().rightPush("klist", "ashe");
        //添加字典数据
        template.opsForHash().put("person", "name", "akali");
        template.opsForHash().put("person", "age", "20");
        //添加集合数据
        template.opsForSet().add("kset", "akali");
        template.opsForSet().add("kset", "amumu");
        template.opsForSet().add("kset", "amumu");
        //添加有序集合数据
        template.opsForZSet().add("kzset", "akali", 2);
        template.opsForZSet().add("kzset", "ashe", 3);
        template.opsForZSet().add("kzset", "ashe", 1);
    }

    @Test
    public void showData() {
        //打印字符串数据
        System.out.println(template.opsForValue().get("kstr1"));
        System.out.println(template.opsForValue().get("kstr2"));
        //打印列表集合
        System.out.println(template.opsForList().range("klist", 0, -1));
        //打印字典数据
        System.out.println(JSON.toJSONString(template.opsForHash().entries("person")));
        //打印集合数据
        System.out.println(JSON.toJSONString(template.opsForSet().members("kset")));
        //打印有序集合数据
        System.out.println(JSON.toJSONString(template.opsForZSet().range("kzset", 0, -1)));
    }

    /**清空redis所有数据*/
    @Test
    public void clear() {
        template.delete(Objects.requireNonNull(template.keys("*")));
    }

    /**查询某键是否存在*/
    @Test
    public void exists() {
        System.out.println("hasKey:" + template.hasKey("person"));
    }

    /**设置某键失效时间*/
    /*立即取值:ex   sleep450毫秒后取值:ex    sleep500毫秒后取值:null*/
    @Test
    public void expire() throws InterruptedException {
        template.opsForValue().set("kexpire", "ex");
        template.expire("kexpire", 500, TimeUnit.MILLISECONDS);
        System.out.println("立即取值:" + template.opsForValue().get("kexpire"));
        Thread.sleep(450);
        System.out.println("sleep450毫秒后取值:" + template.opsForValue().get("kexpire"));
        Thread.sleep(500);
        System.out.println("sleep500毫秒后取值:" + template.opsForValue().get("kexpire"));
    }


    /**常见redis缓存存取使用示例:缓存存在从redis中取值,不存在从数据库中取值,取值后缓存至redis*/
    @Service
    private static class DemoService {
        @Autowired
        StringRedisTemplate userTemplate;

        public String getUsername(String id) {
            String username = "";
            if (userTemplate.hasKey(id)) {
                username = userTemplate.opsForValue().get(id);
            } else {
                //mapper数据库查询select .. where ID=#{id}
                //取值后缓存键值对到redis中
            }
            return username;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值