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的依赖 --> < ...
随机推荐
- nginx部署前端项目
1.在阿里云服务器上安装nginx,推荐使用yum安装 yum install -y nginx // 命令安装 nginx 服务器 2.配置nginx 安装完成后,进入 nginx 配置文件目录 一 ...
- mysql if--else
SQL之case when then用法 case具有两种格式.简单case函数和case搜索函数. --简单case函数 case sex when '1' then '男' when '2' th ...
- leetcode30 串联所有单词的子串
先对words中的单词排列组合,然后对s滑窗操作:部分样例超时,代码如下: class Solution { public: vector<int> findSubstring(strin ...
- Win10蓝牙鼠标老是断连卡顿的解决方法
一直用一个微软家的蓝牙鼠标,饱受鼠标卡顿困扰,今天找到了一个解决方案,用了下,效果显著.具体操作见下文. 原文地址:https://jingyan.baidu.com/article/c85b7a64 ...
- 14 statefulset (sts)控制器
statefulset (sts)控制器 可以用于部署有状态的服务,比如说redis,mysql ,zk等等... 1. 稳定且唯一的网络标志符:2. 稳定且持久的存储3. 有序,平滑地部署和扩展:4 ...
- springboot自定义filter获取spring容器bean对象
今天在自己定义的filter中,想要直接注入spring容器的其它bean进行操作,发现不能正常的注入: 原因:web容器加载顺序导致, 加载顺序是listener——filter——servlet, ...
- Fiddler抓取https设置详解(图文)
本文主要说明了自己在设置fiddler抓取https过程中所遇到的问题及解决步骤,特别是fiddler在设置证书的环节遇到的各种奇葩问题,特此分享! 声明:本文为原创文章,转载请注明来源:https: ...
- dtcms 手机浏览
private string GetSitePath(string webPath, string requestPath, string requestDomain) { //获取当前域名包含的站点 ...
- mysql 添加表情 Incorrect string value: '\xF0\x9F\x98\x8
方法1:使用utf8mb4的mysql编码来容纳这些字符 注意:要使用utf8mb4类型,首先要保证Mysql版本要不低于 MySQL 5.5.3. 第一步:修改my.ini配置文件 [mysql]# ...
- 使用Postman如何做接口自动化测试
师从‘百测’! 一.简介 Postman是一款非常流行的API调试工具,很多攻城狮都应该用过,或听说过,这里不做过多介绍. 官方网站:http://www.getpostman.com 二.接口自动化 ...