springboot指定redis库编号配置实现
yml配置
spring:
redis:
database: #shiro
host: 127.0.0.1
port:
timeout:
password: null
redis-cache:
database: #字符串缓存
host: 127.0.0.1
port:
timeout:
password: null
maxTotal:
maxIdle:
minIdle:
redis配置类
import org.springframework.beans.factory.annotation.Value;
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.StringRedisTemplate;
import org.springframework.util.StringUtils; import redis.clients.jedis.JedisPoolConfig; @Configuration
public class RedisCacheConfig { @Value("${spring.redis-cache.host}")
private String host; // ip
@Value("${spring.redis-cache.port}")
private int port; // 端口
@Value("${spring.redis-cache.password}")
private String password; // 密码
@Value("${spring.redis-cache.database}")
private int database;//数据库索引
@Value("${spring.redis-cache.timeout}")
private int timeout; //链接超时
@Value("${spring.redis-cache.maxIdle}")
private int maxIdle;// 最大空闲连接
@Value("${spring.redis-cache.minIdle}")
private int minIdle;// 最小空闲连接
@Value("${spring.redis-cache.maxTotal}")
private int maxTotal;// 连接池最大连接数(使用负值表示没有限制)
//注入字符串缓存实例,同理可注入Object缓存实例等等
@Bean(name = "cacheTemplate")
public StringRedisTemplate redisTemplate() {
StringRedisTemplate temple = new StringRedisTemplate();
temple.setConnectionFactory(connectionFactory());
return temple;
} public RedisConnectionFactory connectionFactory() {
JedisConnectionFactory jedis = new JedisConnectionFactory();
jedis.setHostName(host);
jedis.setPort(port);
jedis.setTimeout(timeout);
if (!StringUtils.isEmpty(password)) {
jedis.setPassword(password);
}
if (database != 0) {
jedis.setDatabase(database);
}
jedis.setPoolConfig(poolCofig());
// 初始化连接pool
jedis.afterPropertiesSet(); return jedis;
} public JedisPoolConfig poolCofig() {
JedisPoolConfig poolCofig = new JedisPoolConfig();
poolCofig.setMaxIdle(maxIdle);
poolCofig.setMinIdle(minIdle);
poolCofig.setMaxTotal(maxTotal);
return poolCofig;
}
}
缓存服务接口和实现
public interface IStringCacheService { boolean set(String key, String value); boolean set(String key, String value, long secondTime); boolean set(String key, String value, int hourTime); boolean delete(String... key); boolean hasKey(String key); boolean update(String key, String value); boolean update(String key, String value,long time); String get(String key); long getExpire(String key); boolean setExpire(String key,long time);
} import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service; import lombok.extern.slf4j.Slf4j; @Slf4j
@Service
public class StringCacheServiceImpl implements IStringCacheService {
@Autowired
@Qualifier("cacheTemplate")
private StringRedisTemplate cacheTemplate; @Override
public boolean set(String key, String value, long secondTime) {
try {
cacheTemplate.opsForValue().set(key, value, secondTime, TimeUnit.SECONDS);
return true;
} catch (Exception e) {
log.error(e.toString());
}
return false;
} @Override
public boolean set(String key, String value, int hourTime) {
try {
cacheTemplate.opsForValue().set(key, value, hourTime, TimeUnit.HOURS);
return true;
} catch (Exception e) {
log.error(e.toString());
}
return false;
} @Override
public boolean hasKey(String key) {
try {
return cacheTemplate.hasKey(key);
} catch (Exception e) {
log.error(e.toString());
}
return false;
} @Override
public long getExpire(String key) {
return cacheTemplate.getExpire(key, TimeUnit.SECONDS);
} @Override
public boolean setExpire(String key, long time) {
try {
if (time > 0) {
return cacheTemplate.expire(key, time, TimeUnit.SECONDS);
}
} catch (Exception e) {
log.error(e.toString());
}
return false;
} @Override
public boolean set(String key, String value) {
try {
cacheTemplate.opsForValue().set(key, value);
return true;
} catch (Exception e) {
log.error(e.toString());
}
return false;
} @Override
public boolean delete(String... key) {
try {
if (key != null && key.length > 0) {
if (key.length == 1) {
cacheTemplate.delete(key[0]);
} else {
cacheTemplate.delete(Arrays.asList(key));
}
}
return true;
} catch (Exception e) {
log.error(e.toString());
}
return false;
} @Override
public boolean update(String key, String value) {
return set(key, value);
} @Override
public String get(String key) {
return key == null ? null : cacheTemplate.opsForValue().get(key);
} @Override
public boolean update(String key, String value, long time) {
return set(key, value, time);
} }
springboot指定redis库编号配置实现的更多相关文章
- springBoot整合redis(作缓存)
springBoot整合Redis 1,配置Redis配置类 package org.redislearn.configuration; import java.lang.reflect.Method ...
- springBoot集成Redis遇到的坑(择库)源码分析为什么择库失败
提示: springboot提供了一套链接redis的api,也就是个jar包,用到的连接类叫做LettuceConnectionConfiguration,所以我们引入pom时是这样的 <de ...
- 九、springboot整合redis二之缓冲配置
1.创建Cache配置类 @Configuration @EnableCaching public class RedisCacheConfig extends CachingConfigurerSu ...
- springboot系列十、springboot整合redis、多redis数据源配置
一.简介 Redis 的数据库的整合在 java 里面提供的官方工具包:jedis,所以即便你现在使用的是 SpringBoot,那么也继续使用此开发包. 二.redidTemplate操作 在 Sp ...
- 输出redis cluster集群所有节点指定的参数的配置
需要:实现类似redis-trib.rb call 命令的功能,输出redis cluster集群所有节点指定的参数的配置 redis-trib.rb的输出 [redis@lxd-vm3 ~]$ re ...
- redis 指定db库导入导出数据
最近根据之前的项目重新改编一个新的项目,发现上一个项目的搭建者,把一些区域权限和划分放在redis上存储,因此不得不照搬过来,所以搜索一下相关如何做的 发现一个比较简单的做法,记录一下操作过程,方便以 ...
- Redis-基本概念、java操作redis、springboot整合redis,分布式缓存,分布式session管理等
NoSQL的引言 Redis数据库相关指令 Redis持久化相关机制 SpringBoot操作Redis Redis分布式缓存实现 Resis中主从复制架构和哨兵机制 Redis集群搭建 Redis实 ...
- SpringBoot系列: Redis 共享Session
Web项目Session管理是一个很重要的话题, 涉及到系统横向扩展, SpringBoot已经为共享Session很好的解决方案, 这篇文章关注使用Redis共享会话, 同时这也是最常用的方法. = ...
- 【SpringBoot | Redis】SpringBoot整合Redis
SpringBoot整合Redis 1. pom.xml中引入Redis相关包 请注意,这里我们排除了lettuce驱动,采用了jedis驱动 <!-- redis的依赖 --> < ...
随机推荐
- C++ UFT-8和GB2312间的转换
在这个帖子找到的代码 还蛮好用的 https://bbs.csdn.net/topics/391040755 #include <codecvt> #include <locale& ...
- Java-Unsafe
Unsafe 是 sun.misc 包下的一个类,可以直接操作堆外内存,可以随意查看及修改 JVM 中运行时的数据,使 Java 语言拥有了类似 C 语言指针一样操作内存空间的能力. Unsafe 的 ...
- MySQL5.7 创建及查看数据库
1.创建数据库语句create database语句是在MySQL实例上创建一个指定名称的数据库.create schema语句的语义和create database是一样的. 2.语法解析 CREA ...
- vue-判断设备是手机端还是pc端
经常在项目中会有支持 pc 与手机端需求.并且pc与手机端是两个不一样的页面.这时就要求判断设置,根据不同的设置跳转不同的路由. [代码演示] 在 router/index.js 中有两个页面. ex ...
- 用hugo搭建个人博客
这几天研究了用hugo搭建个人博客. 简单的整理了一下. 1.安装hugo(windows 请查看官网介绍 https://gohugo.io/getting-started/installing/) ...
- Module ngx_http_rewrite_module
http://nginx.org/en/docs/http/ngx_http_rewrite_module.html Directives break if return ...
- Mysql 查询当前数据上一条和下一条的记录
获取当前文件上一条与下一条记录的原理是上一条的sql语句,从news表里按从大到小的顺序选择一条比当前ID小的新闻,下一条的sql语句,从news表里按从小到大的顺序选择一条比当前ID大的新闻. 如果 ...
- LVS系列二、LVS集群-DR模式
一. LVS-DR和LVS-IP TUN集群概述 1. Direct Routing(直接路由) Director分配请求到不同的real server.real server处理请求后直接回应给用 ...
- Hadoop集群安装压缩工具Snappy,用于Hbase
最近项目中要用到Hadoop和Hbase,为了节省服务器的存储成本,并提高吞吐,安装并开启HBase的数据压缩为Snappy. 主流的HBase压缩方式有GZip | LZO | Snappy,Sna ...
- 7. grep
grep命令 grep grep是通用正则表达式分析程序(General Regular Expression Parser)的缩写. grep命令可以在它的输入中搜索指定的字符串模式(Pattern ...