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. 浅析Dagger2依赖注入实现过程

    Dragger2是Android应用开发中一个非常优秀的依赖注入框架.本文主要通过结合Google给出的MVP开发案例todo-mvp-dagger(GitHub连接地址:https://github ...

  2. Problem G: 圆周率

    Problem G: 圆周率 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 155  Solved: 99[Submit][Status][Web Bo ...

  3. 欠采样(undersampling)和过采样(oversampling)会对模型带来怎样的影响

    项目中出现了二分类数据不平横问题,研究总结下对于类别不平横问题的处理经验: 为什么类别不平横会影响模型的输出? 许多模型的输出类别是基于阈值的,例如逻辑回归中小于0.5的为反例,大于则为正例.在数据不 ...

  4. python_72_json序列化2

    #序列化(json是最正规的) import json info={ 'name':'Xue Jingjie', 'age':22 } f=open('第72.text','w') print(jso ...

  5. windows下安装python的包管理工具pip,scikit-learn

    打开https://pip.pypa.io/en/latest/installing.html#python-os-support 下载pip-get.py 进入python,执行pip-get.py ...

  6. Java 获取Web项目相对webapp地址

    例如, import java.io.File; import java.io.FileInputStream; import javax.servlet.http.HttpServletReques ...

  7. js控制时间显示格式

    Date.prototype.Format = function (fmt) { //author: meizz     var o = {        "M+": this.g ...

  8. rsyn远程自动同步

    rsync是远程自动同步工具,同时也能实现本地文件的复制,能够实现cp ,scp的功能,但是在远程同步上rsync要scp高效,因为scp能实现增量传输,每次都得全量传输,如果传输大文件时会很消耗网络 ...

  9. nginx负载均衡核心组件介绍

    一.nginx upstream 模块介绍 1.upstream模块介绍 nginx的负载均衡功能依赖于ngx_http_upstream_module模块,所支持的代理方式包括 proxy_pass ...

  10. Python 正则表达式 匹配任意字符

    .(句点)匹配除了换行之外的所有一个字符, .*(点-星)匹配除了换行外的所有字符 >>> >>> r=re.compile(r'.*')>>> ...