spring boot redis代码与配置
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代码与配置的更多相关文章
- Spring Boot Redis 集成配置(转)
Spring Boot Redis 集成配置 .embody{ padding:10px 10px 10px; margin:0 -20px; border-bottom:solid 1px #ede ...
- 4、Spring Boot 2.x 自动配置原理
1.4 Spring Boot 自动配置原理 简介 spring boot自动配置功能可以根据不同情况来决定spring配置应该用哪个,不应该用哪个,举个例子: Spring的JdbcTemplate ...
- spring boot redis 缓存(cache)集成
Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...
- Spring boot 基于注解方式配置datasource
Spring boot 基于注解方式配置datasource 编辑 Xml配置 我们先来回顾下,使用xml配置数据源. 步骤: 先加载数据库相关配置文件; 配置数据源; 配置sqlSessionF ...
- 学记:为spring boot写一个自动配置
spring boot遵循"约定优于配置"的原则,使用annotation对一些常规的配置项做默认配置,减少或不使用xml配置,让你的项目快速运行起来.spring boot的神奇 ...
- Spring Boot 探索系列 - 自动化配置篇
26. Logging Prev Part IV. Spring Boot features Next 26. Logging Spring Boot uses Commons Logging f ...
- Spring Boot实践——用外部配置填充Bean属性的几种方法
引用:https://blog.csdn.net/qq_17586821/article/details/79802320 spring boot允许我们把配置信息外部化.由此,我们就可以在不同的环境 ...
- spring boot(5)-properties参数配置
application.properties application.properties是spring boot默认的配置文件,spring boot默认会在以下两个路径搜索并加载这个文件 s ...
- Spring集成Redis集群(含spring集成redis代码)
代码地址如下:http://www.demodashi.com/demo/11458.html 一.准备工作 安装 Redis 集群 安装参考: http://blog.csdn.net/zk6738 ...
随机推荐
- 【洛谷4011】孤岛营救问题(状压SPFA)
点此看题面 大致题意: 有一个\(N*M\)的四联通迷宫,相邻两个可能互通,可能有一扇门,也可能有一堵墙.对于第\(i\)类的门,你需要有第\(i\)类的钥匙才可以通过.问你从\((1,1)\)到达\ ...
- python_50_函数与函数式编程
import time def logger(): """追加写""" time_format='%Y-%m-%d %X'#年-月-日 小时 ...
- Bootstrap历练实例:轮播(carousel)
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- 把网上图片下载到本地的java工具类
package com.swift; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; ...
- V8引擎——详解
前言 JavaScript绝对是最火的编程语言之一,一直具有很大的用户群,随着在服务端的使用(NodeJs),更是爆发了极强的生命力.编程语言分为编译型语言和解释型语言两类,编译型语言在执行之前要先进 ...
- JQuery模拟点击页面上的所有a标签,触发onclick事件
注意: 这种方法需要给所有的a标签加上id属性 页面加载完成模拟点击所有的a标签: <script> $(function () { // 模拟点击页面上的所有a标签,触发onclick事 ...
- linux下通过phpize为php在不重新编译php情况下安装模块memcache
通过phpize为php在不重新编译php情况下安装模块memcache 1. 下载 wget http://pecl.php.net/get/memcache-2.2.4.tgz 解压 ...
- MySql学习笔记04
复习 day01 数据库相关: create database db4 character set utf8; show databases; show create database db4; dr ...
- 【JAVA】mac配置java环境变量
如果用bash,修改~/.bash_profile 或 ~/.profile: 如果用zsh,修改-/.zshrc 修改这些文件之后,重修打开terminal,配置不会丢 首先确保已经安装了jdk: ...
- 用纯CSS实现加载中动画效果
HTML <div class="pswp__preloader__icn"> <div class="pswp__preloader__cut&quo ...