Using Java to operate Redis requires jedis-2.1.0.jar, download address: jedis-2.1.0.jar
If you need to use Redis connection pool, you also need commons-pool-1.5.4.jar. Download address: commons-pool-1.5.4.jar
package com.test;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import org.junit.Before;import org.junit.Test;import redis.clients.jedis.Jedis;public class TestRedis { private Jedis jedis; @Before public void setup() { //Connect the redis server, 192.168.0.100:6379 jedis = new Jedis("192.168.0.100", 6379); //Permission authentication jedis.auth("admin"); } /** * redis storage string*/ @Test public void testString() { //----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //Delete a key System.out.println(jedis.get("name")); //Set multiple key-value pairs jedis.mset("name","liuling","age","23","qq","476777XXX"); jedis.incr("age"); //Please add 1 System.out.println(jedis.get("name") + "-" + jedis.get("age") + "-" + jedis.get("qq")); } /** * redis operation Map */ @Test public void testMap() { //---------------- Map<String, String> map = new HashMap<String, String>(); map.put("name", "xinxin"); map.put("age", "22"); map.put("qq", "123456"); jedis.hmset("user",map); //Take out the name in user and execute the result: [minxr]-->Note that the result is a generic List //The first parameter is the key stored in the map object in redis, followed by the key of the object placed in the map, and the key behind the key can be multiple, which is a variable parameter List<String> rsmap = jedis.hmget("user", "name", "age", "qq"); System.out.println(rsmap); //Delete a key value in the map jedis.hdel("user","age"); System.out.println(jedis.hmget("user", "age")); //Because it is deleted, the return is null System.out.println(jedis.hlen("user")); //Return the number of values stored in the key with the key of the user 2 System.out.println(jedis.exists("user")); //Return true if there is a record with the user. System.out.println(jedis.hkeys("user")); //Return all keys in the map object System.out.println(jedis.hvals("user"));//Return all values in the map object Iterator<String> iter=jedis.hkeys("user").iterator(); while (iter.hasNext()){ String key = iter.next(); System.out.println(key+":"+jedis.hmget("user",key)); } } /** * Jedis Operation List */ @Test public void testList(){ //Remove all contents before starting jedis.del("java framework"); System.out.println(jedis.lrange("java framework",0,-1)); //First store three pieces of data in the key java framework jedis.lpush("java framework","spring"); jedis.lpush("java framework","struts"); jedis.lpush("java framework","hibernate"); //Fetch all data jedis.lrange and take out the range, //The first is the key, the second is the start position, and the third is the end position, and jedis.llen gets the length -1 to indicate that all System.out.println(jedis.lrange("java framework",0,-1)); jedis.del("java framework"); jedis.rpush("java framework","spring"); jedis.rpush("java framework","struts"); jedis.rpush("java framework","hibernate"); System.out.println(jedis.lrange("java framework",0,-1)); } /** * jedis operation Set */ @Test public void testSet(){ //Add jedis.sadd("user","liuling"); jedis.sadd("user","xinxin"); jedis.sadd("user","ling"); jedis.sadd("user","zhangxinxin"); jedis.sadd("user","who"); //Remove noname jedis.srem("user","who"); System.out.println(jedis.smembers("user")); //Get all added value System.out.println(jedis.sismember("user", "who")); //Judge whether who is an element of the user set System.out.println(jedis.srandmember("user")); System.out.println(jedis.scrad("user"));//Return the number of elements in the collection} @Test public void test() throws InterruptedException { //jedis Sort//Note that rpush and lpush here are List operations. is a bidirectional linked list (but from the perspective of performance) jedis.del("a");//Clear the data first, then add the data for testing jedis.rpush("a", "1"); jedis.lpush("a","6"); jedis.lpush("a","3"); jedis.lpush("a","9"); System.out.println(jedis.lrange("a",0,-1));// [9, 3, 6, 1] System.out.println(jedis.sort("a")); //[1, 3, 6, 9] //Input the result after sorting System.out.println(jedis.lrange("a",0,-1)); } @Test public void testRedisPool() { RedisUtil.getJedis().set("newname", "Chinese test"); System.out.println(RedisUtil.getJedis().get("newname")); }} Redis connection pool:
package com.test;import redis.clients.jedis.Jedis;import redis.clients.jedis.JedisPool;import redis.clients.jedis.JedisPoolConfig;public final class RedisUtil { //Redis server IP private static String ADDR = "192.168.0.100"; //Redis port number private static int PORT = 6379; //Access password private static String AUTH = "admin"; //The maximum number of available connection instances, the default value is 8; //If the value is -1, it means no limit; if the pool has already allocated maxActive Jedis instances, the pool's status at this time is exhausted (exhaused). private static int MAX_ACTIVE = 1024; //Contacts how many jedis instances with state idle (idle) in a pool, and the default value is also 8. private static int MAX_IDLE = 200; //The maximum time to wait for available connections, in milliseconds, the default value is -1, indicating that the timeout will never be reached. If the waiting time is exceeded, a JedisConnectionException will be directly thrown; private static int MAX_WAIT = 10000; private static int TIMEOUT = 10000; //When borrowing a jedis instance, whether to perform validate operations in advance; if true, the obtained jedis instances are all available; private static boolean TEST_ON_BORROW = true; private static JedisPool jedisPool = null; /** * Initialize the Redis connection pool*/ static { try { JedisPoolConfig config = new JedisPoolConfig(); config.setMaxActive(MAX_ACTIVE); config.setMaxIdle(MAX_IDLE); config.setMaxWait(MAX_WAIT); config.setTestOnBorrow(TEST_ON_BORROW); jedisPool = new JedisPool(config, ADDR, PORT, TIMEOUT, AUTH); } catch (Exception e) { e.printStackTrace(); } } /** * Get Jedis instance* @return */ public synchronized static Jedis getJedis() { try { if (jedisPool != null) { Jedis resource = jedisPool.getResource(); return resource; } else { return null; } } catch (Exception e) { e.printStackTrace(); return null; } } /** * Release the jedis resource* @param jedis */ public static void returnResource(final Jedis jedis) { if (jedis != null) { jedisPool.returnResource(jedis); } }}The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.