数据类型:
1.string 可以是字符串、整数、浮点数
2.list 它是一个有序的双向链表,使用场景是需要经常插入和删除的数据
3.set 无序且独一无二的string元素集合
4.hash 一个键值对的map无序列表,适合存储对象
5.zset 有序且独一无二的string元素集合集合,元素的排序是根据分值的大小排序
1.hash
1. 一个键值对的map无序列表,适合存储对象
hmset key field1 value1 [field2 value2] : 设置多个键值对
hgetall key :获取所有键值
hkeys key: 获取所有键
hvals key:获取所有键的值
hlen key:获取某个对象的键值对的数量
hexists key filed :判断是否存在field字段
hdel key field1 [field2]:删除某个对象的field1字段
hmget key field1 [field2]:获取键的值
2.在java程序中
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
RedisTemplate redisTemplate = applicationContext.getBean(RedisTemplate.class);
String key = "hash";
Map<String, String> map = new HashMap<String, String>();
map.put("f1", "val1");
map.put("f2", "val2");
// 相当于hmset命令
redisTemplate.opsForHash().putAll(key, map);
// 相当于hset命令
redisTemplate.opsForHash().put(key, "f3", "6");
printValueForhash(redisTemplate, key, "f3");
// 相当于 hexists key filed命令
boolean exists = redisTemplate.opsForHash().hasKey(key, "f3");
System.out.println(exists);
// 相当于hgetall命令
Map keyValMap = redisTemplate.opsForHash().entries(key);
// 相当于hincrby命令
redisTemplate.opsForHash().increment(key, "f3", 2);
printValueForhash(redisTemplate, key, "f3");
// 相当于hincrbyfloat命令
redisTemplate.opsForHash().increment(key, "f3", 0.88);
printValueForhash(redisTemplate, key, "f3");
// 相当于hvals命令
List valueList = redisTemplate.opsForHash().values(key);
// 相当于hkeys命令
Set keyList = redisTemplate.opsForHash().keys(key);
// 相当于hmget命令
List valueList2 = redisTemplate.opsForHash().multiGet(key, keyList);
// 相当于hsetnx命令
boolean success = redisTemplate.opsForHash().putIfAbsent(key, "f4", "val4");
System.out.println(success);
// 相当于hdel命令
Long result = redisTemplate.opsForHash().delete(key, "f1", "f2");
System.out.println(result);
2.List
它是一个有序的双向链表,使用场景是需要经常插入和删除的数据
链表命令分左操作和有操作
lpush key node1 [node2]:将节点插入到链表最左边
rpush key node1 [node2]:将节点插入到链表最右边
lindex key index:读取下标为index的节点
llen key:求链表的节点数
lpop key:删除左边第一个节点并返回
rpop key:删除右边第一个节点并返回
linsert key before|after node new_node:在节点node前后插入新节点new_node
lrange list start end:获取链表list从start下标到end下标的节点值
lrem list count value:从左到右删除小于或等于count个等于value的节点
lset key index node:设置下标为index的节点值为node
ltrim key start stop :只保留从start到stop区间的节点
2.java程序中
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
RedisTemplate redisTemplate = applicationContext.getBean(RedisTemplate.class);
try {
// 删除链表,以便我们可以反复测试
redisTemplate.delete("list");
// 把node3插入链表list
redisTemplate.opsForList().leftPush("list", "node3");
List<String> nodeList = new ArrayList<String>();
for (int i = 2; i >= 1; i--) {
nodeList.add("node" + i);
}
// 相当于lpush把多个价值从左插入链表
redisTemplate.opsForList().leftPushAll("list", nodeList);
// 从右边插入一个节点
redisTemplate.opsForList().rightPush("list", "node4");
// 获取下标为0的节点
String node1 = (String) redisTemplate.opsForList().index("list", 0);
// 获取链表长度
long size = redisTemplate.opsForList().size("list");
// 从左边弹出一个节点
String lpop = (String) redisTemplate.opsForList().leftPop("list");
// 从右边弹出一个节点
String rpop = (String) redisTemplate.opsForList().rightPop("list");
// 注意,需要使用更为底层的命令才能操作linsert命令
// 使用linsert命令在node2前插入一个节点
redisTemplate.getConnectionFactory().getConnection().lInsert("list".getBytes("utf-8"),
RedisListCommands.Position.BEFORE, "node2".getBytes("utf-8"), "before_node".getBytes("utf-8"));
// 使用linsert命令在node2后插入一个节点
redisTemplate.getConnectionFactory().getConnection().lInsert("list".getBytes("utf-8"),
RedisListCommands.Position.AFTER, "node2".getBytes("utf-8"), "after_node".getBytes("utf-8"));
// 判断list是否存在,如果存在则从左边插入head节点
redisTemplate.opsForList().leftPushIfPresent("list", "head");
// 判断list是否存在,如果存在则从右边插入end节点
redisTemplate.opsForList().rightPushIfPresent("list", "end");
// 从左到右,或者下标从0到10的节点元素
List valueList = redisTemplate.opsForList().range("list", 0, 10);
nodeList.clear();
for (int i = 1; i <= 3; i++) {
nodeList.add("node");
}
// 在链表左边插入三个值为node的节点
redisTemplate.opsForList().leftPushAll("list", nodeList);
// 从左到右删除至多三个node节点
redisTemplate.opsForList().remove("list", 3, "node");
// 给链表下标为0的节点设置新值
redisTemplate.opsForList().set("list", 0, "new_head_value");
} catch (UnsupportedEncodingException ex) {
ex.printStackTrace();
}