SpringBoot 集成Jedis操作set
题外话:
Redis是个有趣的东西,相信搞java的或多或少都会用到,面试时也总离不开问Redis,之前觉得redis只是用做缓存,飞快!也因为最初在封装底层的时候,使用Redisson,所以大部分都只用到了String这种类型,不管相应的value是List还是Map,最多也就以json格式存储,慢慢的用多了,才发现在业务中错过了许多优化的地方;
其中Set类型是一个不错的选择,举个例子,我们实际业务中存在粉丝订阅关系,同时,因为采用Spring Cloud分布式架构,加上各个微服务之间做了分库,导致许多地方在查询时需要feign调用订阅关系去做其他逻辑,用Set存储可以解决粉丝关注,粉丝数统计,我关注的人也关注了谁等等问题;
1、pom.xml
<!-- jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.2</version>
</dependency>
2、注入bean
@Bean
public JedisPool redisPoolFactory(
@Value("${spring.redis.host}") String redisHost,
@Value("${spring.redis.port}") int redisPort,
@Value("${spring.redis.password}") String redisPassword,
@Value("${spring.redis.database}") int database ,
@Value("${spring.redis.jedis.pool.max-wait}") int maxWaitMillis,
@Value("${spring.redis.jedis.pool.max-idle}") int maxIdle,
@Value("${spring.redis.jedis.pool.max-active}") int maxActive
){
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(maxIdle);
jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
jedisPoolConfig.setMaxTotal(maxActive);
jedisPoolConfig.setMinIdle(0);
jedisPoolConfig.setMaxIdle(maxIdle);
JedisPool jedisPool = new JedisPool(jedisPoolConfig,redisHost,redisPort,0,redisPassword);
return jedisPool;
}
@Bean
public JedisUtils jedisUtils (JedisPool jedisPool ){
return new JedisUtils(jedisPool);
}
3、JedisUtils操作set
package com.cookie.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import java.util.Set;
/**
* author : cxq
* Date : 2019/7/11
*/
//@Component
public class JedisUtils {
private final static Logger logger = LoggerFactory.getLogger(JedisUtils.class);
// @Autowired
private JedisPool jedisPool ;
public JedisUtils(JedisPool jedisPool) {
this.jedisPool = jedisPool;
}
/**
* 查询set集合数据
* @param key
* @return
*/
public Set<String> getSet(String key ){
Jedis jedis = null ;
Set<String> set = null ;
try {
jedis = jedisPool.getResource();
set = jedis.smembers(key);
}catch (Exception e ){
logger.error(" get set error : "+e.getMessage());
}
return set ;
}
/**
* 往set中添加数据
* @param key
* @param values
* @return
*/
public Long addSet(String key , String... values ){
Jedis jedis = null ;
try {
jedis = jedisPool.getResource();
return jedis.sadd(key,values);
}catch (Exception e ){
logger.error(" get set error : "+e.getMessage());
}
return 0L ;
}
/**
* 删除数据
* @param key
* @param values
* @return
*/
public Long delSet(String key , String... values ){
Jedis jedis = null ;
try {
jedis = jedisPool.getResource();
return jedis.srem(key,values);
}catch (Exception e ){
logger.error(" del set error : "+e.getMessage());
}
return 0L ;
}
/**
* 求第一个key与其他key不同的部分
* @param keys
* @return
*/
public Set<String> getDiffSet(String... keys){
Jedis jedis = null ;
try {
jedis = jedisPool.getResource();
return jedis.sdiff(keys);
}catch (Exception e ){
logger.error(" get diff set error : "+e.getMessage());
}
return null ;
}
/**
* 求key的合集
* @param keys
* @return
*/
public Set<String> getUnionSet(String... keys){
Jedis jedis = null ;
try {
jedis = jedisPool.getResource();
return jedis.sunion(keys);
}catch (Exception e ){
logger.error(" get union set error : "+e.getMessage());
}
return null ;
}
/**
* 求key的交集
* @param keys
* @return
*/
public Set<String> getInterSet(String... keys){
Jedis jedis = null ;
try {
jedis = jedisPool.getResource();
return jedis.sinter(keys);
}catch (Exception e ){
logger.error(" get inter set error : "+e.getMessage());
}
return null ;
}
/**
* 获取key的长度
* @param key
* @return
*/
public Long getSetCount(String key ){
Jedis jedis = null ;
try {
jedis = jedisPool.getResource();
return jedis.scard(key);
}catch (Exception e ){
logger.error(" get set count error : "+e.getMessage());
}
return 0L ;
}
/**
* 判断值是否存在
* @param key
* @param value
* @return
*/
public boolean checkValueIsInSet(String key , String value ){
Jedis jedis = null ;
try {
jedis = jedisPool.getResource();
return jedis.sismember(key,value);
}catch (Exception e ){
logger.error(" check member is in set error : "+e.getMessage());
}
return false ;
}
}
SpringBoot 集成Jedis操作set的更多相关文章
- Springboot集成Jedis + Redisson(已自测)
原文:https://blog.csdn.net/c_zyer/article/details/79415728 本文主要跟大家分享在Springboot中集成Jedis和Redisson的方法.为什 ...
- Spring-Boot 使用 Jedis 操作 Redis
背景: 1.Redis 之前学了个皮毛 还忘的差不多了,感觉公司项目中的Redis用的真的牛逼,so 需要深造. 2.有个同事在搞Jedis,勾起了我对知识的向往,不会用,但是很渴望. 过程: 1.改 ...
- Redis系统学习之SpringBoot集成Redis操作API(集成SpringDataRedis及其分析)
SpringDataRedis调用Redis底层解读 在SpringBoot2.X之前还是直接使用的官方推荐的Jedis连接的Redis 在2.X之后换为了lettuce Jedis:采用直接连接,多 ...
- springboot集成jpa操作mybatis数据库
数据库如下 CREATE TABLE `jpa`.`Untitled` ( `cust_id` bigint() NOT NULL AUTO_INCREMENT, `cust_address` var ...
- springboot集成redis操作
使用HashOperations操作redis----https://www.cnblogs.com/shiguotao-com/p/10560458.html 使用HashOperations操作r ...
- Springboot集成Swagger操作步骤
特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...
- SpringBoot集成Elasticsearch7.6
前言: 本文不赘述Elasticsearch的相关基础知识点和部署,只介绍如何在SpringBoot如何集成Elasticsearch并进行数据操作 Spring Data项目中提供了操作es的框架S ...
- springboot集成websocket实现向前端浏览器发送一个对象,发送消息操作手动触发
工作中有这样一个需示,我们把项目中用到代码缓存到前端浏览器IndexedDB里面,当系统管理员在后台对代码进行变动操作时我们要更新前端缓存中的代码怎么做开始用想用版本方式来处理,但这样的话每次使用代码 ...
- Windows环境下springboot集成redis的安装与使用
一,redis安装 首先我们需要下载Windows版本的redis压缩包地址如下: https://github.com/MicrosoftArchive/redis/releases 连接打开后如下 ...
随机推荐
- MySQL性能分析之Explain
目录 Explain基础 Explain进阶 Explain基础 关于explain命令相信大家并不陌生,具体用法和字段含义可以参考官网explain-output ,这里需要强调rows是核心指标, ...
- ZooKeeper入门(二) Zookeeper选举
1 背景 1.1 什么是leader选举 在zookeeper集群中,每个节点都会投票,如果某个节点获得超过半数以上的节点的投票,则该节点就是leader节点了 1.2 zookeeper集群选举le ...
- Codeforces 617E:XOR and Favorite Number(莫队算法)
http://codeforces.com/problemset/problem/617/E 题意:给出n个数,q个询问区间,问这个区间里面有多少个区间[i,j]可以使得ai^ai+1^...^aj ...
- InstallShield 2018 打包安装
关于InstallShield 2018打包安装程序的使用 1. 下载InstallShield2018 建议使用新的版本,毕竟新的版本功能功能全.问题少.用户体验佳. 下载地址:http://www ...
- Coder必须自废的两样神功
Coder必须自废的两样神功 大理段氏以一阳指神功驰名天下.奉六脉宝经,立天龙佛院:凭借数百载基业威名,与嵩山少林.终南全真分庭抗礼:乃宋代中华武术三大派系之一. 二指禅是一种鲜为人知的秘传功法,通过 ...
- RDIFramework.NET框架基于Quartz.Net实现任务调度详解及效果展示
在上一篇Quartz.Net实现作业定时调度详解,我们通过实例代码详细讲解与演示了基于Quartz.NET开发的详细方法.本篇我们主要讲述基于RDIFramework.NET框架整合Quartz.NE ...
- MyBatis从入门到精通:第一章配置MyBatis
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC ...
- CF39D Cubical Planet-C++
银河系中没有你找不到的东西!有一颗形状为立方体的的行星正在绕着一颗形状为二十面体的恒星运转.现在我们让这颗行星的两个在同一条体对角线上的顶点置于(0,0,0)和(1,1,1)上.有两只苍蝇住在行星上. ...
- BI之路学习笔记1--SSIS包的认识和设计
进入了新的公司,开始接触新的方向,内心激动而又兴奋,对于BI以前知道的极少,从今天开始要好好学习了~ BI的概念,功能,强大之处在此先不做赘述,BI之路先要一步一个脚印扎实做起,现在正在看的也是之前好 ...
- weblogic安装时检查监视器: 必须配置为至少显示 256 种颜色,实际空间未知→失败
1.首先如果你出现的结果是[未通过],则设置DISPLAY环境变量. 按网上方法:export DISPLAY=:0.0 然后继续安装你的东西……若成功则恭喜你~ 若[失败],按网上方法让你去看日志 ...