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 连接打开后如下 ...
随机推荐
- python的数据类型之字符串(二)
字符串常见操作 如有字符串mystr = 'hello xiaose',以下是常见的操作 1.find 检测某个字符串是否包含在 mystr中,如果是返回开始的索引值,否则返回-1 格式:mystr. ...
- 常用的方法论-PARR
- 2018.9.8 2018NOIP冲刺之配对
普及组第四题难度 主体思路竟然是贪心Q_Q 链接:https://www.nowcoder.com/acm/contest/164/D来源:牛客网 题目描述 小A有n个长度都是L的字符串.这些字符串只 ...
- 深入理解Java虚拟机笔记——虚拟机类加载机制
目录 概述 动态加载和动态连接 类加载的时机 类的生命周期 被动引用 例子一(调用子类继承父类的字段) 例子二(数组) 例子三(静态常量) 类加载的过程 加载 验证 准备 解析 符号引用 直接引用 初 ...
- HDU 1542:Atlantis(扫描线+线段树 矩形面积并)***
题目链接 题意 给出n个矩形,求面积并. 思路 使用扫描线,我这里离散化y轴,按照x坐标从左往右扫过去.离散化后的y轴可以用线段树维护整个y上面的线段总长度,当碰到扫描线的时候,就可以统计面积.这里要 ...
- CodeForces 691D:Swaps in Permutation(并查集)
http://codeforces.com/contest/691/problem/D D. Swaps in Permutation You are given a permutation of ...
- SpringBoot项目构建成jar运行后,如何正确读取resource下的文件
SpringBoot项目构建成jar运行后,如何正确读取resource下的文件 不管你使用的是SpringBoot 1.x还是SpringBoot2.x,在开Dev环境中使用eclipse.IEAD ...
- 盘一盘 System.out.println()
System.out.println("Hello World")是大部分程序员入门的第一行代码,也可以说是程序员们最熟悉的一行代码.大家真的深入研究过System.out.pri ...
- 【超详细】vultr(CentOS7)+LNMP+WordPress 搭建个人博客
心血来潮想搭建个人博客,我的vps只用来搭ss未免太过浪费 在这里记录一下搭建个人博客的历程 0x00 写在前面 why vultr: 优点:便宜. 性能优良.按时间计费(不用包年 学生党的福音).稳 ...
- websocket的加密和解密过程
加密: import struct msg_bytes = "the emperor has not been half-baked in the early days of the col ...