Java Code Examples for redis.clients.jedis.Jedis#sadd()

The following examples show how to use redis.clients.jedis.Jedis#sadd() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JedisUtil.java    From scaffold-cloud with MIT License 6 votes vote down vote up
/**
 * 设置Set缓存
 *
 * @param key          键
 * @param value        值
 * @param cacheSeconds 超时时间,0为不超时
 * @return
 */
public static long setObjectSet(String key, Set<Object> value, int cacheSeconds) {
    long result = 0;
    Jedis jedis = null;
    try {
        jedis = getResource();
        if (jedis.exists(getBytesKey(key))) {
            jedis.del(key);
        }
        for (Object o : value) {
            result += jedis.sadd(getBytesKey(key), toBytes(o));
        }

        if (cacheSeconds != 0) {
            jedis.expire(key, cacheSeconds);
        }
    } catch (Exception e) {
        logger.warn("setObjectSet {} = {}", key, value, e);
    } finally {
        close(jedis);
    }
    return result;
}
 
Example 2
Source File: RedisDAO.java    From jseckill with Apache License 2.0 6 votes vote down vote up
public void setAllGoods(List<Seckill> list) {
    Jedis jedis = jedisPool.getResource();
    if (list == null || list.size()< 1) {
        logger.info("--FatalError!!! seckill_list_data is empty");
        return;
    }

    jedis.del(RedisKey.SECKILL_ID_SET);

    for (Seckill seckill : list) {
        jedis.sadd(RedisKey.SECKILL_ID_SET, seckill.getSeckillId() + "");

        String seckillGoodsKey = RedisKeyPrefix.SECKILL_GOODS + seckill.getSeckillId();
        byte[] goodsBytes = ProtostuffIOUtil.toByteArray(seckill, MyRuntimeSchema.getInstance().getGoodsRuntimeSchema(),
                LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE));
        jedis.set(seckillGoodsKey.getBytes(), goodsBytes);
    }
    jedis.close();
    logger.info("数据库Goods数据同步到Redis完毕!");
}
 
Example 3
Source File: JedisUtil.java    From scaffold-cloud with MIT License 6 votes vote down vote up
/**
 * 向Set缓存中添加值
 *
 * @param key   键
 * @param value 值
 * @return
 */
public static long saddObj(String key, Object... value) {
    long result = 0;
    Jedis jedis = null;
    try {
        jedis = getResource();
        for (Object o : value) {
            result += jedis.sadd(getBytesKey(key), toBytes(o));
        }
    } catch (Exception e) {
        logger.warn("setSetObjectAdd {} = {}", key, value, e);
    } finally {
        close(jedis);
    }
    return result;
}
 
Example 4
Source File: RedisDataSetTest.java    From jmeter-plugins with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws IOException {
    // Set up thread variables
    JMeterContext jmcx = JMeterContextService.getContext();
    jmcx.setVariables(new JMeterVariables());
    threadVars = jmcx.getVariables();

    // Create new mock RedisServer
    server = RedisServer.newRedisServer(6370);
    server.start();

    // Setup test data
    JedisPool pool = new JedisPool(new JedisPoolConfig(), "localhost", 6370);
    Jedis jedis = pool.getResource();

    jedis.rpush("testRecycleList", "12345678,1234","12345679,1235", "12345680,1236");
    jedis.rpush("testConsumeList", "12345678,1234","12345679,1235", "12345680,1236");
    jedis.sadd("testRecycleSet", "12345678,1234","12345679,1235", "12345680,1236");
    jedis.sadd("testConsumeSet", "12345678,1234","12345679,1235", "12345680,1236");

    jedis.shutdown();

}
 
Example 5
Source File: JedisTransactionDemo.java    From Redis-4.x-Cookbook with MIT License 6 votes vote down vote up
public static void main(String[] args) {
    //Connecting to localhost Redis server
    Jedis jedis = new Jedis("localhost");

    //Initialize
    String user = "user:1000";
    String restaurantOrderCount = "restaurant_orders:200";
    String restaurantUsers = "restaurant_users:200";
    jedis.set(restaurantOrderCount, "400");
    jedis.sadd(restaurantUsers, "user:302", "user:401");

    //Create a Redis transaction
    Transaction transaction = jedis.multi();
    Response<Long> countResponse = transaction.incr(restaurantOrderCount);
    transaction.sadd(restaurantUsers, user);
    Response<Set<String>> userSet = transaction.smembers(restaurantUsers);
    //Execute transaction
    transaction.exec();

    //Handle responses
    System.out.printf("Number of orders: %d\n", countResponse.get());
    System.out.printf("Users: %s\n", userSet.get());
    System.exit(0);
}
 
Example 6
Source File: RedisJobStore.java    From redis-quartz with MIT License 6 votes vote down vote up
/**
 * Stores job in redis.
 *
 * @param newJob the new job
 * @param replaceExisting the replace existing
 * @param jedis thread-safe redis connection
 * @throws ObjectAlreadyExistsException
 */
private void storeJob(JobDetail newJob, boolean replaceExisting, Jedis jedis)
		throws ObjectAlreadyExistsException {
	String jobHashKey = createJobHashKey(newJob.getKey().getGroup(), newJob.getKey().getName());
	String jobDataMapHashKey = createJobDataMapHashKey(newJob.getKey().getGroup(), newJob.getKey().getName());
	String jobGroupSetKey = createJobGroupSetKey(newJob.getKey().getGroup());
	
	if (jedis.exists(jobHashKey) && !replaceExisting)
		throw new ObjectAlreadyExistsException(newJob);
				
     Map<String, String> jobDetails = new HashMap<>();
	jobDetails.put(DESCRIPTION, newJob.getDescription() != null ? newJob.getDescription() : "");
	jobDetails.put(JOB_CLASS, newJob.getJobClass().getName());
	jobDetails.put(IS_DURABLE, Boolean.toString(newJob.isDurable()));
	jobDetails.put(BLOCKED_BY, "");
	jobDetails.put(BLOCK_TIME, "");
	jedis.hmset(jobHashKey, jobDetails);
	
	if (newJob.getJobDataMap() != null && !newJob.getJobDataMap().isEmpty())
		jedis.hmset(jobDataMapHashKey, getStringDataMap(newJob.getJobDataMap()));			
	
	jedis.sadd(JOBS_SET, jobHashKey);
	jedis.sadd(JOB_GROUPS_SET, jobGroupSetKey);
	jedis.sadd(jobGroupSetKey, jobHashKey);
}
 
Example 7
Source File: TestRedisQParserPluginIT.java    From solr-redis with Apache License 2.0 6 votes vote down vote up
@Before
@Override
public void setUp() throws Exception {
  super.setUp();
  clearIndex();
  assertU(commit());

  try {
    jedis = new Jedis("localhost");
    jedis.flushAll();
    jedis.sadd("test_set", "test");
    jedis.hset("test_hash", "key1", "value1");
    jedis.lpush("test_list", "element1");
  } catch (final RuntimeException ex) {
    log.error("Errorw when configuring local Jedis connection", ex);
  }
}
 
Example 8
Source File: RedisDataSet.java    From jmeter-plugins with Apache License 2.0 6 votes vote down vote up
private void addDataToConnection(Jedis conn, String key, String data) {
    try {
        if (redisDataType == RedisDataType.REDIS_DATA_TYPE_LIST) {
            log.debug("Executing rpush against redis list");
            // Add data string to list's tail
            conn.rpush(redisKey, data);
        } else if (redisDataType == RedisDataType.REDIS_DATA_TYPE_SET) {
            log.debug("Executing sadd against redis set");
            conn.sadd(key, data);
        } else {
            log.warn("Unexpected redis datatype: {0}".format(key));
        }
    } catch (JedisDataException jde) {
        log.error("Exception when adding data to Redis: " + jde);
    }
}
 
Example 9
Source File: RedisClient.java    From Mykit with Apache License 2.0 6 votes vote down vote up
public <T> long sadd(String key, List<T> ts) {
	Jedis client = jedisPool.getResource();
	try {
		if (ts == null || ts.size() == 0) {
			return 0l;
		}
		String[] values = new String[ts.size()];
		for (int i = 0; i < ts.size(); i++) {
			values[i] = ts.get(i).toString();
		}
		return client.sadd(key, values);
	} finally {
		// 向连接池“归还”资源
		jedisPool.returnResourceObject(client);
	}
}
 
Example 10
Source File: JedisUtils.java    From Shop-for-JavaWeb with MIT License 6 votes vote down vote up
/**
 * 设置Set缓存
 * @param key 键
 * @param value 值
 * @param cacheSeconds 超时时间,0为不超时
 * @return
 */
public static long setSet(String key, Set<String> value, int cacheSeconds) {
	long result = 0;
	Jedis jedis = null;
	try {
		jedis = getResource();
		if (jedis.exists(key)) {
			jedis.del(key);
		}
		result = jedis.sadd(key, (String[])value.toArray());
		if (cacheSeconds != 0) {
			jedis.expire(key, cacheSeconds);
		}
		logger.debug("setSet {} = {}", key, value);
	} catch (Exception e) {
		logger.warn("setSet {} = {}", key, value, e);
	} finally {
		returnResource(jedis);
	}
	return result;
}
 
Example 11
Source File: JedisClientPool.java    From blog-sample with Apache License 2.0 5 votes vote down vote up
@Override
public Long sadd(String key, String... members) {
    Jedis jedis = jedisPool.getResource();
    Long result = jedis.sadd(key, members);
    jedis.close();
    return result;
}
 
Example 12
Source File: JedisUtils.java    From easyweb with Apache License 2.0 5 votes vote down vote up
/**
 * 向Set缓存中添加值
 * @param key 键
 * @param value 值
 * @return
 */
public static long setSetAdd(String key, String... value) {
	long result = 0;
	Jedis jedis = null;
	try {
		jedis = getResource();
		result = jedis.sadd(key, value);
		logger.debug("setSetAdd {} = {}", key, value);
	} catch (Exception e) {
		logger.warn("setSetAdd {} = {}", key, value, e);
	} finally {
		returnResource(jedis);
	}
	return result;
}
 
Example 13
Source File: QuoteFactDB.java    From VileBot with MIT License 5 votes vote down vote up
/**
 * Add a quote to the quote set of a noun.
 *
 * @param noun The noun to add a quote to
 * @param quote The new quote
 */
public static void addQuote( String noun, String quote )
{
    Jedis jedis = pool.getResource();
    try
    {
        jedis.sadd( keyOfQuoteSetsPrefix + noun, quote );
    }
    finally
    {
        pool.returnResource( jedis );
    }
}
 
Example 14
Source File: JedisUtils.java    From fw-spring-cloud with Apache License 2.0 5 votes vote down vote up
/**
 * <p>
 * 通过key向指定的set中添加value
 * </p>
 *
 * @param key
 * @param members
 *            可以是一个String 也可以是一个String数组
 * @return 添加成功的个数
 */
public Long sadd(String key, String... members) {
    Jedis jedis = null;
    Long res = null;
    try {
        jedis = jedisPool.getResource();
        res = jedis.sadd(key, members);
    } catch (Exception e) {

        log.error(e.getMessage());
    } finally {
        returnResource(jedisPool, jedis);
    }
    return res;
}
 
Example 15
Source File: SetOperUtil.java    From springboot-learn with MIT License 5 votes vote down vote up
public static void oper(Jedis jedis) {

        Long id = jedis.sadd("set", "1");
        Long id2 = jedis.sadd("set", "1", "12");
        Set<String> smembers = jedis.smembers("set");
        System.out.println("smembers:" + smembers);
        System.out.println("set type:" + jedis.type("hash"));

    }
 
Example 16
Source File: ExcuseDB.java    From VileBot with MIT License 5 votes vote down vote up
public static void addExcuse( String excuse )
{
    Jedis jedis = pool.getResource();
    try
    {
        jedis.sadd( keyOfExcuseSet, excuse );
    }
    finally
    {
        pool.returnResource( jedis );
    }
}
 
Example 17
Source File: Main.java    From JavaBase with MIT License 4 votes vote down vote up
private void transferSet(String key, Jedis originJedis, Jedis targetJedis) {
  Set<String> members = originJedis.smembers(key);
  String[] temp = new String[members.size()];
  String[] result = members.toArray(temp);
  targetJedis.sadd(key, result);
}
 
Example 18
Source File: JedisUtil.java    From BigData with GNU General Public License v3.0 4 votes vote down vote up
public long sadd(byte[] key, byte[] member) {
	Jedis jedis = getJedis();
	long s = jedis.sadd(key, member);
	returnJedis(jedis);
	return s;
}
 
Example 19
Source File: RedisMetricsDatastore.java    From java-slack-sdk with MIT License 4 votes vote down vote up
private void addToMessageIdsKeyIndices(Jedis jedis, String statsKey) {
    jedis.sadd("MessageIdsKeys", statsKey);
}
 
Example 20
Source File: RedisClient.java    From springboot-learn with MIT License 2 votes vote down vote up
/**
 * 集合添加
 *
 * @param key
 * @param members
 */
public void add(String key, String... members) {
    Jedis jedis = jedisPool.getResource();
    jedis.sadd(key, members);
}