题外话:

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. mysql-8.0.16-winx64/Linux修改root用户密码

    连接数据库等基础操作请自行解决哈,本篇是重点记录如何改密码. 一.查询用户密码: 查询用户密码命令: select host, user, authentication_string from mys ...

  2. 为什么Java只有值传递?

    形参和实参 形式参数,是在方法定义阶段,是定义某个函数时使用的参数,用于接收实参传入.例f(x,y)中x和y是形参. 实际参数,是在方法调用阶段,是主调函数调用有参函数时,实际传递的内容.例f(3,7 ...

  3. django基础知识之状态保持session:

    状态保持 http协议是无状态的:每次请求都是一次新的请求,不会记得之前通信的状态 客户端与服务器端的一次通信,就是一次会话 实现状态保持的方式:在客户端或服务器端存储与会话有关的数据 存储方式包括c ...

  4. 洛谷p1216 IOI1994 Day1T1

    洛谷p1216 IOI1994 Day1T1 洛谷原题 题目描述 观察下面的数字金字塔. 写一个程序来查找从最高点到底部任意处结束的路径,使路径经过数字的和最大.每一步可以走到左下方的点也可以到达右下 ...

  5. python基础认识(一)

    这些日子以来,新闻铺天盖地的都是人工智能,那么借着这股潮流,python也随之火起来了,现在的python不仅仅可以进行人工智能领域的开发.还可以进行web.爬虫等领域的运用.因此,我认为作为一个紧跟 ...

  6. 《转载黑马教程》HTML&&CSS讲义0,,包含教程_仅供参考

    今日内容 1. web概念概述 2. HTML web概念概述 * JavaWeb: * 使用Java语言开发基于互联网的项目 * 软件架构: 1. C/S: Client/Server 客户端/服务 ...

  7. Apache Dubbo已不再局限于Java语言

    2017 年 9 月 7 日,在沉寂了4年之后,Dubbo 悄悄的在 GitHub 发布了 2.5.4 版本.随后又迅速发布了 2.5.5.2.5.6.2.5.7 等release.在 2017年 1 ...

  8. CI工具Jenkins的安装配置【linux】——jenkins集成sonarqube-异常解决

    Setup 官网https://jenkins.io/ 下载war包,扔到tomcat下启动即可. 如果有port限制,在iptables中打开商品限制. 访问http://ip:port/jenki ...

  9. springcloud-路由gateway

    1. 场景描述 springcloud刚推出的时候用的是netflix全家桶,路由用的zuul(springcloud-路由Zull),但是据说zull1.0在大数据量访问的时候存在较大性能问题,2. ...

  10. EnjoyingSoft之Mule ESB开发教程第三篇:Mule message structure - Mule message结构

    目录 1. 探索Mule Message结构 2. Mule Message的Payload 3. Mule Message的Property 4. Mule Message的Attachment 5 ...