private static void useMSetNX (JRedis jredis) { // Here we are using a mixed set of value types Map<String, byte[]> kvMap = new HashMap<String, byte[]>(); kvMap.put("foo", "bar".getBytes()); kvMap.put("cat", "meow".getBytes()); kvMap.put("dog", "woof".getBytes()); kvMap.put("bird", "whale fail".getBytes()); kvMap.put("pi", String.valueOf(3.141592653589793).getBytes()); try { jredis.flushdb(); jredis.set("bird", "tweet"); // <= force an error on msetnx boolean stat = jredis.msetnx(kvMap); if(!stat) { System.out.format("Couldn't msetnx - one of these already exists: %s\n", kvMap.keySet()); } // and now we can find out which one existed: for(String key : kvMap.keySet()){ if(jredis.exists(key)){ System.out.format("key '%s' [value: %s] already existed!\n", key, new String(jredis.get(key))); } } } catch (RedisException e) { e.printStackTrace(); } }
/** * In a tight loop, we execute a few select * commands that touch the various permutations of * request complexity, and response type, so that * we can pinpoint the bottlenecks and the general * runtime characteristics of the JRedic provider. * @throws RedisException */ public void run () throws RedisException { Log.log("***** JProfileTestCase ****"); // jredis.auth("jredis").ping().flushall(); int iter = 100000; String key = "foostring"; String cntrkey = "foocntr"; String listkey = "foolist"; String setkey = "fooset"; byte[] data = "meow".getBytes(); long start = System.currentTimeMillis(); for(Long j=0L; j<iter; j++) { jredis.incr(cntrkey); jredis.set(key, data); jredis.sadd(setkey, data); jredis.rpush(listkey, data); } long delta = System.currentTimeMillis() - start; float rate = ((float)iter * 1000) / delta; System.out.format("%d iterations | %d msec | %8.2f /sec \n", iter, delta, rate); } }
@Override protected void work() { for(int i=0; i<reqCnt; i++) { try { jredis.llen (key); } catch (RedisException e) { e.printStackTrace(); } } } };
private void run(String password) { try { ConnectionSpec spec = DefaultConnectionSpec.newSpec().setCredentials(password); JRedis jredis = new JRedisClient(spec); jredis.ping(); if(!jredis.exists(bkey)) { jredis.set(bkey, "Hello Again!"); System.out.format("Hello! You should run me again!\n"); } else { String msg = toStr ( jredis.get(bkey) ); System.out.format("%s\n", msg); } jredis.quit(); } catch (RedisException e){ if (e.getCommand()==Command.PING){ System.out.format("I'll need that password! Try again with password as command line arg for this program.\n"); } } catch (ProviderException e){ System.out.format("Oh no, an 'un-documented feature': %s\nKindly report it.", e.getMessage()); } catch (ClientRuntimeException e){ System.out.format("%s\n", e.getMessage()); } } }
jredis.flushdb(); jredis.zadd(zset, rand.nextDouble(), getRandomAsciiString(rand, 8)); List<ZSetEntry> subset = jredis.zrangeSubset(zset, 0, 10); List<ZSetEntry> revsubset = jredis.zrevrangeSubset(zset, 0, 10);
pipeline.incr(cntrKey); long cntr = toLong (pipeline.sync().get(cntrKey)); pipeline.set("random:"+i, "value:" + rand.nextInt()); String randomVal = toStr (pipeline.sync().get("random:"+999)); System.out.format ("end using sync() = %d msec\n", System.currentTimeMillis() - start); pipeline.sync().quit();
@Override protected void prep() { for(int i=0; i<reqCnt; i++) { try { jredis.sadd (key, i); } catch (RedisException e) { e.printStackTrace(); } } } @Override
private static void useMSet (JRedis jredis) { // BulkSetMapping provides a set of static methods to create new specific // flavors of KeyValueSet<T> for the JRedis.mset(..) method. // use this when your values are all of the same type. // Here we are using String values KeyValueSet.Strings kvSet = BulkSetMapping.newStringKVSet(); kvSet .add("foo", "woof") .add("bar", "meow") .add("paz", "the salt") .add("x?", "yz!"); try { jredis.flushdb(); jredis.mset(kvSet); } catch (RedisException e) { e.printStackTrace(); } }
@Override protected void work() { for(int i=0; i<reqCnt; i++) { try { jredis.incr (key); } catch (RedisException e) { e.printStackTrace(); } } } };
@Override protected void prep() { try { for(int i=0; i<reqCnt; i++){ jredis.set(key, data); } } catch (RedisException e) { e.printStackTrace(); } } @Override
/** * Using the synchronous interface */ public static void usingSyncClient () { ConnectionSpec spec = DefaultConnectionSpec.newSpec() .setCredentials("jredis".getBytes()) .setDatabase(10); JRedis jredis = new JRedisClient(spec); System.out.println ("** using JRedis **"); useZRangeSubset (jredis); jredis.quit(); }
private void cleandb(JRedis jredis) throws RedisException { jredis.flushdb(); } }
@Override protected void work() { try { for(int i=0; i<reqCnt; i++){ jredis.get("fixedbytes_"+id); } } catch (RedisException e) { e.printStackTrace(); } } };
@Override protected void work() { for(int i=0; i<reqCnt; i++){ try { jredis.ping(); } catch (RedisException e) { e.printStackTrace(); } } } };