哈希命令
基本命令
入门命令
localhost:6379> hset map id 123
(integer) 1
localhost:6379> hset map name zhangsan
(integer) 1
localhost:6379> hget map id
"123"
localhost:6379> hget map name
"zhangsan"
Hdel 命令
用于删除哈希表 key 中的一个或多个指定字段,不存在的字段将被忽略。
localhost:6379> hdel map id
(integer) 1
Hexists 命令
用于查看哈希表的指定字段是否存在。
localhost:6379> hexists map id
(integer) 0
localhost:6379> hexists map name
(integer) 1
Hget 命令
用于返回哈希表中指定字段的值。
localhost:6379> hget map id
(nil)
localhost:6379> hget map name
"zhangsan"
Hgetall 命令
用于返回哈希表中,所有的字段和值。
在返回值里,紧跟每个字段名(field name)之后是字段的值(value),所以返回值的长度是哈希表大小的两倍。
localhost:6379> hset map age 24
(integer) 1
localhost:6379> hgetall map
1) "name"
2) "zhangsan"
3) "age"
4) "24"
Hincrby 命令
用于为哈希表中的字段值加上指定增量值。
增量也可以为负数,相当于对指定字段进行减法操作。
如果哈希表的 key 不存在,一个新的哈希表被创建并执行 HINCRBY 命令。
如果指定的字段不存在,那么在执行命令前,字段的值被初始化为 0 。
对一个储存字符串值的字段执行 HINCRBY 命令将造成一个错误。
本操作的值被限制在 64 位(bit)有符号数字表示之内。
localhost:6379> hincrby map id 1
(integer) 1
localhost:6379> hincrby map age 2
(integer) 26
localhost:6379> hgetall map
1) "name"
2) "zhangsan"
3) "age"
4) "26"
5) "id"
6) "1"
Hincrbyfloat 命令
用于为哈希表中的字段值加上指定浮点数增量值。
如果指定的字段不存在,那么在执行命令前,字段的值被初始化为 0 。
localhost:6379> hincrbyfloat map age -5.5
"20.5"
localhost:6379> hget map age
"20.5"
Hkeys 命令
用于获取哈希表中的所有域(field)。
localhost:6379> hkeys map
1) "name"
2) "age"
3) "id"
Hlen 命令
用于获取哈希表中字段的数量。
localhost:6379> hlen map
(integer) 3
Hmget 命令
用于返回哈希表中,一个或多个给定字段的值。
如果指定的字段不存在于哈希表,那么返回一个 nil 值。
localhost:6379> hmget map id name remark
1) "1"
2) "zhangsan"
3) (nil)
Hmset 命令
用于同时将多个 field-value (字段-值)对设置到哈希表中。
此命令会覆盖哈希表中已存在的字段。
如果哈希表不存在,会创建一个空哈希表,并执行 HMSET 操作。
localhost:6379> hmset map id 2 name wangfang
OK
localhost:6379> hgetall map
1) "name"
2) "wangfang"
3) "age"
4) "20.5"
5) "id"
6) "2"
Hset 命令
用于为哈希表中的字段赋值 。
如果哈希表不存在,一个新的哈希表被创建并进行 HSET 操作。
如果字段已经存在于哈希表中,旧值将被覆盖。
localhost:6379> hset map id 10089
(integer) 0
localhost:6379> hget map id
"10089"
Hsetnx 命令
用于为哈希表中不存在的的字段赋值 。
如果哈希表不存在,一个新的哈希表被创建并进行 HSET 操作。
如果字段已经存在于哈希表中,操作无效。
如果 key 不存在,一个新哈希表被创建并执行 HSETNX 命令。
Hvals 命令
返回哈希表所有域(field)的值。
localhost:6379> hvals map
1) "wangfang"
2) "20.5"
3) "10089"
spring调用redis
pom.xml配置 核心依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<!-- slf4j日志统一管理 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
<!-- spring中的redis -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.8.4.RELEASE</version>
</dependency>
hash-redis.xml spring配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd"
default-lazy-init="false">
<!-- 占位符配置文件 -->
<!-- <context:property-placeholder location="classpath:redis.properties" /> -->
<!-- redis 连接池配置-->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="50" />
<property name="maxTotal" value="100" />
<property name="maxWaitMillis" value="20000" />
</bean>
<!-- Spring-redis连接池工厂配置 -->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="127.0.0.1" />
<property name="port" value="6379" />
<property name="timeout" value="2000" />
<property name="poolConfig" ref="poolConfig" />
</bean>
<!-- 序列化 String类型 -->
<bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" />
<!-- 序列化 对象 -->
<!-- <bean id="jdkSerializationRedisSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /> -->
<!-- redisTemplate 定义 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
<property name="defaultSerializer" ref="stringRedisSerializer" />
<property name="hashKeySerializer" ref="stringRedisSerializer" />
<property name="hashValueSerializer" ref="stringRedisSerializer" />
</bean>
</beans>
测试代码1
package redis.hash;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
public class RedisHashTest {
private static final Logger logger = LoggerFactory.getLogger(RedisHashTest.class);
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("redis-conf/hash/hash-redis.xml");
RedisTemplate redisTemplate = applicationContext.getBean(RedisTemplate.class);
HashOperations opsForHash = redisTemplate.opsForHash();
// 1.设置值
Map map = new HashMap();
map.put("f1", "test");
map.put("f2", "张三");
opsForHash.putAll("hash", map);
opsForHash.put("hash", "f3", "12");
// 2.读取值
String value = (String) opsForHash.get("hash", "f3");
logger.debug("从hash中获取f3值:{}", value);
// 3.判断关键字
Boolean hasKey = opsForHash.hasKey("hash", "f3");
logger.debug("hash中是否包含f3值:{}", hasKey);
// 4.获取所有值
Map entries = opsForHash.entries("hash");
logger.debug("hash所有值:{}", entries);
// 5.增加1
// 配置类型为String,导致报错,暂不解决
/*opsForHash.increment("hash", "f3", 1);
value = (String) opsForHash.get("hash", "f3");
logger.debug("从hash中获取f3值:{}", value);*/
// hkeys命令
Set keys = opsForHash.keys("hash");
// hvals命令
List values = opsForHash.values("hash");
// hmget命令
opsForHash.multiGet("hash", Arrays.asList("f", "f2"));
// hsetnx命令
opsForHash.putIfAbsent("hash", "f4", "12");
// hdel命令
opsForHash.delete("hash", "f4");
}
}
可视化工具 redis-desktop-manager下载
redis-desktop-manager-0.9.99.zip工具下载:
链接:https://pan.baidu.com/s/1BB-MvTfx6T1SDD_eq5tq5w
提取码:snby