Java Code Examples for redis.clients.jedis.Jedis#set()
The following examples show how to use
redis.clients.jedis.Jedis#set() .
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: RedisService.java From seckill with Apache License 2.0 | 8 votes |
/** * 设置对象 * * @param prefix * @param key * @param value * @param <T> * @return */ public <T> boolean set(KeyPrefix prefix, String key, T value) { Jedis jedis = null; try { jedis = jedisPool.getResource(); String str = beanToString(value); if (str == null || str.length() < 0) { return false; } //生成真正的key String realKey = prefix.getPrefix() + key; int seconds = prefix.expireSeconds(); if (seconds < 1) { jedis.set(realKey, str); } else { jedis.setex(realKey, seconds, str); } return true; } finally { returnToPool(jedis); } }
Example 2
Source File: JedisSentinelPoolTest.java From cachecloud with Apache License 2.0 | 6 votes |
@Test public void checkResourceIsCloseable() { GenericObjectPoolConfig config = new GenericObjectPoolConfig(); config.setMaxTotal(1); config.setBlockWhenExhausted(false); JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels, config, 1000, "foobared", 2); Jedis jedis = pool.getResource(); try { jedis.set("hello", "jedis"); } finally { jedis.close(); } Jedis jedis2 = pool.getResource(); try { assertEquals(jedis, jedis2); } finally { jedis2.close(); } }
Example 3
Source File: JedisPoolTest.java From cachecloud with Apache License 2.0 | 6 votes |
@Test public void checkJedisIsReusedWhenReturned() { JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort()); Jedis jedis = pool.getResource(); jedis.auth("foobared"); jedis.set("foo", "0"); jedis.close(); jedis = pool.getResource(); jedis.auth("foobared"); jedis.incr("foo"); jedis.close(); pool.destroy(); assertTrue(pool.isClosed()); }
Example 4
Source File: JedisTransactionDemo.java From Redis-4.x-Cookbook with MIT License | 6 votes |
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 5
Source File: RedisClient.java From springboot-learn with MIT License | 5 votes |
/** * 写入缓存 * * @param key * @param value * @return Boolean */ public String set(final String key, String value) { Jedis jedis = null; try { jedis = getJedis(); return jedis.set(key, String.valueOf(value)); } catch (Exception e) { logger.error("[RedisClient] set e,", e); return ""; } finally { close(jedis); } }
Example 6
Source File: ToxiproxyTest.java From testcontainers-java with MIT License | 5 votes |
@Test public void testMultipleProxiesCanBeCreated() { try (GenericContainer secondRedis = new GenericContainer("redis:5.0.4") .withExposedPorts(6379) .withNetwork(network)) { secondRedis.start(); final ToxiproxyContainer.ContainerProxy firstProxy = toxiproxy.getProxy(redis, 6379); final ToxiproxyContainer.ContainerProxy secondProxy = toxiproxy.getProxy(secondRedis, 6379); final Jedis firstJedis = createJedis(firstProxy.getContainerIpAddress(), firstProxy.getProxyPort()); final Jedis secondJedis = createJedis(secondProxy.getContainerIpAddress(), secondProxy.getProxyPort()); firstJedis.set("somekey", "somevalue"); secondJedis.set("somekey", "somevalue"); firstProxy.setConnectionCut(true); assertThrows("calls fail when the connection is cut, for only the relevant proxy", JedisConnectionException.class, () -> { firstJedis.get("somekey"); }); assertEquals("access via a different proxy is OK", "somevalue", secondJedis.get("somekey")); } }
Example 7
Source File: JedisPoolTest.java From cachecloud with Apache License 2.0 | 5 votes |
@Test(expected = JedisConnectionException.class) public void checkPoolOverflow() { GenericObjectPoolConfig config = new GenericObjectPoolConfig(); config.setMaxTotal(1); config.setBlockWhenExhausted(false); JedisPool pool = new JedisPool(config, hnp.getHost(), hnp.getPort()); Jedis jedis = pool.getResource(); jedis.auth("foobared"); jedis.set("foo", "0"); Jedis newJedis = pool.getResource(); newJedis.auth("foobared"); newJedis.incr("foo"); }
Example 8
Source File: PoolManagementTest.java From MythRedisClient with Apache License 2.0 | 5 votes |
@Test public void testGetRedisPool() throws Exception { RedisPools result = poolManagement.getRedisPool(); System.out.println(result.getDatabaseNum()); Jedis jedis = result.getJedis(); jedis.keys(""); jedis.set("names","testGetRedisPool"); Assert.assertEquals("testGetRedisPool",jedis.get("names")); }
Example 9
Source File: RedisClient.java From flycache with Apache License 2.0 | 5 votes |
/** * <p> * 向redis存入key和value,并释放连接资源 * </p> * <p> * 如果key已经存在 则覆盖 * </p> * * @param key * @param value * @return 成功 返回OK 失败返回 0 */ public String set(String key, String value) { Jedis jedis = null; try { jedis = jedisPool.getResource(); return jedis.set(key, value); } catch (Exception e) { e.printStackTrace(); return "0"; } finally { closeJedis(jedis); } }
Example 10
Source File: JedisSentinelPoolTest.java From cachecloud with Apache License 2.0 | 5 votes |
@Test public void returnResourceShouldResetState() { GenericObjectPoolConfig config = new GenericObjectPoolConfig(); config.setMaxTotal(1); config.setBlockWhenExhausted(false); JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels, config, 1000, "foobared", 2); Jedis jedis = pool.getResource(); Jedis jedis2 = null; try { jedis.set("hello", "jedis"); Transaction t = jedis.multi(); t.set("hello", "world"); jedis.close(); jedis2 = pool.getResource(); assertTrue(jedis == jedis2); assertEquals("jedis", jedis2.get("hello")); } catch (JedisConnectionException e) { if (jedis2 != null) { jedis2 = null; } } finally { jedis2.close(); pool.destroy(); } }
Example 11
Source File: MappingCache.java From MicroCommunity with Apache License 2.0 | 5 votes |
/** * 保存数据 * * @param mapping */ public static void setVaule(Mapping mapping) { Jedis redis = null; try { redis = getJedis(); redis.set((mapping.getDomain() + mapping.getKey() + _SUFFIX_MAPPING).getBytes(), SerializeUtil.serialize(mapping)); } finally { if (redis != null) { redis.close(); } } }
Example 12
Source File: RedisSet.java From DDMQ with Apache License 2.0 | 5 votes |
@Override public String execute(Jedis jedis) { if (expx != null) { return jedis.set(key, value, nxpx, expx, time); } else if (nxpx != null) { return jedis.set(key, value, nxpx); } else { return jedis.set(key, value); } }
Example 13
Source File: JedisTest.java From cachecloud with Apache License 2.0 | 5 votes |
@Test public void startWithUrlString() { Jedis j = new Jedis("localhost", 6380); j.auth("foobared"); j.select(2); j.set("foo", "bar"); Jedis jedis = new Jedis("redis://:foobared@localhost:6380/2"); assertEquals("PONG", jedis.ping()); assertEquals("bar", jedis.get("foo")); }
Example 14
Source File: RedisServiceImpl.java From blog_demos with Apache License 2.0 | 5 votes |
@Override public void strSet(String key, String value) { Jedis jedis = borrowJedis(); if(null!=jedis){ jedis.set(key, value); } returnJedis(jedis); }
Example 15
Source File: RedisService.java From pybbs with GNU Affero General Public License v3.0 | 5 votes |
/** * 带有过期时间的保存数据到redis,到期自动删除 * * @param key * @param value * @param expireTime 单位 秒 */ public void setString(String key, String value, int expireTime) { JedisPool instance = this.instance(); if (StringUtils.isEmpty(key) || StringUtils.isEmpty(value) || instance == null) return; Jedis jedis = instance.getResource(); SetParams params = new SetParams(); params.px(expireTime * 1000); jedis.set(key, value, params); jedis.close(); }
Example 16
Source File: JedisClientSingle.java From learning-taotaoMall with MIT License | 5 votes |
@Override public String set(String key, String value) { Jedis jedis = jedisPool.getResource(); String string = jedis.set(key, value); jedis.close(); return string; }
Example 17
Source File: RedisClient.java From star-zone with Apache License 2.0 | 5 votes |
/** * <p> * 向redis存入key和value,并释放连接资源 * </p> * <p> * 如果key已经存在 则覆盖 * </p> * * @param key * @param value * @return 成功 返回OK 失败返回 0 */ public String set(String key, String value) { Jedis jedis = null; try { jedis = jedisPool.getResource(); return jedis.set(key, value); } catch (Exception e) { e.printStackTrace(); return "0"; } finally { closeJedis(jedis); } }
Example 18
Source File: JedisTest.java From cachecloud with Apache License 2.0 | 5 votes |
@Test public void startWithUrl() throws URISyntaxException { Jedis j = new Jedis("localhost", 6380); j.auth("foobared"); j.select(2); j.set("foo", "bar"); Jedis jedis = new Jedis(new URI("redis://:foobared@localhost:6380/2")); assertEquals("PONG", jedis.ping()); assertEquals("bar", jedis.get("foo")); }
Example 19
Source File: RedisStore.java From datacollector with Apache License 2.0 | 4 votes |
public void put(String key, String value) { // Persist any new keys to Redis. Jedis jedis = pool.getResource(); jedis.set(key, value); }
Example 20
Source File: UserController.java From token-authentication-example with Apache License 2.0 | 4 votes |
@RequestMapping(value = "login", method = RequestMethod.POST) @ApiOperation("用户登录接口") public ResponseTemplate login(@RequestBody(required = false) JSONObject userInfo) { String username = userInfo.getString("username"); String password = userInfo.getString("password"); // User curentUser = new User().selectOne(new EntityWrapper<User>() // .eq("username",username) // .eq("password",password) // .eq("del_flag",0)); List<User> users = userMapper.selectList(new EntityWrapper<User>() .eq("username", username) .eq("password", password)); User currentUser = users.get(0); JSONObject result = new JSONObject(); if (currentUser != null) { Jedis jedis = new Jedis("localhost", 6379); String token = tokenGenerator.generate(username, password); jedis.set(username, token); jedis.expire(username, ConstantKit.TOKEN_EXPIRE_TIME); jedis.set(token, username); jedis.expire(token, ConstantKit.TOKEN_EXPIRE_TIME); Long currentTime = System.currentTimeMillis(); jedis.set(token + username, currentTime.toString()); //用完关闭 jedis.close(); result.put("status", "登录成功"); result.put("token", token); } else { result.put("status", "登录失败"); } return ResponseTemplate.builder() .code(200) .message("登录成功") .data(result) .build(); }