Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to operate Redis in JAVA Program

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

JAVA program how to operate Redis, for this problem, this article details the corresponding analysis and solutions, hoping to help more small partners who want to solve this problem find a simpler and easier way.

package redisdemo.redistest; import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map; import redis.clients.jedis.Jedis;/** * Redis Basic Operating Methods * redis.clients jedis 2.7.3 * @QQ : 525354786 * @author gaojingsong * */public class TestRedis { private static Jedis jedis; public static void main(String[] args) throws Exception { connect(); testString(); testMap(); // testList(); testSet(); // testSort();} public static void connect() { //Connect to redis server, 192.168.0.100:6379 jedis = new Jedis("192.168.1.111", 6379); //Permission authentication //jedis.auth("admin"); } /** * redis storage string */ public static void testString() { //-----Add Data---------- jedis.set("name","Zhang San");//put value--> Zhang San into key-->name System.out.println(jedis.get("name"));//Execution result: Zhang San jedis.append("name", "Li Si"); //splice System.out.println(jedis.get("name")); jedis.del("name"); //delete a key System.out.println(jedis.get("name")); //Set multiple key-value pairs jedis.mset("name","Zhang Side","age","23","qq","11111"); jedis.incr("age"); //add 1 System.out.println(jedis.get("name") + "-" + jedis.get("age") + "-" + jedis.get("qq")); } /** * redis Operation Map */ public static void testMap() { //-----Add Data---------- Map map = new HashMap(); map.put("name", "Li Si"); map.put("age", "22"); map.put("qq", "123456"); jedis.hmset("user",map); //Remove name from user, execute result:[minxr]--> Note that the result is a generic List //The first parameter is the key of the map object stored in redis, followed by the key of the object placed in the map. The following key can be followed by multiple keys, which are variable parameters. List rsmap = jedis.hmget("user", "name", "age", "qq"); System.out.println(rsmap); //delete a key from the map jedis.hdel("user","age"); System.out.println(jedis.hmget("user", "age")); //null returned because deleted System.out.println(jedis.hlen("user")); //Returns the number of values stored in the key with key user2 System.out.println(jedis.exists("user"));//returns true if there is a record with key user System.out.println(jedis.hkeys("user"));//Returns all keys in the map object System.out.println(jedis.hvals("user"));//Returns all values in the map object Iterator iter=jedis.hkeys("user").iterator(); while (iter.hasNext()){ String key = iter.next(); System.out.println(key+":"+jedis.hmget("user",key)); } } /** * jedis Operation List */ public static void testList(){ //Remove all content before starting jedis.del("program"); System.out.println(jedis.lrange("program",0,-1)); //store three pieces of data in the key program first jedis.lpush("program","JAVA"); jedis.lpush("program","PHP"); jedis.lpush("program","ASP"); //extract all data again jedis.lrange is extract by range //The first is the key, the second is the start position, the third is the end position, jedis.llen Get length-1 means get all System.out.println(jedis.lrange("program",0,-1)); jedis.del("program"); jedis.rpush("program","spring"); jedis.rpush("program","struts"); jedis.rpush("program","hibernate"); System.out.println(jedis.lrange("program",0,-1)); } /** * jedis Operation Set */ public static void testSet(){ //add jedis.sadd("kuser",new String[]{"2","4"}); System.out.println(jedis.smembers("kuser"));//Get all added values } /** * jedis Operation Sort */ public static void testSort() throws InterruptedException { //jedis sort //Note that rpush and lpush here are List operations. is a doubly linked list (but in terms of performance) jedis.del("st");//Clear data first, then add data for testing jedis.rpush("st", "1"); jedis.lpush("st","6"); jedis.lpush("st","3"); jedis.lpush("st","9"); System.out.println(jedis.lrange("st",0,-1));// [9, 3, 6, 1] System.out.println(jedis.sort("st")); //[1, 3, 6, 9] //Enter the sorted result System.out.println(jedis.lrange("st",0,-1)); } }

About JAVA program how to operate Redis questions to share the answer here, I hope the above content can have some help for everyone, if you still have a lot of doubts not solved, you can pay attention to the industry information channel to learn more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report