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的依赖 --> < ...
随机推荐
- MySQL单机安装
操作系统:CentOS 7 MySQL:5.6 MySQL的卸载 查看MySQL软件 卸载MySQL 查看是否还有 MySQL 软件,有的话继续删除. 安装MySQL 启动MySQL 设置root用户 ...
- Java中的<< 和 >> 和 >>> 分析理解
Java中的<< 和 >> 和 >>> 详细分析 <<表示左移移,不分正负数,低位补0: 注:以下数据类型默认为byte-8位 左移时不管正负,低 ...
- ubuntu18.04安装mysql以及重置密码创建新用户
1.安装mysqlsudo apt-get install mysql-serversudo apt-get install mysql-clientsudo apt-get install libm ...
- QT 多线程程序设计 -互斥
QT通过三种形式提供了对线程的支持.它们分别是,一.平台无关的线程类,二.线程安全的事件投递,三.跨线程的信号-槽连接.这使得开发轻巧的多线程Qt程序更为容易,并能充分利用多处理器机器的优势.多线程编 ...
- sentinel控制台监控数据持久化【MySQL】
根据官方wiki文档,sentinel控制台的实时监控数据,默认仅存储 5 分钟以内的数据.如需持久化,需要定制实现相关接口. https://github.com/alibaba/Sentinel/ ...
- mac mysql重置root用户密码
苹果机安装的MySQL后,设置初始密码 引子:.在苹果机上安装的MySQL之后,通过MySQLWorkBench登录本地数据连接,发现没有密码,而在安装MySQL的过程中,是没有设置过密码的其实,刚刚 ...
- 在Eclipse中使用WindowBuilder设计Swing程序
在Eclipse中使用WindowBuilder设计Swing程序 Swing程序表示Java的客户端窗体程序,除了通过手动编写代码的方式设计Swing程序之外,Eclipse中还提供了一种W ...
- 九十一:CMS系统之cms用户模型定义
数据库信息 DEBUG = TrueSQLALCHEMY_DATABASE_URI = 'mysql+pymysql://root:123456@127.0.0.1:3306/test'SQLALCH ...
- ssm整合用到的依赖jar包(不充足)
<!--spring 的核心的jar包--><dependency> <groupId>org.springframework</groupId> &l ...
- JAVA 基础编程练习题13 【程序 13 根据条件求数字】
13 [程序 13 根据条件求数字] 题目:一个整数,它加上 100 后是一个完全平方数,再加上 168 又是一个完全平方数,请问该数是多少? 程序分析:在 10 万以内判断,先将该数加上 100 后 ...