import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper; @Configuration
public class RedisConfig { @SuppressWarnings({ "rawtypes", "unchecked" })
@Bean
public RedisTemplate<String, String> redisTemplate(final RedisConnectionFactory factory) {
final StringRedisTemplate template = new StringRedisTemplate(factory);
final Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
final ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
template.setValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
} }
/**
* Created by pc on 2017/1/25.
*/
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component; @Component
public class RedisUtil {
private static final Logger log = LoggerFactory.getLogger(RedisUtil.class);
@Autowired
private RedisTemplate redisTemplate; public RedisUtil() {
} public void remove(String... keys) {
String[] var2 = keys;
int var3 = keys.length; for(int var4 = 0; var4 < var3; ++var4) {
String key = var2[var4];
this.remove(key);
} } public void removePattern(String pattern) {
Set keys = this.redisTemplate.keys(pattern);
if(keys.size() > 0) {
this.redisTemplate.delete(keys);
} } public void remove(String key) {
if(this.exists(key)) {
this.redisTemplate.delete(key);
} } public boolean exists(String key) {
return this.redisTemplate.hasKey(key).booleanValue();
} public Object get(String key) {
Object result = null;
ValueOperations operations = this.redisTemplate.opsForValue();
result = operations.get(key);
return result;
} public boolean set(String key, Object value) {
boolean result = false; try {
ValueOperations e = this.redisTemplate.opsForValue();
e.set(key, value);
result = true;
} catch (Exception var5) {
log.error("RedisUtil set exception", var5);
} return result;
} public boolean set(String key, Object value, Long expireTime) {
boolean result = false; try {
ValueOperations e = this.redisTemplate.opsForValue();
e.set(key, value);
this.redisTemplate.expire(key, expireTime.longValue(), TimeUnit.SECONDS);
result = true;
} catch (Exception var6) {
log.error("RedisUtil set exception", var6);
} return result;
} public boolean setIfAbsent(String key, Object value) {
boolean result = false; try {
ValueOperations e = this.redisTemplate.opsForValue();
result = e.setIfAbsent(key, value).booleanValue();
} catch (Exception var5) {
log.error("RedisUtil setIfAbsent exception", var5);
} return result;
} public boolean setIfAbsent(String key, Object value, Long expireTime) {
boolean result = false; try {
ValueOperations e = this.redisTemplate.opsForValue();
result = e.setIfAbsent(key, value).booleanValue();
if(result) {
this.redisTemplate.expire(key, expireTime.longValue(), TimeUnit.SECONDS);
}
} catch (Exception var6) {
log.error("RedisUtil setIfAbsent exception", var6);
} return result;
} public boolean tryLock(String key, Long cacheSeconds) {
boolean isLock = false; try {
isLock = this.setIfAbsent(key, "", cacheSeconds);
} catch (Exception var5) {
log.error("RedisUtil tryLock exception", var5);
} return isLock;
}
}
# REDIS (RedisProperties)
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=192.168.128.131
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=0

spring boot redis代码与配置的更多相关文章

  1. Spring Boot Redis 集成配置(转)

    Spring Boot Redis 集成配置 .embody{ padding:10px 10px 10px; margin:0 -20px; border-bottom:solid 1px #ede ...

  2. 4、Spring Boot 2.x 自动配置原理

    1.4 Spring Boot 自动配置原理 简介 spring boot自动配置功能可以根据不同情况来决定spring配置应该用哪个,不应该用哪个,举个例子: Spring的JdbcTemplate ...

  3. spring boot redis 缓存(cache)集成

    Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...

  4. Spring boot 基于注解方式配置datasource

    Spring boot 基于注解方式配置datasource 编辑 ​ Xml配置 我们先来回顾下,使用xml配置数据源. 步骤: 先加载数据库相关配置文件; 配置数据源; 配置sqlSessionF ...

  5. 学记:为spring boot写一个自动配置

    spring boot遵循"约定优于配置"的原则,使用annotation对一些常规的配置项做默认配置,减少或不使用xml配置,让你的项目快速运行起来.spring boot的神奇 ...

  6. Spring Boot 探索系列 - 自动化配置篇

    26. Logging Prev  Part IV. Spring Boot features  Next 26. Logging Spring Boot uses Commons Logging f ...

  7. Spring Boot实践——用外部配置填充Bean属性的几种方法

    引用:https://blog.csdn.net/qq_17586821/article/details/79802320 spring boot允许我们把配置信息外部化.由此,我们就可以在不同的环境 ...

  8. spring boot(5)-properties参数配置

     application.properties application.properties是spring boot默认的配置文件,spring boot默认会在以下两个路径搜索并加载这个文件 s ...

  9. Spring集成Redis集群(含spring集成redis代码)

    代码地址如下:http://www.demodashi.com/demo/11458.html 一.准备工作 安装 Redis 集群 安装参考: http://blog.csdn.net/zk6738 ...

随机推荐

  1. js 生成随机数解决缓存的问题

    对于缓存有一个解决方法是在链接后添加随机数 例如登陆后跳转到链接/home,但是有缓存上次用户的登陆名,于是在/home后面加上一个随机数 var href = '/home?'+Math.rando ...

  2. 进入Windows之前发出警告

    实现效果: 知识运用: 通过注册表中HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\子键下的LegalNoticeCaption ...

  3. python_62_装饰器5

    import time def timer(func): #timer(test1) func=test1 def deco(*args,**kwargs): start_time=time.time ...

  4. C#的接口基础教程之七 覆盖虚接口

    有时候我们需要表达一种抽象的东西,它是一些东西的概括,但我们又不能真正的看到它成为一个实体在我们眼前出现,为此面向对象的编程语言便有了抽象类的概念.C#作为一个面向对象的语言,必然也会引入抽象类这一概 ...

  5. 安装mysql提示This application requires .NET framework 4.0.

    问题描述:安装MySQL社区版时遇到This application requires .NET framework 4.0. 解决方法:在http://search.microsoft.com/zh ...

  6. jquery的ajax请求

    加载页面内容,如果不加选择器,会加载整个页面内容 加选择器会获取选择器内容 例如: <script> //可以获取json格式的文件 $.ajax({ type:"get&quo ...

  7. GNU C中__attribute__

    __attribute__基本介绍: 1. __attribute__ 可以设置函数属性.变量属性和类型属性. 2. __attribute__ 语法格式为:__attribute__ ((attri ...

  8. java中equals和==的区别详解

    java中的数据类型,可分为两类: 1.基本数据类型. byte,short,char,int,long,float,double,boolean这八大原始数据类型他们之间的比较,使用“==”,比较的 ...

  9. MultipartFile 动态决定是否上传文件,解决不上传文件报错

    controller 接收参数   用 HttpServletRequest 代替 @RequestParam()  接收参数 picFile 前台 传文件的参数名字  ,   这样 前段 传 nul ...

  10. TP5 发送邮件代码

    发送邮箱邮件方法 /** * 系统邮件发送函数 * @param string $tomail 接收邮件者邮箱 * @param string $name 接收邮件者名称 * @param strin ...