SpringBoot1.0版本2.0版本配置redis

pom 依赖

1<dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-data-redis</artifactId> 4 </dependency> 5 <dependency> 6 <groupId>redis.clients</groupId> 7 <artifactId>jedis</artifactId> 8 </dependency>

yml文件配置

1#springboot版本为2:0:2RELEASE中: 2 redis: 3 # Redis数据库索引(默认为04 database: 10 5 # Redis服务器地址 6 host: localhost 7 # Redis服务器连接端口 8 port: 6379 9 # Redis服务器连接密码(默认为空) 10 password: wiki123 11 timeout: 3 12 jedis: 13 pool: 14 max-active: 8 # 连接池最大连接数(使用负值表示没有限制) 15 max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制) 16 max-idle: 8 # 连接池中的最大空闲连接 17 min-idle: 0 # 连接池中的最小空闲连接 18 19#redis配置 springboot版本为1.3.2RELEASE中的RedisProperties配置文件类 20# redis: 21# # Redis数据库索引(默认为022# database: 10 23# # Redis服务器地址 24# host: localhost 25# # Redis服务器连接端口 26# port: 6379 27# # Redis服务器连接密码(默认为空) 28# password: wiki123 29# timeout: 3 30# pool: 31# # 连接池最大连接数(使用负值表示没有限制) 32# max-active: 8 33# # 连接池最大阻塞等待时间(使用负值表示没有限制) 34# max-wait: -1 35# # 连接池中的最大空闲连接 36# max-idle: 8 37# # 连接池中的最小空闲连接 38# min-idle: 0 39# # 连接超时时间(毫秒)

配置redis

1package top.xz.hand.config; 2 3import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 4import org.springframework.boot.context.properties.ConfigurationProperties; 5import org.springframework.context.annotation.Bean; 6import org.springframework.context.annotation.Configuration; 7import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; 8import org.springframework.data.redis.core.RedisTemplate; 9import org.springframework.data.redis.core.StringRedisTemplate; 10import redis.clients.jedis.JedisPoolConfig; 11 12@Configuration 13@EnableAutoConfiguration 14public class RedisConfig { 15 16 /** 17 * 获取JedisPoolConfig配置 18 * 19 * @return 20 */ 21 @Bean 22 @ConfigurationProperties(prefix = "spring.redis.jedis.pool") 23 public JedisPoolConfig getRedisConfig(){ 24 JedisPoolConfig config = new JedisPoolConfig(); 25 return config; 26 } 27 28 /** 29 * 获取JedisConnectionFactory工厂 30 * 31 * @return 32 */ 33 @Bean 34 @ConfigurationProperties(prefix = "spring.redis") 35 public JedisConnectionFactory getConnectionFactory() { 36 JedisConnectionFactory factory = new JedisConnectionFactory(); 37 factory.setUsePool(true); 38 JedisPoolConfig config = getRedisConfig(); 39 factory.setPoolConfig(config); 40 return factory; 41 } 42 43 /** 44 * 获取RedisTemplate模板 45 * 46 * @return 47 */ 48 @Bean 49 public RedisTemplate<?, ?> getRedisTemplate() { 50 JedisConnectionFactory factory = getConnectionFactory(); 51 RedisTemplate<?, ?> template = new StringRedisTemplate(factory); 52 return template; 53 } 54 55}

redis接口

1package top.xz.hand.service; 2 3public interface RedisService { 4 /** 5 * set存数据 6 * @param key 7 * @param value 8 * @return 9 */ 10 boolean set(String key, String value); 11 12 /** 13 * get获取数据 14 * @param key 15 * @return 16 */ 17 String get(String key); 18 19 /** 20 * 设置有效天数 21 * @param key 22 * @param expire 23 * @return 24 */ 25 boolean expire(String key, long expire); 26 27 /** 28 * 移除数据 29 * @param key 30 * @return 31 */ 32 boolean remove(String key); 33 34}

实现

1package top.xz.hand.service.impl; 2 3import org.springframework.dao.DataAccessException; 4import org.springframework.data.redis.connection.RedisConnection; 5import org.springframework.data.redis.core.RedisCallback; 6import org.springframework.data.redis.core.RedisTemplate; 7import org.springframework.data.redis.serializer.RedisSerializer; 8import org.springframework.stereotype.Service; 9import top.xz.hand.service.RedisService; 10 11import javax.annotation.Resource; 12import java.util.concurrent.TimeUnit; 13@Service 14public class RedisServiceImpl implements RedisService { 15 @Resource 16 private RedisTemplate<String, ?> redisTemplate; 17 18 @Override 19 public boolean set(final String key, final String value) { 20 boolean result = redisTemplate.execute(new RedisCallback<Boolean>() { 21 @Override 22 public Boolean doInRedis(RedisConnection connection) throws DataAccessException { 23 RedisSerializer<String> serializer = redisTemplate.getStringSerializer(); 24 connection.set(serializer.serialize(key), serializer.serialize(value)); 25 return true; 26 } 27 }); 28 return result; 29 } 30 31 @Override 32 public String get(final String key) { 33 String result = redisTemplate.execute(new RedisCallback<String>() { 34 @Override 35 public String doInRedis(RedisConnection connection) throws DataAccessException { 36 RedisSerializer<String> serializer = redisTemplate.getStringSerializer(); 37 byte[] value = connection.get(serializer.serialize(key)); 38 return serializer.deserialize(value); 39 } 40 }); 41 return result; 42 } 43 44 @Override 45 public boolean expire(final String key, long expire) { 46 return redisTemplate.expire(key, expire, TimeUnit.SECONDS); 47 } 48 49 @Override 50 public boolean remove(final String key) { 51 boolean result = redisTemplate.execute(new RedisCallback<Boolean>() { 52 @Override 53 public Boolean doInRedis(RedisConnection connection) throws DataAccessException { 54 connection.del(key.getBytes()); 55 return true; 56 } 57 }); 58 return result; 59 } 60}
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

SpringBoot1.0版本2.0版本配置redis - HelloWorld