关于Redis的介绍很多,我就介绍关于我在web开发中使用到比较多的点。
基于内存可持久化的日志型、Key-Value数据库。说到这就可以想到Java中字典性集合:Map、Map
、Map 等。就和Redis的字符型数据类型一样。
# 存入一个name(名字)叫xiaohua
set name xiaohua
# 读取名字
get name
# 存入age(年龄)18,并用21覆盖
set age 18
set age 21
# 获取到21,18被后来者覆盖
get age
# 过期策略,设置保存code(验证码)为abcd,只有十秒存在时间
setex code 10 abcd
# 十秒之后code -> null
get code
# setnx key value 只有在 key 不存在时设置 key 的值
setnx a 666
setnx a 999
# 返回999
get a
# 往key 为001 存入 hash键值对 2对
hset 001 name xiaohua
hset 001 age 23
hset 002 name xiaomei
hset 002 age 18
# HGET key field 获取存储在哈希表中指定字段的值
# 获取002中的 name的值
hget 002 name
# HDEL key field 删除存储在哈希表中的指定字段
# 删除 002 中的name
hdel 002 name
# HKEYS key 获取哈希表中所有字段
hkeys 002
# HVALS key 获取哈希表中所有值
HVALS 002
# HGETALL key 获取在哈希表中指定 key 的所有字段和值
HGETALL 001
# LPUSH key value1 [value2] 将一个或多个值插入到列表头部
LPUSH list1 a b c d
LPUSH list2 1 2 3 4
LPUSH list3 4 3 2 1
# LRANGE key start stop 获取列表指定范围内的元素
LRANGE list1 0 -1 # 查所有
# a最先插入,所以在尾部
# RPOP key 移除并获取列表最后一个元素
RPOP list1 # 返回a 当然a没有了
# LLEN key 获取列表长度
LLEN list1 # 3
# BRPOP key1 [key2 ] timeout 移出并获取列表的最后一个元素, 如果列表没有元素会阻塞列表直到等待超 时或发现可弹出元素为止
BRPOP list1 10
# SADD key member1 [member2] 向集合添加一个或多个成员
SADD set1 a b c
SADD set2 1 2 3
# SMEMBERS key 返回集合中的所有成员
SMEMBERS set1
# SCARD key 获取集合的成员数
SCARD set1
# SINTER key1 [key2] 返回给定所有集合的交集
SINTER set1 set2 # a 交集是a
# SUNION key1 [key2] 返回所有给定集合的并集
SUNION set1 set2 # a b c 2 3
# SDIFF key1 [key2] 返回给定所有集合的差集
SDIFF set1 set2 #注意顺序 2者相减
# SREM key member1 [member2] 移除集合中一个或多个成员
SREM set1 a b # 删除元素
# ZADD key score1 member1 [score2 member2] 向有序集合添加一个或多个成员,或者更新已存在成员的 分数
zadd sset1 0.1 a 4.0 b 2.1 c
zadd sset2 9.0 p 8.8 w 7.0 q
# ZRANGE key start stop [WITHSCORES] 通过索引区间返回有序集合中指定区间内的成员
ZRANGE sset1 0 -1 # 获得全部
ZRANGE sset1 0 -1 WITHSCORES # 分数也会显示
# ZINCRBY key increment member 有序集合中对指定成员的分数加上增量 increment
ZINCRBY sset1 20 b #意味着b的分值改变了
# 可以查询下
ZRANGE sset1 0 -1 WITHSCORES
# ZREM key member [member ...] 移除有序集合中的一个或多个成员
ZREM sset1 b #将b这个元素移除
# KEYS pattern 查找所有符合给定模式( pattern)的 key
KEYS * #查询所有的key
# EXISTS key 检查给定 key 是否存在
EXISTS name
# TYPE key 返回 key 所储存的值的类型
TYPE name
# TTL key 返回给定 key 的剩余生存时间(TTL, time to live),以秒为单位
# -1表示一直存在
TTL name
# DEL key 该命令用于在 key 存在是删除 key
DEL name
#查询所有的key
KEYS *
以在Spring Boot架构中的使用为例
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-redisartifactId>
dependency>
spring:
redis:
host: localhost # 服务器IP
port: 6379 # 端口号
password: # 服务密码,需设置,不设置为空
database: 10 # 数据库编号
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
@Slf4j
public class RedisConfiguration {
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory){
log.info("创建Redis模板对象...");
RedisTemplate redisTemplate = new RedisTemplate();
//设置redis的连接工厂对象
redisTemplate.setConnectionFactory(redisConnectionFactory);
//设置redis key的序列化器
redisTemplate.setKeySerializer(new StringRedisSerializer());
return redisTemplate;
}
}
@Resource
RedisTemplate redisTemplate;
@Test
public void redisTemplates(){
//string数据操作
ValueOperations valueOperations = redisTemplate.opsForValue();
//hash类型的数据操作
HashOperations hashOperations = redisTemplate.opsForHash();
//list类型的数据操作
ListOperations listOperations = redisTemplate.opsForList();
//set类型数据操作
SetOperations setOperations = redisTemplate.opsForSet();
//zset类型数据操作
ZSetOperations zSetOperations = redisTemplate.opsForZSet();
}
// set get setex setnx
redisTemplate.opsForValue().set("name","xioahua");
String city = (String) redisTemplate.opsForValue().get("name");
System.out.println(city);
// 过期策略
redisTemplate.opsForValue().set("code","1234",3, TimeUnit.MINUTES);
// 存入前判断
redisTemplate.opsForValue().setIfAbsent("lock","1");
redisTemplate.opsForValue().setIfAbsent("lock","2");
//hset hget hdel hkeys hvals
HashOperations hashOperations = redisTemplate.opsForHash();
hashOperations.put("100","name","xiaohua");
hashOperations.put("100","age","20");
String name = (String) hashOperations.get("100", "name");
System.out.println(name);
Set keys = hashOperations.keys("100");
System.out.println(keys);
List values = hashOperations.values("100");
System.out.println(values);
hashOperations.delete("100","age");
//lpush lrange rpop llen
ListOperations listOperations = redisTemplate.opsForList();
listOperations.leftPushAll("mlist","a","b","c");
listOperations.leftPush("mlist","d");
List mylist = listOperations.range("mlist", 0, -1);
System.out.println(mylist);
listOperations.rightPop("mlist");
Long size = listOperations.size("mlist");
System.out.println(size);
//sadd smembers scard sinter sunion srem
SetOperations setOperations = redisTemplate.opsForSet();
setOperations.add("set1","a","b","c","d");
setOperations.add("set2","a","b","x","y");
Set members = setOperations.members("set1");
System.out.println(members);
Long size = setOperations.size("set1");
System.out.println(size);
Set intersect = setOperations.intersect("set1", "set2");
System.out.println(intersect);
Set union = setOperations.union("set1", "set2");
System.out.println(union);
setOperations.remove("set1","a","b");
//zadd zrange zincrby zrem
ZSetOperations zSetOperations = redisTemplate.opsForZSet();
zSetOperations.add("zset1","a",10);
zSetOperations.add("zset1","b",12);
zSetOperations.add("zset1","c",9);
Set zset1 = zSetOperations.range("zset1", 0, -1);
System.out.println(zset1);
zSetOperations.incrementScore("zset1","c",10);
zSetOperations.remove("zset1","a","b");
//keys exists type del
Set keys = redisTemplate.keys("*");
System.out.println(keys);
Boolean name = redisTemplate.hasKey("name");
Boolean set1 = redisTemplate.hasKey("set1");
for (Object key : keys) {
DataType type = redisTemplate.type(key);
System.out.println(type.name());
}
redisTemplate.delete("mlist");