Jedis操作redis--Set篇

本文涉及的产品
Redis 开源版,标准版 2GB
推荐场景:
搭建游戏排行榜
云数据库 Tair(兼容Redis),内存型 2GB
简介:
  1. package com.component.jedis;

  2. import java.util.Set;

  3. import redis.clients.jedis.Jedis;


  4. public class JedisSet extends JedisCommon{
  5.         
  6.         private static final String OK_CODE = "OK";
  7.     private static final String OK_MULTI_CODE = "+OK";
  8.     
  9.     /**
  10.      * 把一个或多个元素添加到指定集合
  11.      * 
  12.      * @param key
  13.      * @param members
  14.      * @return
  15.      */
  16.     public static Long sadd(String key, String members){
  17.         Jedis jedis = JedisManager.getJedis();
  18.         Long result ;
  19.         try {
  20.             result = jedis.sadd(key, members);
  21.         }finally{
  22.             JedisManager.returnResource(jedis);
  23.         }
  24.         return result;
  25.     }
  26.     
  27.     /**
  28.      * 返回多个集合的差集
  29.      * @param keys
  30.      * @return
  31.      */
  32.     public static Set<String> sdiff(String... keys){
  33.         Jedis jedis = JedisManager.getJedis();
  34.         Set<String> result ;
  35.         try {
  36.             result = jedis.sdiff(keys);
  37.         }finally{
  38.             JedisManager.returnResource(jedis);
  39.         }
  40.         return result;
  41.     }
  42.     
  43.     /**
  44.      * 返回多个集合的交集
  45.      * 
  46.      * @param keys
  47.      * @return
  48.      */
  49.     public static Set<String> sinter(String... keys){
  50.         Jedis jedis = JedisManager.getJedis();
  51.         Set<String> result ;
  52.         try {
  53.             result = jedis.sinter(keys);
  54.         }finally{
  55.             JedisManager.returnResource(jedis);
  56.         }
  57.         return result;
  58.     }
  59.     
  60.     /**
  61.      * 判断元素是否是集合成员
  62.      * 
  63.      * @param key
  64.      * @param member
  65.      * @return
  66.      */
  67.     public static boolean sismember(String key, String member){
  68.         Jedis jedis = JedisManager.getJedis();
  69.         boolean result ;
  70.         try {
  71.             result = jedis.sismember(key, member);
  72.         }finally{
  73.             JedisManager.returnResource(jedis);
  74.         }
  75.         return result;
  76.     }
  77.     
  78.     /**
  79.      * 返回集合所有成员
  80.      * 
  81.      * @param key
  82.      * @return
  83.      */
  84.     public static Set<String> smembers(String key){
  85.         Jedis jedis = JedisManager.getJedis();
  86.         Set<String> result ;
  87.         try {
  88.             result = jedis.smembers(key);
  89.         }finally{
  90.             JedisManager.returnResource(jedis);
  91.         }
  92.         return result;
  93.     }
  94.     
  95.     /**
  96.      * 把指定成员从一个集合移动到目标集合,
  97.      *  指定成员不存在,不执行任何操作 
  98.      * @param srckey
  99.      * @param dstkey
  100.      * @param member
  101.      * @return
  102.      */
  103.     public static Long smove(String srckey, String dstkey, String member){
  104.         Jedis jedis = JedisManager.getJedis();
  105.         Long result ;
  106.         try {
  107.             result = jedis.smove(srckey, dstkey, member);
  108.         }finally{
  109.             JedisManager.returnResource(jedis);
  110.         }
  111.         return result;
  112.     }
  113.     
  114.     /**
  115.      * 随机移除并返回一个元素
  116.      * 
  117.      * @param key
  118.      * @return
  119.      */
  120.     public static String spop(String key){
  121.         Jedis jedis = JedisManager.getJedis();
  122.         String result ;
  123.         try {
  124.             result = jedis.spop(key);
  125.         }finally{
  126.             JedisManager.returnResource(jedis);
  127.         }
  128.         return result;
  129.     }
  130.     
  131.     /**
  132.      * 移除一个或多个元素,不存在的  元素会被忽略
  133.      * 
  134.      * @param key
  135.      * @param members
  136.      * @return
  137.      */
  138.     public static Long srem(String key, String... members){
  139.         Jedis jedis = JedisManager.getJedis();
  140.         Long result ;
  141.         try {
  142.             result = jedis.srem(key, members);
  143.         }finally{
  144.             JedisManager.returnResource(jedis);
  145.         }
  146.         return result;
  147.     }
  148.     
  149.     /**
  150.      * 返回指定集合的并集
  151.      * 
  152.      * @param keys
  153.      * @return
  154.      */
  155.     public static Set<String> sunion(String... keys){
  156.         Jedis jedis = JedisManager.getJedis();
  157.         Set<String> result ;
  158.         try {
  159.             result = jedis.sunion(keys);
  160.         }finally{
  161.             JedisManager.returnResource(jedis);
  162.         }
  163.         return result;
  164.     }
  165.     
  166.     /**
  167.      * 当key不存在时,才放入值,超时时间单位为秒
  168.      * @param jedis
  169.      * @param key
  170.      * @param value
  171.      * @return
  172.      */
  173.     public static boolean set(String key, String value, Long timeOut){
  174.         
  175.         Jedis jedis = null;
  176.         String resp = null;
  177.         try{
  178.             jedis = JedisManager.getJedis();
  179.             resp = jedis.set(key, value, "NX", "EX", timeOut == null ? 1L : timeOut);
  180.         } finally{
  181.             JedisManager.returnResource(jedis);  
  182.         }
  183.         return isStatusOk(resp);
  184.     }
  185.     
  186.     /**
  187.      * 判断 返回值是否ok.
  188.      */
  189.     public static boolean isStatusOk(String status) {
  190.         return (status != null) && (OK_CODE.equals(status) || OK_MULTI_CODE.equals(status));
  191.     }
  192. }





本文作者: zhaohui520013
本文来自云栖社区合作伙伴rediscn,了解相关信息可以关注redis.cn网站。
相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
目录
相关文章
|
5月前
|
NoSQL Java API
Redis官方推荐的Java连接开发工具Jedis
Redis官方推荐的Java连接开发工具Jedis
|
17天前
|
NoSQL 网络协议 Java
[Redis] 渐进式遍历+使用jedis操作Redis+使用Spring操作Redis
[Redis] 渐进式遍历+使用jedis操作Redis+使用Spring操作Redis
28 7
|
5月前
|
NoSQL Java Redis
redis-学习笔记(Jedis 通用命令)
redis-学习笔记(Jedis 通用命令)
46 1
|
18天前
|
NoSQL Java 网络安全
[Redis] 渐进式遍历+使用jedis操作Redis+使用Spring操作Redis
[Redis] 渐进式遍历+使用jedis操作Redis+使用Spring操作Redis
|
14天前
|
JSON NoSQL Java
redis的java客户端的使用(Jedis、SpringDataRedis、SpringBoot整合redis、redisTemplate序列化及stringRedisTemplate序列化)
这篇文章介绍了在Java中使用Redis客户端的几种方法,包括Jedis、SpringDataRedis和SpringBoot整合Redis的操作。文章详细解释了Jedis的基本使用步骤,Jedis连接池的创建和使用,以及在SpringBoot项目中如何配置和使用RedisTemplate和StringRedisTemplate。此外,还探讨了RedisTemplate序列化的两种实践方案,包括默认的JDK序列化和自定义的JSON序列化,以及StringRedisTemplate的使用,它要求键和值都必须是String类型。
redis的java客户端的使用(Jedis、SpringDataRedis、SpringBoot整合redis、redisTemplate序列化及stringRedisTemplate序列化)
|
2月前
|
NoSQL Java Linux
Jedis测试redis。(redis在linux虚拟机中)
该博客文章提供了使用Jedis客户端连接Linux虚拟机中的Redis服务器的步骤,包括Maven依赖配置、测试用例编写以及测试结果的截图。
|
2月前
|
缓存 NoSQL Java
【Azure Redis 缓存】定位Java Spring Boot 使用 Jedis 或 Lettuce 无法连接到 Redis的网络连通性步骤
【Azure Redis 缓存】定位Java Spring Boot 使用 Jedis 或 Lettuce 无法连接到 Redis的网络连通性步骤
|
2月前
|
缓存 NoSQL Java
【Azure Redis 缓存 Azure Cache For Redis】当使用Jedis客户端连接Redis时候,遇见JedisConnectionException: Could not get a resource from the pool / Redis connection los
【Azure Redis 缓存 Azure Cache For Redis】当使用Jedis客户端连接Redis时候,遇见JedisConnectionException: Could not get a resource from the pool / Redis connection los
102 0
|
3月前
|
Java Redis 数据安全/隐私保护
Redis14----Redis的java客户端-jedis的连接池,jedis本身是线程不安全的,并且频繁的创建和销毁连接会有性能损耗,最好用jedis连接池代替jedis,配置端口,密码
Redis14----Redis的java客户端-jedis的连接池,jedis本身是线程不安全的,并且频繁的创建和销毁连接会有性能损耗,最好用jedis连接池代替jedis,配置端口,密码
|
3月前
|
Java Redis 数据安全/隐私保护
Redis13的Java客户端-Jedis快速入门,建立连接的写法,ip地址,设置密码密码,选择库的写法
Redis13的Java客户端-Jedis快速入门,建立连接的写法,ip地址,设置密码密码,选择库的写法