题外话:

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的更多相关文章

  1. Springboot集成Jedis + Redisson(已自测)

    原文:https://blog.csdn.net/c_zyer/article/details/79415728 本文主要跟大家分享在Springboot中集成Jedis和Redisson的方法.为什 ...

  2. Spring-Boot 使用 Jedis 操作 Redis

    背景: 1.Redis 之前学了个皮毛 还忘的差不多了,感觉公司项目中的Redis用的真的牛逼,so 需要深造. 2.有个同事在搞Jedis,勾起了我对知识的向往,不会用,但是很渴望. 过程: 1.改 ...

  3. Redis系统学习之SpringBoot集成Redis操作API(集成SpringDataRedis及其分析)

    SpringDataRedis调用Redis底层解读 在SpringBoot2.X之前还是直接使用的官方推荐的Jedis连接的Redis 在2.X之后换为了lettuce Jedis:采用直接连接,多 ...

  4. springboot集成jpa操作mybatis数据库

    数据库如下 CREATE TABLE `jpa`.`Untitled` ( `cust_id` bigint() NOT NULL AUTO_INCREMENT, `cust_address` var ...

  5. springboot集成redis操作

    使用HashOperations操作redis----https://www.cnblogs.com/shiguotao-com/p/10560458.html 使用HashOperations操作r ...

  6. Springboot集成Swagger操作步骤

    特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...

  7. SpringBoot集成Elasticsearch7.6

    前言: 本文不赘述Elasticsearch的相关基础知识点和部署,只介绍如何在SpringBoot如何集成Elasticsearch并进行数据操作 Spring Data项目中提供了操作es的框架S ...

  8. springboot集成websocket实现向前端浏览器发送一个对象,发送消息操作手动触发

    工作中有这样一个需示,我们把项目中用到代码缓存到前端浏览器IndexedDB里面,当系统管理员在后台对代码进行变动操作时我们要更新前端缓存中的代码怎么做开始用想用版本方式来处理,但这样的话每次使用代码 ...

  9. Windows环境下springboot集成redis的安装与使用

    一,redis安装 首先我们需要下载Windows版本的redis压缩包地址如下: https://github.com/MicrosoftArchive/redis/releases 连接打开后如下 ...

随机推荐

  1. Don’t Repeat Yourself

    The Don’t Repeat Yourself (DRY) principle states that duplication in logic should be eliminated via ...

  2. 深入理解C#的装箱和拆箱

    个人理解(本质): 封箱是把值类型转换为引用类型 拆箱是把引用类型转换为值类型 封箱是把值类型转换为System.Object类型,或者转换为由值类型实现的接口类型: 例如: struct Mystr ...

  3. XAML与C#与WPF三者到底有什么关系?

    XAML是.NET体系开发程序或者网页时前台编程的一种布局方式或者说开发语言,可以比较自由的用标签的方式进行布局,借鉴了HTML和XML等语言的风格,并且加入了一些动画等的实现.C#则是后台逻辑开发用 ...

  4. 阿里云体验:安装jdk

    在阿里云的linux服务器上默认是没有安装java环境的,需要自己安装.查了许多资料,发现这篇文章简洁易用.http://www.cnblogs.com/cloudwind/archive/2012/ ...

  5. .Net Core 学习依赖注入自定义Service

    1. 定义一个服务,包含一个方法 public class TextService { public string Print(string m) { return m; } } 2. 写一个扩展方法 ...

  6. 关于CMTS设备的一些备忘

    博主工作内容包括cable modem,对CM的工作方式有一些了解,但是对CMTS头端怎么带动一个用户小区长久以来一直是一头雾水.今天找了些资料,对这块有了一些了解,并把自己的理解总结下来. 比如我家 ...

  7. 基于Actor模型的CQRS、ES解决方案分享

    开场白 大家晚上好,我是郑承良,跟大家分享的话题是<基于Actor模型的CQRS/ES解决方案分享>,最近一段时间我一直是这个话题的学习者.追随者,这个话题目前生产环境落地的资料少一些,分 ...

  8. centos7 linux下增加swap虚拟内存分区大小

    此方法不限于centos,linux均适用 最近在服务器上部署了一个java项目,java进程经常性莫名被自动Kill,首先java程序是没有报错的,那么我想可能是内存不足的原因,因为4G内存的服务上 ...

  9. 分布式Streaming Data Processing - Samza

    ​ 现在的主流的互联网应用越来越依赖streaming data来提供用户一些interesting statistics insights.以linkedin为例,最近90天有多少人看过你的link ...

  10. Excel催化剂开源第26波-Excel离线生成二维码条形码

    在中国特有环境下,二维码.条形码的使用场景非常广泛,因Excel本身就是一个非常不错的报表生成环境,若Excel上能够直接生成二维码.条形码,且是批量化操作的,直接一条龙从数据到报表都由Excel完成 ...