package grl.redis.users;

import redis.clients.jedis.Jedis;

import java.util.HashMap;
import java.util.Map;

public class HashSample {
    public static void main(String[] args) {
        // 从环境变量中获取主机信息
        String redisHost = System.getenv("REDIS_HOST");

        Jedis redis = new Jedis(redisHost);

        Map<String, String> map = new HashMap<>();
        map.put("email", "user1@domain.com");
        map.put("userid", "12356");
        map.put("address", "1111 Main St. Houston TX, 77054");
        map.put("phone", "555-124-5544");

        //Store all values in the map
        //REDIS HMSET
        redis.hmset("user.by.id." + map.get("userid"), map);

        //Index this user by their email
        redis.set("user.id.by.email."+map.get("email"), map.get("userid"));

        System.out.println("User stored: " + map.get("userid"));

        //Confirm that all values are set
        //REDIS HGETALL
        System.out.println(
                redis.hgetAll("user.by.id." + map.get("userid"))
        );

        map = new HashMap<>();
        map.put("email", "user2@other.domain.com");
        map.put("userid", "24567");
        map.put("address", "1111 Main St. Houston TX, 77054");
        map.put("phone", "555-124-5544");

        //Store all values in the map
        //REDIS HMSET
        redis.hmset("user.by.id." + map.get("userid"), map);

        //Index this user by their email
        redis.set("user.id.by.email.", map.get("email"));

        System.out.println("User stored: " + map.get("userid"));

        //Confirm that all values are set
        //REDIS HGETALL
        System.out.println(
                redis.hgetAll("user.by.id." + map.get("userid"))
        );

        //Get user by email using String key
        String email = "user1@domain.com";
        String userid = redis.get("user.id.by.email." + email);
        if (userid != null) {
            System.out.println(String.format(
                    "Found user by email: %s",
                    redis.hgetAll("user.by.id." + userid)
            ));
        }
    }
}

@@ -553,57 +553,45 @@
   //
-Store all values in the map%0A        //REDIS HMSET
+ %E5%B0%86%E6%89%80%E6%9C%89%E6%95%B0%E6%8D%AE%E4%BF%9D%E5%AD%98%E5%9C%A8 map %E4%B8%AD%EF%BC%8C%E7%84%B6%E5%90%8E%E4%BD%BF%E7%94%A8 hmset %E6%96%B9%E6%B3%95%E4%B8%80%E6%AC%A1%E6%8F%92%E5%85%A5%E5%AF%B9%E8%B1%A1%E6%95%B0%E6%8D%AE
 %0A   
@@ -663,38 +663,27 @@
   //
-Index this user by their email
+ %E4%BB%A5%E9%82%AE%E4%BB%B6%E5%9C%B0%E5%9D%80%E4%BD%9C%E4%B8%BA%E7%B4%A2%E5%BC%95%EF%BC%8C%E4%BF%9D%E5%AD%98 ID %E4%BF%A1%E6%81%AF
 %0A   
@@ -716,17 +716,19 @@
 .email.%22
-+
+ + 
 map.get(
@@ -838,63 +838,32 @@
   //
-Confirm that all values are set%0A        //REDIS HGETALL
+ %E4%BD%BF%E7%94%A8 hgetall %E6%96%B9%E6%B3%95%E6%A3%80%E6%9F%A5%E6%98%AF%E5%90%A6%E6%95%B0%E6%8D%AE%E6%8F%92%E5%85%A5%E6%88%90%E5%8A%9F
 %0A   
@@ -956,32 +956,49 @@
 %22))%0A        );%0A%0A
+        // %E7%AC%AC%E4%BA%8C%E4%B8%AA%E5%AF%B9%E8%B1%A1%0A
         map = ne
@@ -1210,68 +1210,8 @@
 );%0A%0A
-        //Store all values in the map%0A        //REDIS HMSET%0A
     
@@ -1270,50 +1270,8 @@
 ap);
-%0A%0A        //Index this user by their email
 %0A   
@@ -1396,74 +1396,8 @@
 );%0A%0A
-        //Confirm that all values are set%0A        //REDIS HGETALL%0A
     
@@ -1511,42 +1511,35 @@
   //
-Get user by email using String key
+ %E9%80%9A%E8%BF%87%E7%B4%A2%E5%BC%95%E5%BE%97%E5%88%B0%E7%94%A8%E6%88%B7 ID%EF%BC%8C %E5%86%8D%E6%A0%B9%E6%8D%AE ID %E6%9D%A5%E8%BF%9B%E8%A1%8C%E6%9F%A5%E8%AF%A2%E3%80%82
 %0A   

add hashsample comment