Add dependency to pom.xml
1 2 3 4 5 |
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.5.2</version> </dependency> |
Try java code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
// Connect to local Redis cluster Jedis jedis = new Jedis("localhost"); // Work with key jedis.set("myKey", "myValue"); String myValue= jedis.get("myKey"); jedis.del("myKey"); // Work with queue jedis.lpush("myQueue", "item1"); jedis.lpush("myQueue", "item2"); String item1 = jedis.rpop("myQueue"); boolean queueExists= jedis.exists("myQueue"); String item2 = jedis.rpop("myQueue"); String nullItem = jedis.rpop("myQueue"); boolean queueExists= jedis.exists("myQueue"); // Set if not exists Long isBreakfast= jedis.setnx("meal", "breakfast"); Long isLunch = jedis.setnx("meal", "lunch"); Long isTea = jedis.setnx("meal", "5 o'clock tea"); String firstMeal = jedis.get("meal"); jedis.del("meal"); // Expiration // 2 minutes (120 sec) of showers jedis.setex("weather", 120, "rain"); // Change to 5 minutes of good weather jedis.expire("weather", 300); |