SpringBoot2 中使用 Redis

本贴最后更新于 2070 天前,其中的信息可能已经天翻地覆

前言

简介

SpringBoot2 中,使用 Redis,官方提供了两个 starter:

  • spring-boot-starter-data-redis
  • spring-boot-starter-data-redis-reactive

其实 spring-boot-starter-data-redis-reactive 中也仅依赖了 spring-boot-starter-data-redis

第一个例子

由于 SpringBoot 的特点,所以上手是最容易的。

依赖

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

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

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

Controller

package com.example.testredis;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    private static final Logger logger = LoggerFactory.getLogger(TestController.class);

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @RequestMapping("/set")
    public void set(String key, String value) {
        stringRedisTemplate.opsForValue().set(key, value);
    }

    @RequestMapping("/get")
    public String show(String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }

    @RequestMapping("/set2")
    public void set2(String key, String value) {
        // 可选,自定义序列化方法
        stringRedisTemplate.execute(new RedisCallback<Boolean>() {
            @Override
            public Boolean doInRedis(RedisConnection redisConnection) throws DataAccessException {
                RedisSerializer<String> redisSerializer = redisTemplate.getStringSerializer();
                byte[] serializeKey = redisSerializer.serialize(key);
                byte[] serializeValue = redisSerializer.serialize(value);
                return redisConnection.setNX(serializeKey, serializeValue);
            }
        });
    }
}

执行

$ curl -s "localhost:8080/get?key=host"
note.abeffect.com

$ curl -s "localhost:8080/set?key=key&value=abeffect"

$ curl -s "localhost:8080/get?key=key"
abeffect

$ curl -s "localhost:8080/set2?key=key&value=abeffect"

$ curl -s "localhost:8080/get?key=key"
abeffect

参考

  • 数据库

    据说 99% 的性能瓶颈都在数据库。

    330 引用 • 614 回帖 • 3 关注
  • Java

    Java 是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由 Sun Microsystems 公司于 1995 年 5 月推出的。Java 技术具有卓越的通用性、高效性、平台移植性和安全性。

    3167 引用 • 8207 回帖

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...