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. 在 Go 语言中使用 Session(一)

    在上一篇博客 理解Cookie和Session 中,我们了解了 Cookie 和 Session 的一些基础知识,也知道了 Session 的基本原理是由服务端保存一份状态信息(以及它的唯一标识符), ...

  2. 解决虚拟机上的tomcat无法被主机访问的问题

    在wmware中安装linux后安装好数据库,JDK及tomcat后启动服务,虚拟机中可以访问,但是主机却无法访问,但是同时主机和虚拟机之间可以ping的通.   网上查阅资料后   第一种解决方法是 ...

  3. spring boot 下 开启 gzip

    [参考文章]:Spring boot开启Gzip压缩 [参考文章]:Accept-Encoding Spring 版本 :5.1.2-RELEASE 1. application.yml 配置 ser ...

  4. 压力测试 Jmeter的简单测试及常用查看结果参数介绍

    (1)保存方案 (2)创建线程组 线程组用来模拟用户的并发访问 线程组主要包含三个参数:线程数.准备时长(Ramp-Up Period(in seconds)).循环次数. 线程数:虚拟用户数.一个虚 ...

  5. 理解MyCat分库分表

    1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18.

  6. 有关react-native的最常用的库(文件、样式、UI组件)

    一.对文件的处理 1.react-native-fs 2.react-native-file-selector 3.MaterialFilePicker 二.React-Native 样式指南 1.r ...

  7. 使用Jsp +Js + Jquery + EasyUI + Servlet + Lucene,完成分页

    步一:创建ArticleDao.java类 public class ArticleDao { public Integer getAllObjectNum(String keywords) thro ...

  8. 利用Smarty实现文本隔行变色

    行变色 php页面 <?phpinclude "libs/Smarty.class.php";$smarty = new Smarty(); $link = mysql_co ...

  9. for(foo('a') ; foo('b') && (i<2);foo('c'))的执行结果

    static boolean foo(char c) { System.out.println(c); return true; } public static void main(String[] ...

  10. Ubuntu端口常用命令

    # 查看哪些进程打开了指定端口port(对于守护进程必须以root用户执行才能查看到) lsof -i:port # 查看哪些进程打开了指定端口port,最后一列是进程ID(此方法对于守护进程作用不大 ...