The hash data type of Redis and its application scenario---shopping cart

Source: Internet
Author: User

1.HASH Data Structure

The hashes type in Redis can be seen as a map container with string key and string value

2.Hash Store Shopping cart data operations Code Implementation

Import Java.util.HashMap;
Import Java.util.Map;
Import Java.util.Map.Entry;

Import Java.util.Set;
Import Org.junit.After;
Import Org.junit.Before;

Import Org.junit.Test;

Import Redis.clients.jedis.Jedis; /** * *//** * <p>Title:RedisHashTest</p> * <p>description: </p> * <p>company: </
	p> * @author Jiechang * @date December 14, 2015 Morning 11:12:57 * @vesion 1.0 */public class Redishashtest {Jedis Jedis = null;
	@Before public void before () {Jedis = new Jedis ("", 6379);
	} @After public void after () {jedis.disconnect (); * */@Test public void Hset () {//Insert a hash data (cart) into the Redis library Jedis.hset ("cart:user00001", "/**")
	Deep understanding of the JVM "," 1 "); }/** * Insert multiple Data */@Test public void Hmset () {//once to add more field-value to hashmap<string in this hash data (shopping cart) that already exists
		string> productmap= New hashmap<string, string> ();
		Productmap.put ("Springmvc from beginner to Proficient", "2");
		Productmap.put ("Advanced JS siege Lion Treasure", "2"); Hmset adding multiple Data JEdis.hmset ("cart:user00001", Productmap);  /** * Only Remove all Key-value */@Test public void Hgetall () {//Get the specified user from the shopping cart, all goods and their quantity map<string, string>
		Catmap = Jedis.hgetall ("cart:user00001");
		set<entry<string, string>> entryset = Catmap.entryset ();
		System.out.println ("----------take out the goods and quantity information in the shopping basket---------");
		For (entry<string, string> entry:entryset) {System.out.println (Entry.getkey () + ":" +entry.getvalue ()); }}/** * Only remove all keys */@Test public void Hkeys () {System.out.println ("----------only take out the list of items in the basket---------
		Get all the goods from the shopping cart set<string> products = Jedis.hkeys ("cart:user00001");
		for (String p:products) {System.out.println (P);

Output results

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: and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: