Redis(Remote Dictionary Server,远程字典服务器)是一个开源的高性能键值对(key-value)数据库。它以其出色的性能、可靠性和灵活性而闻名,常用于开发中来实现多种数据存储和通信模式
内存中数据库:Redis主要将数据存储在内存中,这使得读写操作非常快速
支持多种数据类型:Redis支持多种类型的数据存储,包括字符串(strings)、列表(lists)、集合(sets)、有序集合(sorted sets)、散列(hashes)、位图(bitmaps)、超日志(hyperloglogs)等
持久化:尽管Redis是内存数据库,但它提供了持久化机制,可以将内存中的数据保存到磁盘,防止数据丢失
丰富的特性:Redis提供了发布/订阅、事务、Lua脚本、慢查询日志、监视器等高级功能
字符串(String):
key: "value"
命令
:设置值:set key value,获取值:get key列表(List):
key: ["item1", "item2", ..., "itemN"]
集合(Set):
key: {"item1", "item2", ..., "itemN"}
有序集合(Sorted Set):
key: {(item1, score1), (item2, score2), ..., (itemN, scoreN)}
哈希(Hash):
key: {field1: "value1", field2: "value2", ..., fieldN: "valueN"}
命令:往hash设置值:hset hash key value
从hash中获得值:hget hash set
org.springframework.boot spring-boot-starter-data-redis org.springframework.boot spring-boot-starter-cache
我的redis没有设置密码所以不用配置password
spring redis: host: localhost port: 6379 database: 0
序列化器主要是为了解决存储到redis中数据乱码的问题
创建一个config类,将RedisTemplate注册成Bean
package com.sky.config; 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; } }
package com.sky.test; import com.sky.config.RedisConfiguration; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.redis.core.*; import java.util.List; import java.util.Set; import java.util.concurrent.TimeUnit; //@SpringBootTest public class SpringDateRedisTest { @Autowired private RedisTemplate redisTemplate; @Test public void TestRedisTemplate(){ System.out.println(redisTemplate); 1.字符串类型 // ValueOperations valueOperations = redisTemplate.opsForValue(); // //2.Hash类型 // HashOperations hashOperations = redisTemplate.opsForHash(); // //3.列表(类似与队列) // ListOperations listOperations = redisTemplate.opsForList(); // //4.集合类型 // SetOperations setOperations = redisTemplate.opsForSet(); // //5.有序集合 // ZSetOperations zSetOperations = redisTemplate.opsForZSet(); } @Test public void StringTest(){ //获得redis数据库操作对象 ValueOperations valueOperations = redisTemplate.opsForValue(); //set key value valueOperations.set("name","张三"); //get key valueOperations.get("name"); //setex key seconds value valueOperations.set("yam","123456",1, TimeUnit.MINUTES); //setnx key value valueOperations.setIfAbsent("look","李四"); valueOperations.setIfAbsent("look","王五"); } @Test public void HashTest(){ HashOperations hashOperations = redisTemplate.opsForHash(); //Hset key field value hashOperations.put("student","name","小明"); hashOperations.put("student","age","10"); //Hget key field Object o = hashOperations.get("student", "name"); System.out.println(o); //Hkeys key Set student = hashOperations.keys("student"); //遍历集合 for (Object o1 : student) { System.out.println(o1); } //Hvals key List student1 = hashOperations.values("student"); //Hdel key filed hashOperations.delete("student","age"); } @Test public void ListTest(){ ListOperations listOperations = redisTemplate.opsForList(); //Lpush key value listOperations.leftPush("100","aa"); listOperations.leftPush("100","bb"); listOperations.leftPushAll("100","cc,dd,ee"); //Lrange key start stop listOperations.range("100",0,-1); //lpop key listOperations.leftPop("100"); //Llen key listOperations.size("100"); } @Test public void SetTest(){ SetOperations setOperations = redisTemplate.opsForSet(); //Sadd key member1 setOperations.add("Zoo","小猫","小狗","大象"); setOperations.add("Home","小猫","小狗"); //Smembers key setOperations.members("zoo"); //Scard key System.out.println(setOperations.size("Zoo")); //Sinter members1 members2 setOperations.intersect("Zoo","home"); //Sunion members1 members2 Set union = setOperations.union("Zoo", "home"); for (Object o : union) { System.out.println(o); } } @Test public void ZsetTest(){ //有序集合操作对象 ZSetOperations zSetOperations = redisTemplate.opsForZSet(); //Zadd key score1 member1 score2 member2 zSetOperations.add("花园","向日葵",10); zSetOperations.add("花园","百合",9); //Zrange key start stop Set set = zSetOperations.range("花园", 0, -1); for (Object o : set) { System.out.println(o); } //Zrem key member1 member2...... zSetOperations.incrementScore("花园","百合",5); zSetOperations.remove("花园","向日葵"); } }
Redis适用于需要快速访问和处理大量数据的应用程序,它的高性能和低延迟特性使其成为许多现代应用程序的首选数据存储解决方案。然而,由于其内存数据库的特性,Redis通常不适用于存储大量持久化数据,除非使用其持久化功能或与其他持久化存储解决方案结合使用
常常使用redis来进行缓存,提高请求响应速度
依据redis数据类型的特性来实现一些特殊的功能如字符串的setnx命令来实现分布式锁
redis的消息订阅来实现消息中间件的功能。