1、引入 spring-boot-starter-redis

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

2、application.yml配置redis信息

spring:
redis:
host: 127.0.0.1
port: 6379
password:
pool:
max-active: 100
max-idle: 10
max-wait: 100000
timeout: 0

3、集成Redis
基于JedisPool配置,使用RedisTemplate来操作redis的方式。

a、RedisConfig.java

package com.lynch.redis;

import java.lang.reflect.Method;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer; import redis.clients.jedis.JedisPoolConfig; @Configuration
@EnableAutoConfiguration
public class RedisConfig { /**
* 获取JedisPoolConfig配置
*
* @return
*/
@Bean
@ConfigurationProperties(prefix = "spring.redis.pool")
public JedisPoolConfig getRedisConfig(){
JedisPoolConfig config = new JedisPoolConfig();
return config;
} /**
* 获取JedisConnectionFactory工厂
*
* @return
*/
@Bean
@ConfigurationProperties(prefix = "spring.redis")
public JedisConnectionFactory getConnectionFactory() {
JedisConnectionFactory factory = new JedisConnectionFactory();
factory.setUsePool(true);
JedisPoolConfig config = getRedisConfig();
factory.setPoolConfig(config);
return factory;
} /**
* 获取RedisTemplate模板
*
* @return
*/
@Bean
public RedisTemplate<?, ?> getRedisTemplate() {
JedisConnectionFactory factory = getConnectionFactory();
RedisTemplate<?, ?> template = new StringRedisTemplate(factory);
return template;
} }

@Configuration注解 用于定义配置类,可替换xml配置文件,被注解的类内部包含有一个或多个被@Bean注解的方法,这些方法将会被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext类进行扫描,并用于构建bean定义,初始化Spring容器。

@EnableAutoConfiguration注解
启用Spring应用程序上下文的自动配置,尝试猜测和配置您可能需要的bean。自动配置类通常基于类路径和定义的bean应用。

@ConfigurationProperties注解
用于读取配置文件的信息,在这里是读取配置在yml里的redis的相关配置项。

@Bean注解
用在方法上,告诉Spring容器,你可以从下面这个方法中拿到一个Bean

b、RedisService.java

package com.lynch.redis;

public interface RedisService {

    /**
* set存数据
* @param key
* @param value
* @return
*/
boolean set(String key, String value); /**
* get获取数据
* @param key
* @return
*/
String get(String key); /**
* 设置有效天数
* @param key
* @param expire
* @return
*/
boolean expire(String key, long expire); /**
* 移除数据
* @param key
* @return
*/
boolean remove(String key); }

c、RedisServiceImpl.java

package com.lynch.redis;

import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

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.serializer.RedisSerializer;
import org.springframework.stereotype.Service; @Service
public class RedisServiceImpl implements RedisService { @Resource
private RedisTemplate<String, ?> redisTemplate; @Override
public boolean set(final String key, final String value) {
boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
@Override
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
connection.set(serializer.serialize(key), serializer.serialize(value));
return true;
}
});
return result;
} @Override
public String get(final String key) {
String result = redisTemplate.execute(new RedisCallback<String>() {
@Override
public String doInRedis(RedisConnection connection) throws DataAccessException {
RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
byte[] value = connection.get(serializer.serialize(key));
return serializer.deserialize(value);
}
});
return result;
} @Override
public boolean expire(final String key, long expire) {
return redisTemplate.expire(key, expire, TimeUnit.SECONDS);
} @Override
public boolean remove(final String key) {
boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
@Override
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
connection.del(key.getBytes());
return true;
}
});
return result;
}
}

4、Redis测试

package com.lynch.config;

import org.junit.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 com.lynch.redis.RedisService; @RunWith(SpringRunner.class)
@SpringBootTest
public class RedisServiceTest {
@Autowired
private RedisService redisService; @Test
public void contextLoads() {
} @Test
public void setString() {
redisService.set("redis_string_test", "springboot redis!中国");
} @Test
public void getString() {
String result = redisService.get("redis_string_test");
System.out.println(result);
} @Test
public void remove() {
System.out.println(redisService.remove("redis_string_test"));
} }

SpringBoot集成Redis的更多相关文章

  1. 【springBoot】springBoot集成redis的key,value序列化的相关问题

    使用的是maven工程 springBoot集成redis默认使用的是注解,在官方文档中只需要2步; 1.在pom文件中引入即可 <dependency> <groupId>o ...

  2. SpringBoot集成redis的key,value序列化的相关问题

    使用的是maven工程 springBoot集成redis默认使用的是注解,在官方文档中只需要2步; 1.在pom文件中引入即可 <dependency> <groupId>o ...

  3. springboot集成redis(mybatis、分布式session)

    安装Redis请参考:<CentOS快速安装Redis> 一.springboot集成redis并实现DB与缓存同步 1.添加redis及数据库相关依赖(pom.xml) <depe ...

  4. Windows环境下springboot集成redis的安装与使用

    一,redis安装 首先我们需要下载Windows版本的redis压缩包地址如下: https://github.com/MicrosoftArchive/redis/releases 连接打开后如下 ...

  5. springBoot集成Redis遇到的坑(择库)源码分析为什么择库失败

    提示: springboot提供了一套链接redis的api,也就是个jar包,用到的连接类叫做LettuceConnectionConfiguration,所以我们引入pom时是这样的 <de ...

  6. SpringBoot | 集成Redis

    Windows下安装: https://github.com/MicrosoftArchive/redis/releases zip下就解包到自定义目录下,msi就跟着步骤安装 进入安装目录下运行命令 ...

  7. springboot集成redis使用redis作为session报错ClassNotFoundException类RememberMeServices

    springboot 集成redis使用redis作为缓存,会报错的问题. 错误信息: java.lang.IllegalStateException: Error processing condit ...

  8. Springboot集成Redis步骤

    Spring boot 集成Redis的步骤如下: 1.在pom.xml中配置相关的jar依赖: <!--加载spring boot redis包 --> <dependency&g ...

  9. SpringBoot集成Redis分布式锁以及Redis缓存

    https://blog.csdn.net/qq_26525215/article/details/79182687 集成Redis 首先在pom.xml中加入需要的redis依赖和缓存依赖 < ...

随机推荐

  1. thinkphp 视图(二)变量输出、赋值和替换

    view下的html文件会编译成php文件 编译的文件在runtime 下的temp目录 <p>{$email}</p> 会编译成 <?php echo $email; ...

  2. Zookeeper 集群配置及启动

    准备工作 1. 集群机器 192.168.8.2 192.168.8.6 192.168.8.11 2. 包 zookeeper-3.4.10.tar.gz 集群配置 1. 解压路径 192.168. ...

  3. python基础之Day7part1集合

    一.集合 1.定义 s=set() 2.特点 每个元素必须是不可变类型,但集合本身是可变类型的,有add和remove等功能 3.用途 去重(原理:for循环if判断元素是否已存在,不存在则追加) 关 ...

  4. ScrollView嵌套Linearlayout显示不全的解决办法

    以为ScrollView只能嵌套一个元素,所以把几个控件都包裹在了一个LinearLayout中了.但是发现底部显示不全,滑动不到最底下. 代码: <ScrollView android:id= ...

  5. vue使用路由跳转到上一页

    this.$router.go(-1) <template> <div> <button class="btn btn-success" @click ...

  6. c语言模拟c++的继承和多态

    //C++中的继承与多态 struct A { virtual void fun() //C++中的多态:通过虚函数实现 { cout << "A:fun()" < ...

  7. std::string的拷贝赋值研究

    说明:以下涉及的std::string的源代码摘自4.8.2版本.结论:std::string的拷贝复制是基于引用计数的浅拷贝,因此它们指向相同的数据地址. // std::string类定义type ...

  8. 通过MFC设计一个简单的计价程序

    1.实验目的 掌握使用MFC应用程序向导创建应用程序的方法. 掌握新建对话框资源的方法. 掌握生成对话框的方法. 2.实验内容 用应用程序创建一个默认的对话框应用程序,在对话框中能进入下一个对话框,在 ...

  9. Awake()跟Start()差在哪?

    刚开始学Unity的时候,最难搞定的就是这两个functions的差异,依照官方文件所描述的: Awake(): Awake is called when the script instance is ...

  10. 【转】学习Java虚拟机没用? 听听当事人是怎么说的!

    我是大名鼎鼎的Java 虚拟机,  据说这个星球上每天有900多万程序员和我打交道,这真是一个惊人的数字. 这900多万人中不少人对我的技术内幕非常感兴趣, 有事儿没事儿都要把我“大卸八块”, 深入了 ...