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
3.java 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 ("127.0.0.1", 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