Redis 哈希(Hash)
HSET | 设置哈希表中一个字段的值 | HSET key field value |
HSETNX | 只有在字段不存在时,设置哈希表中一个字段的值 | HSETNX key field value |
HGET | 获取哈希表中指定字段的值 | HGET key field |
HGETALL | 获取哈希表中所有字段和值 | HGETALL key |
HDEL | 删除哈希表中一个或多个字段 | HDEL key field [field ...] |
HEXISTS | 查看哈希表中指定字段是否存在 | HEXISTS key field |
HLEN | 获取哈希表中字段的数量 | HLEN key |
HKEYS | 获取哈希表中所有字段名 | HKEYS key |
HVALS | 获取哈希表中所有字段的值 | HVALS key |
HMSET | 同时设置哈希表中多个字段的值 | HMSET key field1 value1 [field2 value2 ...] |
HMGET | 同时获取哈希表中多个字段的值 | HMGET key field1 [field2 ...] |
HINCRBY | 为哈希表中指定字段的整数值加上指定增量 | HINCRBY key field increment |
HINCRBYFLOAT | 为哈希表中指定字段的浮点数值加上指定增量 | HINCRBYFLOAT key field increment |
场景示例:以存储用户信息为例
假设我们使用Redis的哈希结构来存储用户信息,用户ID作为哈希的键,用户的各项信息作为字段和值。
- 添加用户信息
使用
HMSET
命令可以一次设置多个用户字段信息。
import redis.clients.jedis.Jedis;
public class UserHashExample {
public static void main(String[] args) {
Jedis jedis = new Jedis("localhost");
String userId = "user:1";
jedis.hmset(userId, "name", "Alice", "age", "30", "email", "alice@example.com");
jedis.close();
}
}
- 获取用户单个信息
使用
HGET
命令获取用户的某个特定信息,比如获取用户的年龄。
import redis.clients.jedis.Jedis;
public class GetUserSingleInfoExample {
public static void main(String[] args) {
Jedis jedis = new Jedis("localhost");
String userId = "user:1";
String age = jedis.hget(userId, "age");
System.out.println("User's age: " + age);
jedis.close();
}
}
- 获取用户所有信息
使用
HGETALL
命令获取用户的所有信息。
import redis.clients.jedis.Jedis;
import java.util.Map;
public class GetUserAllInfoExample {
public static void main(String[] args) {
Jedis jedis = new Jedis("localhost");
String userId = "user:1";
Map<String, String> userInfo = jedis.hgetAll(userId);
for (Map.Entry<String, String> entry : userInfo.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
jedis.close();
}
}
- 更新用户信息
使用
HSET
命令更新用户的某项信息,比如更新用户的邮箱。
import redis.clients.jedis.Jedis;
public class UpdateUserInfoExample {
public static void main(String[] args) {
Jedis jedis = new Jedis("localhost");
String userId = "user:1";
jedis.hset(userId, "email", "newalice@example.com");
jedis.close();
}
}
- 删除用户信息字段
使用
HDEL
命令删除用户的某个信息字段,比如删除用户的年龄信息。
import redis.clients.jedis.Jedis;
public class DeleteUserInfoFieldExample {
public static void main(String[] args) {
Jedis jedis = new Jedis("localhost");
String userId = "user:1";
jedis.hdel(userId, "age");
jedis.close();
}
}
- 判断用户信息字段是否存在
使用
HEXISTS
命令判断用户的某个信息字段是否存在,比如判断用户的电话号码字段是否存在。
import redis.clients.jedis.Jedis;
public class CheckUserInfoFieldExistsExample {
public static void main(String[] args) {
Jedis jedis = new Jedis("localhost");
String userId = "user:1";
boolean exists = jedis.hexists(userId, "phone");
System.out.println("Phone field exists: " + exists);
jedis.close();
}
}
- 获取用户信息字段数量
使用
HLEN
命令获取用户信息的字段数量。
import redis.clients.jedis.Jedis;
public class GetUserInfoFieldCountExample {
public static void main(String[] args) {
Jedis jedis = new Jedis("localhost");
String userId = "user:1";
long fieldCount = jedis.hlen(userId);
System.out.println("Number of user fields: " + fieldCount);
jedis.close();
}
}
- 获取用户所有信息字段名
使用
HKEYS
命令获取用户所有信息的字段名。
import redis.clients.jedis.Jedis;
import java.util.Set;
public class GetUserInfoFieldNamesExample {
public static void main(String[] args) {
Jedis jedis = new Jedis("localhost");
String userId = "user:1";
Set<String> fieldNames = jedis.hkeys(userId);
for (String fieldName : fieldNames) {
System.out.println(fieldName);
}
jedis.close();
}
}
- 获取用户所有信息字段值
使用
HVALS
命令获取用户所有信息的字段值。
import redis.clients.jedis.Jedis;
import java.util.List;
public class GetUserInfoFieldValuesExample {
public static void main(String[] args) {
Jedis jedis = new Jedis("localhost");
String userId = "user:1";
List<String> fieldValues = jedis.hvals(userId);
for (String fieldValue : fieldValues) {
System.out.println(fieldValue);
}
jedis.close();
}
}
- 增加用户信息字段的数值
假设用户有积分字段,使用
HINCRBY
命令为用户的积分增加指定数值。
import redis.clients.jedis.Jedis;
public class IncrementUserScoreExample {
public static void main(String[] args) {
Jedis jedis = new Jedis("localhost");
String userId = "user:1";
// 先设置初始积分
jedis.hset(userId, "score", "100");
long newScore = jedis.hincrBy(userId, "score", 50);
System.out.println("New user score: " + newScore);
jedis.close();
}
}
- 增加用户信息字段的浮点数值
假设用户有一个评级字段(如平均评分),使用
HINCRBYFLOAT
命令为用户的评级增加指定的浮点数值。
import redis.clients.jedis.Jedis;
public class IncrementUserRatingExample {
public static void main(String[] args) {
Jedis jedis = new Jedis("localhost");
String userId = "user:1";
// 先设置初始评级
jedis.hset(userId, "rating", "4.5");
double newRating = jedis.hincrByFloat(userId, "rating", 0.5);
System.out.println("New user rating: " + newRating);
jedis.close();
}
}
#牛客创作赏金赛##我的2024牛客高光时刻#