Redis(Remote Dictionary Server)是一个开源的内存数据存储系统,它以键值对(key-value)的形式存储数据,并提供了多种数据结构的支持。
内存存储:Redis将数据存储在内存中,因此具有快速的读写性能。它的高性能使得它非常适用于需要快速读写的应用场景。
数据结构多样:除了支持常规的字符串(string)类型外,Redis还支持更复杂的数据结构,如哈希(hash)、列表(list)、集合(set)、有序集合(sorted set)等。这使得Redis不仅仅是一个简单的键值存储,还可以处理更丰富的数据操作。
持久化支持:Redis提供了两种持久化机制,将内存中的数据持久化到磁盘上,以便在重启后恢复数据。这两种机制分别是快照(snapshotting)和日志(append-only file)。
分布式支持:Redis具备高可扩展性,可以通过主从复制(master-slave replication)和集群(cluster)等方式实现数据的分布式存储和高可用性。
丰富的功能:Redis提供了许多功能和命令,如事务(transaction)、发布订阅(publish/subscribe)、Lua脚本支持、过期键(key expiration)等。这些功能使得Redis在缓存、消息队列、计数器、排行榜、实时数据处理等方面具有广泛的应用场景。
Spring Boot简介
访问Redis官方网站(https://redis.io/)并下载适用于你的操作系统的Redis安装包。根据操作系统的要求,进行安装并启动Redis服务器(redis-server.exe)。
自启动:于redis目录下
redis-server --service-install redis.windows-service.conf --loglevel verbose
或者:
redis-server --service-install redis.windows.conf --loglevel verbose
其它常用命令:
卸载服务:redis-server --service-uninstall
开启服务:redis-server --service-start
停止服务:redis-server --service-stop
在Spring Boot项目的pom.xml文件中添加Spring Data Redis依赖。例如:
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-redisartifactId>
dependency>
在application.properties或application.yml文件中配置Redis连接信息,包括主机、端口、密码等。例如:
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=your_password (如果有密码的话)
创建一个配置类,用于配置Redis连接工厂和其他相关配置。可以使用RedisStandaloneConfiguration或RedisSentinelConfiguration等来配置连接工厂。示例:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
@Configuration
public class RedisConfig {
@Bean
public RedisConnectionFactory redisConnectionFactory() {
RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
config.setHostName("127.0.0.1");
config.setPort(6379);
config.setPassword("your_password"); // 如果有密码的话
return new LettuceConnectionFactory(config);
}
}
在需要使用Redis的类中,可以通过RedisTemplate或ReactiveRedisTemplate来操作Redis。例如:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
@Service
public class RedisService {
private final RedisTemplate<String, String> redisTemplate;
@Autowired
public RedisService(RedisTemplate<String, String> redisTemplate) {
this.redisTemplate = redisTemplate;
}
public void setValue(String key, String value) {
redisTemplate.opsForValue().set(key, value);
}
public String getValue(String key) {
return redisTemplate.opsForValue().get(key);
}
// 其他操作方法...
}
启用Redis注解支持:在Spring Boot应用程序的主类上添加@EnableCaching注解,以启用Redis的注解支持。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@SpringBootApplication
@EnableCaching
public class YourApplication {
public static void main(String[] args) {
SpringApplication.run(YourApplication.class, args);
}
}
使用缓存注解:在需要缓存的方法上使用缓存注解,如@Cacheable、@CachePut、@CacheEvict等。这些注解用于指定方法的缓存行为,如读取缓存、更新缓存、清除缓存等。
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
@Service
public class YourService {
@Cacheable(value = "yourCacheName", key = "#param")
public YourData getData(String param) {
// 从数据库或其他数据源获取数据的逻辑
}
}
在你的查询方法上添加@Cacheable注解,并指定缓存的名称和键。
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
@Service
public class YourService {
@Cacheable(value = "yourCacheName", key = "#param")
public YourData getData(String param) {
// 执行查询逻辑,返回数据
}
}
可以不指定缓存的名称和键,而使用默认的缓存配置。在这种情况下,Spring Boot会为每个缓存方法创建一个默认的缓存。
@CachePut注解用于将方法的返回值更新到缓存中,适用于修改操作。
示例代码:
import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Service;
@Service
public class YourService {
@CachePut(value = "yourCacheName", key = "#article.id")
public Article updateArticle(Article article) {
// 执行修改文章的逻辑,返回修改后的文章数据
}
}
@CacheEvict注解用于从缓存中删除数据,适用于删除操作。
示例代码:
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
@Service
public class YourService {
@CacheEvict(value = "yourCacheName", key = "#articleId")
public void deleteArticle(String articleId) {
// 执行删除文章的逻辑
}
}