1、yml文件配置

#redis哨兵模式配置
redis:
namespace: xxx:xxx:redis
connection:
cacheRedis:
database: 0
timeout: 1000
password: xxx
sentinel:
nodes: ip1:26381,ip2:26382,ip3:26383
master: master
pool:
minIdle: 51
maxActive: 51
2、radis配置类

package com.common.config;

import com..DynamicRedisProvider;
import com..JacksonSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

/**
* @author liu
* @date 2019-01-05
*/
@Configuration
public class RedisConfig {

/**
* @param dynamicRedisProvider
* @return RedisTemplate
* @Title: redisTemplate
* @Description: 读取并配置redis
*/
@SuppressWarnings("rawtypes")
@Bean
public RedisTemplate redisTemplate(DynamicRedisProvider dynamicRedisProvider) {
StringRedisTemplate template = new StringRedisTemplate(dynamicRedisProvider.loadRedis().get("cacheRedis"));
JacksonSerializer.setJacksonSerializer(template);
return template;
}
}
3、radis工具类

package com.common.utils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import com.longfor.gaia.gfs.data.redis.RedisKey;

import lombok.extern.slf4j.Slf4j;

/**
* @Description: Redis工具类
* @author: liuy
* @date: 2019-01-05
*/
@Component
@Slf4j
@SuppressWarnings({ "unchecked", "rawtypes" })
public class RedisUtil {

private static RedisTemplate redisTemplate;

@Autowired
private void setRedisTemplate(RedisTemplate redisTemplate) {
RedisUtil.redisTemplate = redisTemplate;
}

//=============================common============================
/**
* @Title: buildKey
* @Description: 获取redis的key,最终的key由命名空间+key组成
* @param key key
* @return String
*/
public static String buildKey(Object... key) {
return RedisKey.join(key);
}

/**
* 指定缓存失效时间
* @param key 键
* @param time 时间(秒)
* @return
*/
public static Boolean expire(String key,long time) {
try {
key = buildKey(key);
if (time>0) {
redisTemplate.expire(key, time, TimeUnit.SECONDS);
}
return true;
} catch (Exception e) {
log.error(e.getMessage(), e);
return false;
}
}

/**
* 根据key 获取过期时间
* @param key 键 不能为null
* @return 时间(秒) 返回0代表为永久有效
*/
public static Long getExpire(String key) {
key = buildKey(key);
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
}

/**
* 判断key是否存在
* @param key 键
* @return true 存在 false不存在
*/
public static Boolean hasKey(String key) {
try {
key = buildKey(key);
return redisTemplate.hasKey(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
return false;
}
}

/**
* 删除缓存
* @param key 可以传一个值
*/
public static void del(String key) {
key = buildKey(key);
redisTemplate.delete(key);
}

//============================String=============================
/**
* 普通缓存获取
* @param key 键
* @return 值
*/
public static Object get(String key) {
key = buildKey(key);
return key == null ? null : redisTemplate.opsForValue().get(key);
}

/**
* 普通缓存放入
* @param key 键
* @param value 值
* @return true成功 false失败
*/
public static Boolean set(String key, Object value) {
try {
key = buildKey(key);
redisTemplate.opsForValue().set(key, value);
return true;
} catch (Exception e) {
log.error(e.getMessage(), e);
return false;
}

}

/**
* 普通缓存放入并设置时间
* @param key 键
* @param value 值
* @param time 时间(秒) time要大于0 如果time小于等于0 将设置无限期
* @return true成功 false 失败
*/
public static Boolean set(String key, Object value, long time) {
try {
key = buildKey(key);
if (time > 0) {
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
} else {
set(key, value);
}
return true;
} catch (Exception e) {
log.error(e.getMessage(), e);
return false;
}
}
/**
* 获取并且重新设置值
* @param key 键
* @param value 值
* @return true成功 false 失败
*/
public static Boolean getAndSet(String key, Object value){
try {
key = buildKey(key);
redisTemplate.opsForValue().getAndSet(key, value);

return true;
} catch (Exception e) {
log.error(e.getMessage(), e);
return false;
}
}

/**
* 递增
* @param key 键
* @param delta 要增加几(大于0)
* @return
*/
public static Long incr(String key, long delta) {
key = buildKey(key);
if (delta < 0) {
throw new IllegalArgumentException("递增因子必须大于0");
}
return redisTemplate.opsForValue().increment(key, delta);
}

/**
* 递减
* @param key 键
* @param delta 要减少几(小于0)
* @return
*/
public static Long decr(String key, long delta) {
key = buildKey(key);
if(delta < 0){
throw new IllegalArgumentException("递减因子必须大于0");
}
return redisTemplate.opsForValue().increment(key, -delta);
}

//================================Map=================================
/**
* HashGet
* @param key 键 不能为null
* @param item 项 不能为null
* @return 值
*/
public static Object hget(String key,String item) {
key = buildKey(key);
return redisTemplate.opsForHash().get(key, item);
}

/**
* 获取hashKey对应的所有键值
* @param key 键
* @return 对应的多个键值
*/
public static Map<Object,Object> hmget(String key) {
key = buildKey(key);
return redisTemplate.opsForHash().entries(key);
}

/**
* HashSet
* @param key 键
* @param map 对应多个键值
* @return true 成功 false 失败
*/
public static Boolean hmset(String key, Map<String, Object> map) {
try {
key = buildKey(www.yingka178.com key);
redisTemplate.opsForHash().putAll(key, map);
return true;
} catch (Exception e) {
log.error(e.getMessage(www.tiaotiaoylzc.com), e);
return false;
}
}

/**
* HashSet 并设置时间
* @param key 键
* @param map 对应多个键值
* @param time 时间(秒)
* @return true成功 false失败
*/
public static Boolean hmset(String key, Map<String,Object> map, long time){
try {
key = buildKey(key);
redisTemplate.opsForHash().putAll(key, map);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
log.error(e.getMessage(), e);
return false;
}
}

/**
* 向一张hash表中放入数据,如果不存在将创建
* @param key 键
* @param item 项
* @param value 值
* @return true 成功 false失败
*/
public static Boolean hset(String key, String item, Object value) {
try {
key = buildKey(key);
redisTemplate.opsForHash().put(key, item, value);
return true;
} catch (Exception e) {
log.error(e.getMessage(), e);
return false;
}
}

/**
* 向一张hash表中放入数据,如果不存在将创建
* @param key 键
* @param item 项
* @param value 值
* @param time 时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间
* @return true 成功 false失败
*/
public static Boolean hset(String key, String item, Object value, long time) {
try {
key = buildKey(key);
redisTemplate.opsForHash(www.dfgjpt.com).put(key, item, value);
if(time > 0){
expire(key, time);
}
return true;
} catch (Exception e) {
log.error(e.getMessage(), e);
return false;
}
}

/**
* 删除hash表中的值
* @param key 键 不能为null
* @param item 项 可以使多个 不能为null
*/
public static void hdel(String key, Object... item) {
key = buildKey(key);
redisTemplate.opsForHash().delete(key, item);
}

/**
* 判断hash表中是否有该项的值
* @param key 键 不能为null
* @param item 项 不能为null
* @return true 存在 false不存在
*/
public static Boolean hHasKey(String key, String item) {
key = buildKey(key);
return redisTemplate.opsForHash(www.yongshiyule178.com).hasKey(key, item);
}

/**
* hash递增 如果不存在,就会创建一个 并把新增后的值返回
* @param key 键
* @param item 项
* @param by 要增加几(大于0)
* @return
*/
public static Double hincr(String key, String item, double by) {
key = buildKey(key);
return redisTemplate.opsForHash().increment(key, item, by);
}

/**
* hash递减
* @param key 键
* @param item 项
* @param by 要减少记(小于0)
* @return
*/
public static Double hdecr(String key, String item,double by) {
key = buildKey(key);
return redisTemplate.opsForHash().increment(key, item,-by);
}

//============================set=============================
/**
* 根据key获取Set中的所有值
* @param key 键
* @return
*/
public static Set<Object> sGet(String key) {
try {
key = buildKey(key);
return redisTemplate.opsForSet(www.yongshi123.cn).members(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
return new HashSet();
}
}

/**
* 根据value从一个set中查询,是否存在
* @param key 键
* @param value 值
* @return true 存在 false不存在
*/
public static Boolean sHasKey(String key, Object value) {
try {
key = buildKey(key);
return redisTemplate.opsForSet().isMember(key, value);
} catch (Exception e) {
log.error(e.getMessage(), e);
return false;
}
}

/**
* 将数据放入set缓存
* @param key 键
* @param values 值 可以是多个
* @return 成功个数
*/
public static Long sSet(String key, Object...values) {
try {
key = buildKey(key);
return redisTemplate.opsForSet().add(key, values);
} catch (Exception e) {
log.error(e.getMessage(www.fengshen157.com), e);
return 0L;
}
}

/**
* 将set数据放入缓存
* @param key 键
* @param time 时间(秒)
* @param values 值 可以是多个
* @return 成功个数
*/
public static Long sSetAndTime(String key, long time, Object...values) {
try {
key = buildKey(key);
Long count = redisTemplate.opsForSet().add(key, values);
if (time > 0) {
expire(key, time);
}
return count;
} catch (Exception e) {
log.error(e.getMessage(), e);
return 0L;
}
}

/**
* 获取set缓存的长度
* @param key 键
* @return
*/
public static Long sGetSetSize(String key) {
try {
key = buildKey(key);
return redisTemplate.opsForSet().size(key);
} catch (Exception e)www.maituyul1.cn {
log.error(e.getMessage(), e);
return 0L;
}
}

/**
* 移除值为value的
* @param key 键
* @param values 值 可以是多个
* @return 移除的个数
*/
public static Long setRemove(String key, Object ...values) {
try {
key = buildKey(key);
return redisTemplate.opsForSet().remove(key, values);
} catch (Exception e) {
log.error(e.getMessage( www.huarenyl.cn), e);
return 0L;
}
}
//===============================list=================================

/**
* 获取list缓存的内容
* @param key 键
* @param start 开始
* @param end 结束 0 到 -1代表所有值
* @return
*/
public static List<Object> lGet(String key, long start, long end) {
try {
key = buildKey(key);
return redisTemplate.opsForList().range(key, start, end);
} catch (Exception e) {
log.error(e.getMessage(), e);
return new ArrayList();
}
}

/**
* 获取list缓存的长度
* @param key 键
* @return
*/
public static Long lGetListSize(String key) {
try {
key = buildKey(key);
return redisTemplate.opsForList().size(key);
} catch (Exception e)www.xinghenyule.com {
log.error(e.getMessage(), e);
return 0L;
}
}

/**
* 通过索引 获取list中的值
* @param key 键
* @param index 索引 index>=0时, 0 表头,1 第二个元素,依次类推;index<0时,-1,表尾,-2倒数第二个元素,依次类推
* @return
*/
public static Object lGetIndex(String key,long index) {
try {
key = buildKey(key);
return redisTemplate.opsForList().index(key, index);
} catch (Exception e) {
log.error(e.getMessage(), e);
return null;
}
}

/**
* 将list放入缓存
* @param key 键
* @param value 值
* @return
*/
public static Boolean lSet(String key, Object value) {
try {
key = buildKey(www.yihuanyule.cn key);
redisTemplate.opsForList().rightPush(key, value);
return true;
} catch (Exception e) {
log.error(e.getMessage(), e);
return false;
}
}

/**
* 将list放入缓存
* @param key 键
* @param value 值
* @param time 时间(秒)
* @return
*/
public static Boolean lSet(String key, Object value, long time) {
try {
key = buildKey(key);
redisTemplate.opsForList().rightPush(key, value);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
log.error(e.getMessage(), e);
return false;
}
}

/**
* 将list放入缓存
* @param key 键
* @param value 值
* @return
*/
public static Boolean lSet(String key, List<Object> value) {
try {
key = buildKey(www.jiuzhoyulpt.cn key);
redisTemplate.opsForList().rightPushAll(key, value);
return true;
} catch (Exception e) {
log.error(e.getMessage(), e);
return false;
}
}

/**
* 将list放入缓存
* @param key 键
* @param value 值
* @param time 时间(秒)
* @return
*/
public static Boolean lSet(String key, List<Object> value, long time) {
try {
key = buildKey(key);
redisTemplate.opsForList().rightPushAll(key, value);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
log.error(e.getMessage(), e);
return false;
}
}

/**
* 根据索引修改list中的某条数据
* @param key 键
* @param index 索引
* @param value 值
* @return
*/
public static Boolean lUpdateIndex(String key, long index, Object value) {
try {
key = buildKey(key);
redisTemplate.opsForList().set(key, index, value);
return true;
} catch (Exception e) {
log.error(e.getMessage(), e);
return false;
}
}

/**
* 移除N个值为value
* @param key 键
* @param count 移除多少个
* @param value 值
* @return 移除的个数
*/
public static Long lRemove(String key, long count, Object value) {
try {
key = buildKey(key);
return redisTemplate.opsForList().remove(key, count, value);
} catch (Exception e) {
log.error(e.getMessage(), e);
return 0L;

转载于:https://www.cnblogs.com/qwangxiao/p/10481563.html

springboot中radis配置和使用【进阶二】相关推荐

  1. SpringBoot中如何配置使用过滤器(Filter)呢?

    转自: SpringBoot中如何配置使用过滤器(Filter)呢? 下文笔者讲述springboot中配置过滤器的方法分享,如下所示 实现思路:1.定义filter2.将filter注册进sprin ...

  2. springboot中@Configuration配置类加载流程

    springboot中@Configuration配置类加载流程 代码位置 源码解读 每一步的分析 代码位置 ConfigurationClassParser#doProcessConfigurati ...

  3. SpringBoot中Profile配置和加载配置文件

    文章目录 一.多Profile的资源文件 二.profile激活 1.配置文件方式激活profile 2.命令行方式激活profile 三.@profile使用 写在前面: 我是「境里婆娑」.我还是从 ...

  4. SpringBoot中yaml配置

    yaml是一种可读性高,用来表示数据序列化的格式.在SpringBoot中也可以使用properties,但是推荐使用yaml. 在SpringBoot中使用一种全局的配置文件,其名称是固定的为app ...

  5. 数据源(DataSource)是什么以及SpringBoot中数据源配置

    数据源 数据源,简单理解为数据源头,提供了应用程序所需要数据的位置.数据源保证了应用程序与目标数据之间交互的规范和协议,它可以是数据库,文件系统等等.其中数据源定义了位置信息,用户验证信息和交互时所需 ...

  6. SpringBoot 中实现配置和使用定时器_张童瑶的博客

    简单两步,实现在spring boot中配置和使用定时器: 1.在入口类中加入@EnableScheduling注解(即springboot启动类添加注解@EnableScheduling): @Sp ...

  7. Mybatis在Spring-boot中自动配置的底层源码分析

    前言:在Spring-boot结合Mybatis,我们只需要配置一些数据库连接的基本信息,写好Mapper和Dto就可以跑起来了.但是它是如何帮我们完成自动配置,并且执行我们的sql语句的呢?我们一起 ...

  8. SpringBoot中Tomcat配置(学习SpringBoot实战)

    1.Tomcat配置 Spring Boot默认内嵌的Tomcat为Servlet容器,所以本节只讲对Tomcat配置,其实本节的配置对Tomcat.Jetty和Undertow都是通用的. 1.1 ...

  9. SpringBoot中mybatis配置多数据源

    首先需要创建多个数据库 简单的user表 CREATE TABLE `user` (`id` int NOT NULL AUTO_INCREMENT,`name` varchar(255) DEFAU ...

最新文章

  1. 新手必看,17个常见的Python运行时错误
  2. 前端各阶段资源,学得完算我输
  3. deep_sort_face
  4. python使用符号 表示单行注释-Python编程规范之注释
  5. Leetcode03
  6. 期待已久的2012年度最佳 jQuery 插件揭晓
  7. C++类特殊成员函数
  8. 互联网人的恶梦是加班?不,是饥荒!
  9. c ++明明的随机数_从列表C ++程序中随机建议电影
  10. spring bean的创建,生命周期
  11. 用程序同步mysql数据库表_初次用Java写了个数据库表同步工具
  12. linux cordova安装教程,mac怎么安装cordova?
  13. MFC开发IM-MFC任意位置实现窗口拖动
  14. php 字库,矢量字库的剪裁_php
  15. html中居中的三种方式
  16. 从认知动机理论看设计:如何读懂用户?
  17. Vue实现附件上传功能
  18. Oracle中多表查询再按时间倒序
  19. Python批量处理Excel数据后,导入SQL Server
  20. 《CCNP安全Secure 642-637认证考试指南》——2.3节入侵者动机

热门文章

  1. Java 按位与 Java代码_(Java)按位与运算符-是否用于减少前一个位间隔?
  2. mysql, 一对多查询, 统计一表数量
  3. 制作精美的网站首页模板应该如何操作?
  4. 网站发布外链如何防止后期被删除?
  5. mysql 事件_区块链研究实验室 | 使用MySQL存储以太坊事件
  6. xcopy 跳过已经存在的_《天官赐福》舍不得跳过的片头片尾,无别,不散唱出花城心声...
  7. android 数字时钟代码大全,Android自定义view实现数字时钟
  8. python2升级_把Python2.6升级到Python2.7(适用于把Python2升级到Python3)
  9. 在Android中使用AspectJ进行切面编程的简易步骤
  10. 大规模异常滥用检测:基于局部敏感哈希算法——来自Uber Engineering的实践