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. IOS 导航控制器基本使用(UINavigationController代码创建方式)

    ● UINavigationController的使用步骤 ➢ 初始化UINavigationController ➢ 设置UIWindow的rootViewController为UINavigati ...

  2. python_37_文件修改

    f=open('yesterday','r',encoding='utf-8') f_new=open('yesterday_update','w',encoding='utf-8') for lin ...

  3. Luogu [P1958] 上学路线_NOI导刊2009普及(6)

    上学路线_NOI导刊2009普及(6) 题目详见:上学路线_NOI导刊2009普及(6) 这是一道基础的DFS(深搜)题,堪称模板,是新手练习搜索与回溯的好题选. 大致思路:从(1,1)开始搜索,每次 ...

  4. LigerUi中表(Grid)控件的相关属性笔记

    http://blog.csdn.net/dxnn520/article/details/8216560 // ========================================= [每 ...

  5. java Html&JavaScript面试题:判断第二个日期比第一个日期大

    如何用脚本判断用户输入的的字符串是下面的时间格式2004-11-21 必须要保证用户的输入是此格式,并且是时间,比如说月份不大于12等等,另外我需要用户输入两个,并且后一个要比前一个晚,只允许用JAV ...

  6. linux正则表达式基础部分

    1.什么是正则表达式? 简单的说,正则表达式就是为处理大量的字符串而定义的一套规则和方法, 例如:假设“@”代表boy,“!”代表girl.echo“@!” === “boygirl” 通过定义的这些 ...

  7. 数据结构-二叉树(Binary Tree)

    #include <stdio.h> #include <string.h> #include <stdlib.h> #define LIST_INIT_SIZE ...

  8. pip3 的安装 同时安装lxml和pygame

    ubuntu18.04中 首先查看自己电脑的python版本,一般都会有2, 和3 python -V python3 -V 查看pip版本 pip -V pip3 -V 现在我们就可以开始安装我们的 ...

  9. BFS:HDU3085-Nightmare Ⅱ(双向BFS)

    Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 32768/32768 K (Java/Others) Tot ...

  10. [Poj3281]Dining(最大流)

    Description 有n头牛,f种食物,d种饮料,每头牛有nf种喜欢的食物,nd种喜欢的饮料,每种食物如果给一头牛吃了,那么另一个牛就不能吃这种食物了,饮料也同理,问最多有多少头牛可以吃到它喜欢的 ...