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"));
} }

最新文章

  1. 帮朋友急招PHP、Android开发工程师 西安 工资8k-12k
  2. Sqlserver_In、exists使用
  3. 管道和FIFO
  4. context.Response.End()的用法和本质
  5. MyEclipse x.x各版本终极优化配置指南
  6. ASP.net中网站访问量统计方法
  7. Anaroid WebView详解大全
  8. windows10 配置 华为vpn客户端
  9. KVM管理平台openebula安装
  10. sql server两个时间段内,求出周末的量
  11. 盒模型、position、float详解css重点汇总
  12. 如何高效地写CSS--等以后有空多加总结一下
  13. (原创)odoo在docker环境下无法备份
  14. mysqlbinlog恢复数据注意事项【转】
  15. ios开发之--比较两个数组里面的值是否相同
  16. Vue 的style绑定显示background-image
  17. orientdb 图数据库docker 安装试用
  18. log4j-over-slf4j工作原理详解
  19. MySQL 查询结果分组 group by
  20. winform 删除,清空指定文件夹上的所有文件或文件夹

热门文章

  1. Git-git push -u为何第二次不用指定-u?
  2. 【.Net】 大文件可使用的文本分组统计工具(附带源码,原创)
  3. CentOS 7 查询yum安装的软件及路径
  4. [费用流][BZOJ1070]修车
  5. Apache Beam是什么?
  6. The current state of generics in Delphi( 转载)
  7. 中标麒麟(linux)下Qt调用python
  8. fcitx4.2.0自定义中文标点符号
  9. 1-学习tecplot360
  10. Django Model Form