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库编号配置实现的更多相关文章

  1. springBoot整合redis(作缓存)

    springBoot整合Redis 1,配置Redis配置类 package org.redislearn.configuration; import java.lang.reflect.Method ...

  2. springBoot集成Redis遇到的坑(择库)源码分析为什么择库失败

    提示: springboot提供了一套链接redis的api,也就是个jar包,用到的连接类叫做LettuceConnectionConfiguration,所以我们引入pom时是这样的 <de ...

  3. 九、springboot整合redis二之缓冲配置

    1.创建Cache配置类 @Configuration @EnableCaching public class RedisCacheConfig extends CachingConfigurerSu ...

  4. springboot系列十、springboot整合redis、多redis数据源配置

    一.简介 Redis 的数据库的整合在 java 里面提供的官方工具包:jedis,所以即便你现在使用的是 SpringBoot,那么也继续使用此开发包. 二.redidTemplate操作 在 Sp ...

  5. 输出redis cluster集群所有节点指定的参数的配置

    需要:实现类似redis-trib.rb call 命令的功能,输出redis cluster集群所有节点指定的参数的配置 redis-trib.rb的输出 [redis@lxd-vm3 ~]$ re ...

  6. redis 指定db库导入导出数据

    最近根据之前的项目重新改编一个新的项目,发现上一个项目的搭建者,把一些区域权限和划分放在redis上存储,因此不得不照搬过来,所以搜索一下相关如何做的 发现一个比较简单的做法,记录一下操作过程,方便以 ...

  7. Redis-基本概念、java操作redis、springboot整合redis,分布式缓存,分布式session管理等

    NoSQL的引言 Redis数据库相关指令 Redis持久化相关机制 SpringBoot操作Redis Redis分布式缓存实现 Resis中主从复制架构和哨兵机制 Redis集群搭建 Redis实 ...

  8. SpringBoot系列: Redis 共享Session

    Web项目Session管理是一个很重要的话题, 涉及到系统横向扩展, SpringBoot已经为共享Session很好的解决方案, 这篇文章关注使用Redis共享会话, 同时这也是最常用的方法. = ...

  9. 【SpringBoot | Redis】SpringBoot整合Redis

    SpringBoot整合Redis 1. pom.xml中引入Redis相关包 请注意,这里我们排除了lettuce驱动,采用了jedis驱动 <!-- redis的依赖 --> < ...

随机推荐

  1. CISCO实验记录二:路由器基本操作

    一.路由器基本操作要求 1.设置路由器本地时间 2.启用光标跟随 3.设置路由器标语信息和描述信息 4.为接口配置描述信息 5.快速恢复接口到出厂设置 二.路由器基本操作命令 1.设置路由器本地时间 ...

  2. Linux设备驱动程序 之 字符设备的注册

    内核内部使用struct cdev结构来标识字符设备,在内核调用设备的操作之前,必须分配并注册一个或者多个上述结构,为此,我们的代码需要包含<linux/cdev.h>,其中定义了这个结构 ...

  3. 用gcov来检查Qt C++程序的代码覆盖率

      最近才发现MinGW里面包含一个叫做gcov的工具,可以用来检查你的程序运行时调用了哪些代码,同时显示代码行被调用的次数.这个功能在代码的覆盖率和性能调优方便都能用上. 我的运行环境 Window ...

  4. 使用 Jenkins 发布 web 应用至 Tomcat

    Jenkins 的安装:https://www.cnblogs.com/jhxxb/p/11406805.html 发布流程:Git -> Maven -> Tomcat 一.基本环境 G ...

  5. Undo Segment/Undo Retention

    undo_retention简单定义,就是最多数据的最少保留时间.AUM模式下,undo_retention参数用于事务commit后undo数据保留的时间.单位为秒.这是个no guarantee的 ...

  6. Kbengine游戏引擎-【5】用Dockerfile打包镜像kbengine

    本文是以docker为例,以ubuntu 16.04做基础镜像 kengine 1.0.0 用supervisor来管理启动 先放下目录结构图: kb--里面放的是kbengine的编译后的引擎以及d ...

  7. 发布机制-灰度发布-例子:QZone

    ylbtech-发布机制-灰度发布-例子:QZone QZone是另外一个采用灰度发布的例子.大家都知道,QZone在过去的一年中改进是巨大的,从以前慢悠悠的老爷爷变成了一个充满青春活力的小伙子.其中 ...

  8. <application>节点属性

    1.android:allowBackup 它表示是否允许应用程序参与备份.如果将该属性设置为false,则即使备份整个系统,也不会执行这个应用程序的备份操作,而整个系统备份能导致所有应用程序数据通过 ...

  9. 语法错误 : 缺少“;”(在“<”的前面)

    该错误有可能是由错误所指行“<”附近的某个类型忘记#include <>所造成的

  10. Django学习之缓存和信号

    Django学习之缓存和信号   一 缓存 由于Django是动态网站,所有每次请求均会去数据进行相应的操作,当程序访问量大时,耗时必然会更加明显,最简单解决方式是使用:缓存,缓存将一个某个views ...