Summary of Redis partial data structure method

Source: Internet
Author: User

Package Com.practice.util;import Java.util.hashmap;import java.util.list;import java.util.map;import java.util.Set; Import Org.apache.commons.logging.log;import Org.apache.commons.logging.logfactory;import Redis.clients.jedis.jedis;import Redis.clients.jedis.jedispool;import Redis.clients.jedis.jedispoolconfig;public     Class Redisutil {private static final log log = Logfactory.getlog (Redisutil.class);        private static Jedispool jedispool;//non-tiled connection pool private static final object lock = new Object ();        private static final int default_time_out = 30000;        private static String Redisip = "192.168.77.153";        private static Integer redisport = 7000;        /** * Build Redis Tile Connection Pool * * @param IP * @param port * @return Jedispool */public static Jedispool Getjedispool () { if (Jedispool = = null) {synchronized (lock) {if (Jedispool = = null) {Jedispool                    Config config = new jedispoolconfig ();     Set connection pool initialization size and maximum capacity                                   Controls how many Jedis instances can be allocated by a pool, which is obtained by Pool.getresource (), or if the assignment is-1, it means no limit;                    If the pool already has a maxactive Jedis instance assigned to it, the pool's state is exhausted (exhausted) at this time.                                        Config.setmaxtotal (-1);                    Controls the maximum number of Jedis instances in a pool that have an idle (idle) state.                    Config.setmaxidle (1000);  Represents the maximum wait time when a Jedis instance is borrow (introduced), and if the wait time is exceeded, the jedisconnectionexception is thrown directly; Config.setmaxwaitmillis (1000                    * 30);                    Whether the validate operation is performed in advance when a Jedis instance is borrow, and if true, the resulting Jedis instance is available; Config.settestonborrow (true);                                    Write Jedispool = new Jedispool (config, Redisip, redisport,default_time_out);    }}} return jedispool; /** * Return to Connection pool * * @param pool * @param redis */public static void Returnjedisresource (Je Dis redis) {if (Redis! = null) {REdis.close ();        }}//Direct set key-value public static void Setstructure (String key,string value) {Jedispool pool = null;        Jedis Jedis = null;            try {pool = Getjedispool ();            Jedis = Pool.getresource ();        Jedis.set (key, value);        } catch (Exception e) {log.error (e);        } finally {//Return to Connection Pool Returnjedisresource (Jedis);        }} public static void Getsetstructure (String key) {Jedispool pool = null;        Jedis Jedis = null;        String value = "";            try {pool = Getjedispool ();            Jedis = Pool.getresource ();            Value = Jedis.get (key);        System.out.println (value);        } catch (Exception e) {log.error (e);        } finally {//Return to Connection Pool Returnjedisresource (Jedis);        }}//delete data by key public static void Delkey (String key) {Jedispool pool = null;        Jedis Jedis = null;  try {          Pool = Getjedispool ();            Jedis = Pool.getresource ();            Jedis.del (key);        System.out.println ("Del key Success");        } catch (Exception e) {log.error (e);        } finally {//Return to Connection Pool Returnjedisresource (Jedis); }}//mset equivalent to Jedis.set ("Key1", "value1"), Jedis.set ("Key2", "value2") public static void Msetdata (String key1,s        Tring value1,string key2,string value2) {Jedispool pool = null;        Jedis Jedis = null;            try {pool = Getjedispool ();            Jedis = Pool.getresource ();        Jedis.mset (key1,value1,key2,value2);        } catch (Exception e) {log.error (e);        } finally {//Return to Connection Pool Returnjedisresource (Jedis);        }} public static void FlushData () {Jedispool pool = null;        Jedis Jedis = null;            try {pool = Getjedispool ();            Jedis = Pool.getresource ();          Jedis.flushall ();  System.out.println ("Flushall success");        } catch (Exception e) {log.error (e);        } finally {//Return to Connection Pool Returnjedisresource (Jedis);        }}//Determine if key exists and return 1 if present, otherwise 0 public static Boolean booleanexsit (String key) {Jedispool pool = null;        Jedis Jedis = null;        Boolean exsit = false;            try {pool = Getjedispool ();            Jedis = Pool.getresource ();        Exsit = jedis.exists (key);        } catch (Exception e) {log.error (e);        } finally {//Return to Connection Pool Returnjedisresource (Jedis);    }return Exsit;        public static void AppendData (String key,string data) {Jedispool pool = null;        Jedis Jedis = null;            try {pool = Getjedispool ();            Jedis = Pool.getresource ();        Jedis.append (key, data);        } catch (Exception e) {log.error (e); } finally {//Return to Connection pool ReturnjedisresourCE (Jedis);        }}//intercept value public static void GetRange (String key) {Jedispool pool = null;        Jedis Jedis = null;            try {pool = Getjedispool ();            Jedis = Pool.getresource ();            String value = Jedis.getrange (key, 0, 1);            System.out.println (value);        System.out.println ();        } catch (Exception e) {log.error (e);        } finally {//Return to Connection Pool Returnjedisresource (Jedis); }}//list operation is used to insert one or more values into the end of the list (rightmost), and if the list does not exist, an empty list is created and the Rpush operation is performed.    An error is returned when the list exists but is not a list type.        public static void Rpush (String key) {Jedispool pool = null;        Jedis Jedis = null;            try {pool = Getjedispool ();            Jedis = Pool.getresource ();              Jedis.rpush (Key, "Hello How is You?"); Jedis.rpush (key, "Fine thanks.              I ' m have fun with Redis. ");                     Jedis.rpush (Key, "I should look in this NOSQL thing ASAP"); } catch (ExcEption e) {log.error (e);        } finally {//Return to Connection Pool Returnjedisresource (Jedis);        }}//Remove the value of the corresponding position in the list public static void Getpushvalue (String key) {Jedispool pool = null;        Jedis Jedis = null;            try {pool = Getjedispool ();            Jedis = Pool.getresource ();            The first one is key, the second is the starting position, the third is the end position, Jedis.llen gets the length-1 means get all list<string> values = Jedis.lrange (key, 0,-1);            System.out.println (values);        } catch (Exception e) {log.error (e);        } finally {//Return to Connection Pool Returnjedisresource (Jedis);        }} public static void Set (String key) {Jedispool pool = null;        Jedis Jedis = null;            try {pool = Getjedispool ();            Jedis = Pool.getresource ();              Jedis.sadd ("MySet", "1");              Jedis.sadd ("MySet", "2");              Jedis.sadd ("MySet", "3");           Jedis.sadd ("MySet", "4");   set<string> setvalues = jedis.smembers ("MySet");        System.out.println (setvalues);        } catch (Exception e) {log.error (e);        } finally {//Return to Connection Pool Returnjedisresource (Jedis);        }} public static void Srem (String key) {Jedispool pool = null;        Jedis Jedis = null;            try {pool = Getjedispool ();            Jedis = Pool.getresource ();            Jedis.srem (Key, "4");             set<string> setvalues = jedis.smembers ("MySet");        System.out.println (setvalues);        } catch (Exception e) {log.error (e);        } finally {//Return to Connection Pool Returnjedisresource (Jedis);        }} public static void Hmset () {Jedispool pool = null;        Jedis Jedis = null;            try {pool = Getjedispool ();            Jedis = Pool.getresource ();             map<string, string> pairs = new hashmap<string, string> (); Pairs.put ("NamE "," Akshi ");              Pairs.put ("Age", "2");              Pairs.put ("Sex", "Female");              Jedis.hmset ("Kid", pairs);            list<string> name = Jedis.hmget ("Kid", "name");        SYSTEM.OUT.PRINTLN (name);        } catch (Exception e) {log.error (e);        } finally {//Return to Connection Pool Returnjedisresource (Jedis);        }} public static void increment () {Jedispool pool = null;        Jedis Jedis = null;            try {pool = Getjedispool ();            Jedis = Pool.getresource ();              Jedis.hset ("Hashs", "Entrykey", "1");              Jedis.hset ("Hashs", "EntryKey1", "entryValue1");              Jedis.hset ("Hashs", "EntryKey2", "entryValue2");            Determine if a value exists jedis.hexists ("Hashs", "Entrykey");          System.out.println (Jedis.hincrby ("Hashs", "Entrykey", 1));        } catch (Exception e) {log.error (e); } finally {//Return to Connection pool RETURNJEDISRESOURCE (Jedis); }    }

  

Redis is a common NoSQL memory database in engineering development, and simply consolidates its various data types and usages ~

Summary of Redis partial data structure method

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.