Java Code Examples for redis.clients.jedis.ShardedJedis#set()
The following examples show how to use
redis.clients.jedis.ShardedJedis#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: RedisExample.java From java-platform with Apache License 2.0 | 6 votes |
public void testShardNormal() {// 13.619秒 JedisShardInfo jedis = new JedisShardInfo("120.25.241.144", 6379); jedis.setPassword("b840fc02d52404542994"); List<JedisShardInfo> shards = Arrays.asList(jedis); ShardedJedis sharding = new ShardedJedis(shards); long start = System.currentTimeMillis(); for (int i = 0; i < 1000; i++) { sharding.set("n" + i, "n" + i); System.out.println(i); } long end = System.currentTimeMillis(); System.out.println("共花费:" + (end - start) / 1000.0 + "秒"); sharding.disconnect(); try { Closeables.close(sharding, true); } catch (IOException e) { e.printStackTrace(); } }
Example 2
Source File: JedisApiTest.java From easyooo-framework with Apache License 2.0 | 6 votes |
/** * 单机模拟分库 * 耗时:1.244 seconds */ @Test @Ignore public void testShardNormal(){ List<JedisShardInfo> shards = Arrays.asList( new JedisShardInfo("localhost",6379), new JedisShardInfo("localhost",6379)); ShardedJedis sharding = new ShardedJedis(shards); long start = System.currentTimeMillis(); for (int i = 0; i < COUNTER; i++) { String result = sharding.set("sn" + i, "n" + i); } long end = System.currentTimeMillis(); System.out.println("Simple@Sharing SET: " + ((end - start)/1000.0) + " seconds"); sharding.disconnect(); sharding.close(); }
Example 3
Source File: ShardedJedisPoolTest.java From cachecloud with Apache License 2.0 | 6 votes |
@Test public void checkResourceIsCloseable() throws URISyntaxException { GenericObjectPoolConfig config = new GenericObjectPoolConfig(); config.setMaxTotal(1); config.setBlockWhenExhausted(false); List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(); shards.add(new JedisShardInfo(new URI("redis://:foobared@localhost:6380"))); shards.add(new JedisShardInfo(new URI("redis://:foobared@localhost:6379"))); ShardedJedisPool pool = new ShardedJedisPool(config, shards); ShardedJedis jedis = pool.getResource(); try { jedis.set("hello", "jedis"); } finally { jedis.close(); } ShardedJedis jedis2 = pool.getResource(); try { assertEquals(jedis, jedis2); } finally { jedis2.close(); } }
Example 4
Source File: RedisShardedPoolUtil.java From mmall20180107 with Apache License 2.0 | 6 votes |
public static String set(String key,String value){ ShardedJedis jedis = null; String result = null; try { jedis = RedisShardedPool.getJedis(); result = jedis.set(key,value); } catch (Exception e) { log.error("set key:{} value:{} error",key,value,e); RedisShardedPool.returnBrokenResource(jedis); e.printStackTrace(); } RedisShardedPool.returnResource(jedis); return result; }
Example 5
Source File: JedisCacheService.java From howsun-javaee-framework with Apache License 2.0 | 6 votes |
private String setStringValue(String key, String value, int seconds) { ShardedJedis jedis = null; try { jedis = cacheFactory.getResource(); if(seconds > -1){ return jedis.setex(key, seconds, value); }else{ return jedis.set(key, value); } } catch (Exception e) { log.error("获取缓存失败:", e); } finally{ if(jedis != null){ try {cacheFactory.returnResource(jedis);} catch (Exception e2) { log.error("不能释放缓存操纵对象:",e2); } } } return null; }
Example 6
Source File: RedisCacheClient.java From AsuraFramework with Apache License 2.0 | 6 votes |
/** * 从 Redis 2.6.12 版本开始, SET 命令的行为可以通过一系列参数来修改: * EX second :设置键的过期时间为 second 秒。 SET key value EX second 效果等同于 SETEX key second value 。 * PX millisecond :设置键的过期时间为 millisecond 毫秒。 SET key value PX millisecond 效果等同于 PSETEX key millisecond value 。 * NX :只在键不存在时,才对键进行设置操作。 SET key value NX 效果等同于 SETNX key value 。 * XX :只在键已经存在时,才对键进行设置操作。 * * @param key * @param value * @param milliseconds * @return */ public boolean setnx(String key, String value, long milliseconds) { ShardedJedis redis = null; try { redis = pool.getResource(); key = getKeyAll(key); //nx|xx ex|px seconds|milliseconds String result = redis.set(key, value, "nx", "px", milliseconds); return "OK".equalsIgnoreCase(result); } catch (RuntimeException e) { logger.error("redis setex(String key, int seconds, String value):", e); return false; } finally { redis.close(); } }
Example 7
Source File: ShardedJedisTest.java From cachecloud with Apache License 2.0 | 6 votes |
@Test public void checkCloseable() { List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(); shards.add(new JedisShardInfo(redis1.getHost(), redis1.getPort())); shards.add(new JedisShardInfo(redis2.getHost(), redis2.getPort())); shards.get(0).setPassword("foobared"); shards.get(1).setPassword("foobared"); ShardedJedis jedisShard = new ShardedJedis(shards); try { jedisShard.set("shard_closeable", "true"); } finally { jedisShard.close(); } for (Jedis jedis : jedisShard.getAllShards()) { assertTrue(!jedis.isConnected()); } }
Example 8
Source File: RedisExample.java From java-platform with Apache License 2.0 | 5 votes |
public void testShardSimplePool() {// 12.642秒 JedisShardInfo jedis = new JedisShardInfo("120.25.241.144", 6379); jedis.setPassword("b840fc02d52404542994"); List<JedisShardInfo> shards = Arrays.asList(jedis); ShardedJedisPool pool = new ShardedJedisPool(new JedisPoolConfig(), shards); ShardedJedis sharding = pool.getResource(); long start = System.currentTimeMillis(); for (int i = 0; i < 1000; i++) { sharding.set("n" + i, "n" + i); System.out.println(i); } long end = System.currentTimeMillis(); System.out.println("共花费:" + (end - start) / 1000.0 + "秒"); sharding.disconnect(); pool.destroy(); try { Closeables.close(sharding, true); Closeables.close(pool, true); } catch (IOException e) { e.printStackTrace(); } }
Example 9
Source File: MarsRedisTemplate.java From Mars-Java with MIT License | 5 votes |
/** * 设置redis值 * * @param key * @param value * @return */ public boolean set(byte[] key, byte[] value) { ShardedJedis jedis = null; try { jedis = getShardedJedis(); jedis.set(key, value); return true; } catch (Exception e) { logger.error("set redis value error:" + key + "@" + value, e); } finally { recycleJedis(jedis); } return false; }
Example 10
Source File: ShardedJedisPoolTest.java From cachecloud with Apache License 2.0 | 5 votes |
@Test public void returnResourceShouldResetState() throws URISyntaxException { GenericObjectPoolConfig config = new GenericObjectPoolConfig(); config.setMaxTotal(1); config.setBlockWhenExhausted(false); List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(); shards.add(new JedisShardInfo(new URI("redis://:foobared@localhost:6380"))); shards.add(new JedisShardInfo(new URI("redis://:foobared@localhost:6379"))); ShardedJedisPool pool = new ShardedJedisPool(config, shards); ShardedJedis jedis = pool.getResource(); jedis.set("pipelined", String.valueOf(0)); jedis.set("pipelined2", String.valueOf(0)); ShardedJedisPipeline pipeline = jedis.pipelined(); pipeline.incr("pipelined"); pipeline.incr("pipelined2"); jedis.resetState(); pipeline = jedis.pipelined(); pipeline.incr("pipelined"); pipeline.incr("pipelined2"); List<Object> results = pipeline.syncAndReturnAll(); assertEquals(2, results.size()); jedis.close(); pool.destroy(); }
Example 11
Source File: ShardedJedisPoolTest.java From cachecloud with Apache License 2.0 | 5 votes |
@Test public void checkJedisIsReusedWhenReturned() { ShardedJedisPool pool = new ShardedJedisPool(new GenericObjectPoolConfig(), shards); ShardedJedis jedis = pool.getResource(); jedis.set("foo", "0"); jedis.close(); jedis = pool.getResource(); jedis.incr("foo"); jedis.close(); pool.destroy(); }
Example 12
Source File: ShardedJedisPoolTest.java From cachecloud with Apache License 2.0 | 5 votes |
@Test public void checkConnectionWithDefaultPort() { ShardedJedisPool pool = new ShardedJedisPool(new GenericObjectPoolConfig(), shards); ShardedJedis jedis = pool.getResource(); jedis.set("foo", "bar"); assertEquals("bar", jedis.get("foo")); jedis.close(); pool.destroy(); }
Example 13
Source File: ShardedJedisPoolTest.java From cachecloud with Apache License 2.0 | 5 votes |
@Test public void checkCloseableConnections() throws Exception { ShardedJedisPool pool = new ShardedJedisPool(new GenericObjectPoolConfig(), shards); ShardedJedis jedis = pool.getResource(); jedis.set("foo", "bar"); assertEquals("bar", jedis.get("foo")); jedis.close(); pool.close(); assertTrue(pool.isClosed()); }
Example 14
Source File: RedisClient.java From dubbo-plus with Apache License 2.0 | 5 votes |
public void cacheValue(byte[] key,byte[] bytes,int expireSecond){ ShardedJedis jedis = JEDIS_POOL.getResource(); try{ if(expireSecond>0){ jedis.setex(key,expireSecond,bytes); }else{ jedis.set(key,bytes); } }finally { JEDIS_POOL.returnResource(jedis); } }
Example 15
Source File: RedisServiceImpl.java From AsuraFramework with Apache License 2.0 | 5 votes |
@Override public boolean set(final String key, final String value, final int expire) { checkParameters(key, value); final ShardedJedis jedis = getJedis(); final String result = jedis.set(key, value); jedis.expire(key, expire); returnResource(jedis); return SUCCESS.equalsIgnoreCase(result); }
Example 16
Source File: RedisShardedPoolUtil.java From mmall-kay-Java with Apache License 2.0 | 5 votes |
public static String set(String key,String value){ ShardedJedis jedis = null; String result=null; try { jedis = RedisShardedPool.getJedis(); result = jedis.set(key, value); } catch (Exception e) { log.error("set key:{} value:{} error",key,value,e); RedisShardedPool.returnBrokenResource(jedis); return result; } RedisShardedPool.returnResource(jedis); return result; }
Example 17
Source File: JedisCacheService.java From howsun-javaee-framework with Apache License 2.0 | 5 votes |
private String setObject(String key, Object value, int seconds) { ShardedJedis jedis = null; if(value instanceof String){ return setStringValue(key, value.toString(), seconds); } try { jedis = cacheFactory.getResource(); byte[] keyBytes = key.getBytes("UTF-8"); byte result[] = serializer.serialize(value); if(seconds > -1){ return jedis.setex(keyBytes, seconds, result); }else{ return jedis.set(keyBytes, result); } } catch (Exception e) { log.error("获取缓存失败:", e); } finally{ if(jedis != null){ try {cacheFactory.returnResource(jedis);} catch (Exception e2) { log.error("不能释放缓存操纵对象:",e2); } } } return null; }
Example 18
Source File: JedisDemo.java From JavaTutorial with Apache License 2.0 | 5 votes |
/** * 多机分布式+连接池。 */ private static void shardPool() { // 生成多机连接信息列表 List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(); shards.add( new JedisShardInfo("127.0.0.1", 6379) ); shards.add( new JedisShardInfo("192.168.56.102", 6379) ); // 生成连接池配置信息 JedisPoolConfig config = new JedisPoolConfig(); config.setMaxIdle(10); config.setMaxTotal(30); config.setMaxWaitMillis(3*1000); // 在应用初始化的时候生成连接池 ShardedJedisPool pool = new ShardedJedisPool(config, shards); // 在业务操作时,从连接池获取连接 ShardedJedis client = pool.getResource(); try { // 执行指令 String result = client.set("key-string", "Hello, Redis!"); System.out.println( String.format("set指令执行结果:%s", result) ); String value = client.get("key-string"); System.out.println( String.format("get指令执行结果:%s", value) ); } catch (Exception e) { // TODO: handle exception } finally { // 业务操作完成,将连接返回给连接池 if (null != client) { pool.returnResource(client); } } // end of try block // 应用关闭时,释放连接池资源 pool.destroy(); }
Example 19
Source File: MarsRedisLock.java From Mars-Java with MIT License | 5 votes |
/** * 加锁,使用你自己创建的jedis对象 * * @param key 键 * @param value 值 * @param shardedJedis 自己创建的jedis对象 * @return */ public boolean lock(String key, String value, ShardedJedis shardedJedis) { try { if (shardedJedis == null) { return false; } int count = 0; SetParams params = SetParams.setParams().nx().px(5000); String result = shardedJedis.set(key, value, params); while (result == null || !result.toUpperCase().equals("OK")) { /* 如果设置失败,代表这个key已经存在了,也就说明锁被占用了,则进入等待 */ Thread.sleep(500); if (count >= 19) { /* 10秒后还没有获取锁,则停止等待 */ return false; } result = shardedJedis.set(key, value, params); count++; } return true; } catch (Exception e) { logger.error("获取redis锁发生异常", e); return false; } finally { marsRedisTemplate.recycleJedis(shardedJedis); } }
Example 20
Source File: ShardedJedisTest.java From cachecloud with Apache License 2.0 | 4 votes |
/** * Test for "Issue - BinaryShardedJedis.disconnect() may occur memory leak". You can find more * detailed information at https://github.com/xetorthio/jedis/issues/808 * @throws InterruptedException */ @Test public void testAvoidLeaksUponDisconnect() throws InterruptedException { List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(2); // 6379 JedisShardInfo shard1 = new JedisShardInfo(redis1.getHost(), redis1.getPort()); shard1.setPassword("foobared"); shards.add(shard1); // 6380 JedisShardInfo shard2 = new JedisShardInfo(redis2.getHost(), redis2.getPort()); shard2.setPassword("foobared"); shards.add(shard2); @SuppressWarnings("resource") ShardedJedis shardedJedis = new ShardedJedis(shards); // establish the connection for two redis servers shardedJedis.set("a", "bar"); JedisShardInfo ak = shardedJedis.getShardInfo("a"); assertEquals(shard2, ak); shardedJedis.set("b", "bar1"); JedisShardInfo bk = shardedJedis.getShardInfo("b"); assertEquals(shard1, bk); // We set a name to the instance so it's easy to find it Iterator<Jedis> it = shardedJedis.getAllShards().iterator(); Jedis deadClient = it.next(); deadClient.clientSetname("DEAD"); ClientKillerUtil.killClient(deadClient, "DEAD"); assertEquals(true, deadClient.isConnected()); assertEquals(false, deadClient.getClient().getSocket().isClosed()); assertEquals(false, deadClient.getClient().isBroken()); // normal - not found shardedJedis.disconnect(); assertEquals(false, deadClient.isConnected()); assertEquals(true, deadClient.getClient().getSocket().isClosed()); assertEquals(true, deadClient.getClient().isBroken()); Jedis jedis2 = it.next(); assertEquals(false, jedis2.isConnected()); assertEquals(true, jedis2.getClient().getSocket().isClosed()); assertEquals(false, jedis2.getClient().isBroken()); }