返回介绍

Spring Boot 中使用 Redis

发布于 2025-02-18 23:57:57 字数 3850 浏览 0 评论 0 收藏 0

Spring Boot 中除了对常用的关系型数据库提供了优秀的自动化支持之外,对于很多 NoSQL 数据库一样提供了自动化配置的支持,包括:Redis, MongoDB, Elasticsearch, Solr 和 Cassandra。

准备

环境安装

任选其一

CentOs7.3 搭建 Redis-4.0.1 单机服务

CentOs7.3 搭建 Redis-4.0.1 Cluster 集群服务

测试用例

Github 代码

代码我已放到 Github ,导入 spring-boot-examples 项目

github https://github.com/souyunku/spring-boot-examples/tree/master/spring-boot-redis

添加依赖

在项目中添加 spring-boot-starter-data-redis 依赖

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

配置 RedisTemplate 实例

@Configuration
public class RedisConfig {

  private Logger LOG = LoggerFactory.getLogger(RedisConfig.class);

  @Bean
  JedisConnectionFactory jedisConnectionFactory() {
    return new JedisConnectionFactory();
  }

  @Bean
  public RedisTemplate<String, String> redisTemplate() {
    RedisTemplate<String, String> template = new RedisTemplate<String, String>();
    template.setConnectionFactory(jedisConnectionFactory());
    template.setKeySerializer(new StringRedisSerializer());
    LOG.info("create RedisTemplate success");
    return template;
  }
}

配置参数

application.properties

# Redis 数据库索引(默认为 0)
spring.redis.database=0
# Redis 服务器地址
spring.redis.host=127.0.0.1
# Redis 服务器连接端口
spring.redis.port=6379
# Redis 服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=0

操作 Redis 工具类

public class CacheUtils {

  @Resource
  private RedisTemplate<String, String> redisTemplate;
  private static CacheUtils cacheUtils;

  @PostConstruct
  public void init() {
    cacheUtils = this;
    cacheUtils.redisTemplate = this.redisTemplate;
  }

  /**
   * 保存到 hash 集合中
   *
   * @param hName 集合名
   * @param key
   * @param value
   */
  public static void hashSet(String hName, String key, String value) {
    cacheUtils.redisTemplate.opsForHash().put(hName, key, value);
  }

  /**
   * 从 hash 集合里取得
   *
   * @param hName
   * @param key
   * @return
   */

  public static Object hashGet(String hName, String key) {
    return cacheUtils.redisTemplate.opsForHash().get(hName, key);
  }

  /**
   省略 N 多方法
   。。。。。。
   */
}

注册配置类到容器

@Configuration
@Import({RedisConfig.class, CacheUtils.class})
public class RedisAutoConfiguration {

}

单元测试

import io.ymq.redis.utils.CacheUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import io.ymq.redis.run.Application;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * 描述:测试类
 *
 * @author yanpenglei
 * @create 2017-10-16 13:18
 **/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class BaseTest {

  @Test
  public void test() throws Exception {

    CacheUtils.hashSet("test", "ymq", "www.ymq.io");

    System.out.println(CacheUtils.hashGet("test", "ymq"));
  }
  
}

代码我已放到 Github ,导入 spring-boot-examples 项目

github https://github.com/souyunku/spring-boot-examples/tree/master/spring-boot-redis

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文