Redis:java和SpringBoot中使用Redis

目录

  • Jedis操作Redis6
  • SpringBoot整合Redis

Jedis操作Redis6

Jedis是java开发的操作redis的工具包。

引入maven依赖:

<dependency> 
  <groupId>redis.clientsgroupId> 
  <artifactId>jedisartifactId> 
  <version>4.2.1version> 
dependency>

使用redis的api操作redis:

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPubSub;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

public class JedisTest {

    Jedis jedis;

    @Before
    public void before() {
        this.jedis = new Jedis("192.168.200.129", 6379);
    }

    @After
    public void after() {
        //关闭jedis
        this.jedis.close();
    }

    /**
     * 测试redis是否连通
     */
    @Test
    public void test1() {
        String ping = jedis.ping();
        System.out.println(ping);
    }

    /**
     * string类型测试
     */
    @Test
    public void stringTest() {
        jedis.set("site", "http://www.itsoku.com");
        System.out.println(jedis.get("site"));
        System.out.println(jedis.ttl("site"));
    }

    /**
     * list类型测试
     */
    @Test
    public void listTest() {
        jedis.rpush("courses", "java", "spring", "springmvc", "springboot");
        List<String> courses = jedis.lrange("courses", 0, -1);
        for (String course : courses) {
            System.out.println(course);
        }
    }

    /**
     *  zset类型测试
     */
    @Test
    public void setTest() {
        jedis.sadd("users", "tom", "jack", "ready");
        Set<String> users = jedis.smembers("users");
        for (String user : users) {
            System.out.println(user);
        }
    }

    /**
     *  订阅消息
     *
     * @throws InterruptedException
     */
    @Test
    public void subscribeTest() throws InterruptedException {
        //subscribe(消息监听器,频道列表)
        jedis.subscribe(new JedisPubSub() {
            @Override
            public void onMessage(String channel, String message) {
                System.out.println(channel + ":" + message);
            }
        }, "sitemsg");
        TimeUnit.HOURS.sleep(1); }
    }

    /**
     * 发布消息
     */
    @Test
    public void publishTest() {
        jedis.publish("sitemsg", "hello redis");
    }

}

SpringBoot整合Redis

引入redis的maven配置:

<dependency> 
  <groupId>org.springframework.bootgroupId> 
  <artifactId>spring-boot-starter-data-redisartifactId> 
dependency>

application.properties中配置redis信息:

# redis 服务器 ip 
spring.redis.host=192.168.200.129 
# redis 服务器端口 
spring.redis.port=6379 
# redis 密码 
#spring.redis.password=root 
# 连接超时时间(毫秒) 
spring.redis.timeout=60000 
# Redis默认情况下有16个分片,这里配置具体使用的分片,默认是0
spring.redis.database=0

使用RedisTemplate工具类操作redis:

springboot中使用RedisTemplate来操作redis,需要在我们的bean中注入这个对象,代码如下:

@Autowired private RedisTemplate<String, String> redisTemplate; // 用下面5个对象来操作对应的类型 
this.redisTemplate.opsForValue(); //提供了操作string类型的所有方法 
this.redisTemplate.opsForList(); // 提供了操作list类型的所有方法 
this.redisTemplate.opsForSet(); //提供了操作set的所有方法 
this.redisTemplate.opsForHash(); //提供了操作hash表的所有方法 
this.redisTemplate.opsForZSet(); //提供了操作zset的所有方法

RedisTemplate示例代码:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap; import java.util.List;
import java.util.Map; import java.util.Set;
import java.util.concurrent.TimeUnit;


@RestController
@RequestMapping("/redis")
public class RedisController {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @RequestMapping("/stringTest")
    public String stringTest() {
        this.redisTemplate.delete("name");
        this.redisTemplate.opsForValue().set("name", "路人");
        String name = this.redisTemplate.opsForValue().get("name");
        return name;
    }

    @RequestMapping("/listTest")
    public List<String> listTest() {
        this.redisTemplate.delete("names");
        this.redisTemplate.opsForList().rightPushAll("names", "刘德华", "张学友", "郭富城", "黎明");
        List<String> courses = this.redisTemplate.opsForList().range("names", 0, -1);
        return courses;
    }


    @RequestMapping("setTest")
    public Set<String> setTest() {
        this.redisTemplate.delete("courses");
        this.redisTemplate.opsForSet().add("courses", "java", "spring", "springboot");
        Set<String> courses = this.redisTemplate.opsForSet().members("courses");
        return courses;
    }

    @RequestMapping("hashTest")
    public Map<Object, Object> hashTest() {
        this.redisTemplate.delete("userMap");
        Map<String, String> map = new HashMap<>();
        map.put("name", "路人"); map.put("age", "30");
        this.redisTemplate.opsForHash().putAll("userMap", map);
        Map<Object, Object> userMap = this.redisTemplate.opsForHash().entries("userMap");
        return userMap;
    }

    @RequestMapping("zsetTest") 
    public Set<String> zsetTest() { 
        this.redisTemplate.delete("languages"); 
        this.redisTemplate.opsForZSet().add("languages", "java", 100d); 
        this.redisTemplate.opsForZSet().add("languages", "c", 95d); 
        this.redisTemplate.opsForZSet().add("languages", "php", 70); 
        Set<String> languages = this.redisTemplate.opsForZSet().range("languages", 0, -1);
        return languages; 
    }

}

你可能感兴趣的:(Redis,java,redis,spring,boot)