Using Jedis to operate Redis

Source: Internet
Author: User

Redis is an open source Key-value data cache, similar to memcached.

Redis has multiple types of value, including string (string), list (linked list), set (set), Zset (sorted set-ordered set), and hash (hash type).

Jedis is the official Redis-Preferred Java client development package.

//to connect Redis, the default port for Redis is 6379Jedis Jedis=NewJedis ("localhost", 6379); //Verify the password if no password is set this code is omittedJedis.auth ("Password"); Jedis.connect ();//ConnectionJedis.disconnect ();//Disconnect ConnectionSet<String> keys = Jedis.keys ("*");//list all the keysSet<String> keys = Jedis.keys ("key");//find a specific key//removes the given key or keys and ignores the command if the key does not exist.Jedis.del ("Key1"); Jedis.del ("Key1", "Key2", "Key3", "Key4", "Key5");//to remove the lifetime of a given key (setting this key never expires)Jedis.persist ("Key1"); //checks if a given key existsJedis.exists ("Key1"); //Rename key to Newkey, return an error when key and Newkey are the same or key does not existJedis.rename ("Key1", "Key2");//returns the type of value stored by key. //None (key does not exist), string (string), List (list), set (set), Zset (ordered set), hash (hash table)Jedis.type ("Key1");//set key lifetime and it will be automatically deleted when key expires. Jedis.expire ("Key1", 5);//expires in 5 seconds//The string value is associated to key. Jedis.set ("Key1", "value1"); //associates a value of values to a key and sets the lifetime of the key to seconds (seconds). Jedis.setex ("foo", 5, "haha"); //Clear all the keysJedis.flushall ();//returns the number of keysjedis.dbsize ();//The value of the field field in the hash table key is set to values. Jedis.hset ("Key1", "field1", "Field1-value"); Jedis.hset ("Key1", "Field2", "Field2-value"); Map Map=NewHashMap (); Map.put ("Field1", "Field1-value"); Map.put ("Field2", "Field2-value"); Jedis.hmset ("Key1", map); //returns the value of the given field field in the hash table keyJedis.hget ("Key1", "field1");//returns the value (multiple) of a given field field in a hash table keyList List = Jedis.hmget ("Key1", "field1", "Field2");  for(intI=0;i<list.size (); i++) {System.out.println (List.get (i));}//returns all fields and values in the hash table keymap<string,string> map = Jedis.hgetall ("Key1");  for(Map.entry entry:map.entrySet ()) {System.out.print (Entry.getkey () )+ ":" + entry.getvalue () + "\ T"); } //Delete one or more specified fields in the hash table keyJedis.hdel ("Key1", "field1"); Jedis.hdel ("Key1", "field1", "Field2");//View the hash table key for the existence of a given domain field. Jedis.hexists ("Key1", "field1");//returns all the fields in the hash table keyJedis.hkeys ("Key1");//returns all values in the hash table keyJedis.hvals ("Key1");//inserts the value values into the table header of the list key. Jedis.lpush ("Key1", "value1-0"); Jedis.lpush ("Key1", "Value1-1"); Jedis.lpush ("Key1", "Value1-2"); //returns the element in the specified interval in the list key, with the interval specified by the offset start and stop.//Subscript (Index) parameter start and stop starting from 0;//a negative subscript represents a starting from the back (1 indicates the last element of the list, 2 represents the penultimate element of the list, and so on)List List = Jedis.lrange ("Key1", 0,-1);//stop subscript is also within the value range (closed interval) for(intI=0;i<list.size (); i++) {System.out.println (List.get (i));}//returns the length of the list key. Jedis.llen ("Key1")//Add the member element to the collection key. Jedis.sadd ("Key1", "Value0"); Jedis.sadd ("Key1", "value1"); //removes the member element from the collection. Jedis.srem ("Key1", "value1"); //returns all members in the collection key. Set set = Jedis.smembers ("Key1"); //determines whether an element is a member of a collection keyJedis.sismember ("Key1", "value2")); //returns the number of elements of the collection keyJedis.scard ("Key1"); //returns all members of a collection that are the intersection of all given collectionsJedis.sinter ("Key1", "Key2") //returns all members of a collection, which is the set of all the given collectionsJedis.sunion ("Key1", "Key2")//returns all members of a collection, which is the difference set of all given collectionsJedis.sdiff ("Key1", "Key2");

Using Jedis to operate Redis

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.