redis是一个高性能的key-value数据库,Redis支持数据的持久化,可以将内存中的数据保持在磁盘中,重启的时候可以再次加载进行使用。Redis不仅仅支持简单的key-value类型的数据,同时还提供list,set,zset,hash等数据结构的存储。Redis支持数据的备份,即master-slave模式的数据备份。
redis的应用场景:会话缓存(最常用)、消息队列、支付和活动排行榜或计数、发布、订阅消息(消息通知)、商品列表、评论列表等。
一、运行redis的命令:首先切换到redis目录下在运行命令 redis-server.exe redis.windows.conf,开启redis服务。
由redis.windows.conf配置文件,默认ip和端口127.0.0.1:6379,在配置文件内可以修改ip和端口。
客户端连接服务端的命令:redis-cli.exe -h 127.0.0.1 -p 6379
二、SpringBoot使用Redis缓存和spring cache
(1)pom.xml引入jar包,如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
(2)在application.yml配置redis连接信息
(3)Redis缓存配置类
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport{
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private int port;
@Bean
public JedisConnectionFactory jedisConnectionFactory() {
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(host,port);
return new JedisConnectionFactory(redisStandaloneConfiguration);
}
@Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory cf) {
RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();
redisTemplate.setConnectionFactory(cf);
return redisTemplate;
}
/*
缓存管理器
* 要启用spring缓存支持,需创建一个 CacheManager的 bean,CacheManager 接口有很多实现,这里Redis 的集成,用
* RedisCacheManager这个实现类 Redis 不是应用的共享内存,它只是一个内存服务器,就像 MySql 似的,
* 我们需要将应用连接到它并使用某种“语言”进行交互,因此我们还需要一个连接工厂以及一个 Spring 和 Redis 对话要用的
* RedisTemplate, 这些都是 Redis 缓存所必需的配置,把它们都放在自定义的 CachingConfigurerSupport 中
*/
@Bean
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
RedisCacheManager.RedisCacheManagerBuilder builder = RedisCacheManager
.RedisCacheManagerBuilder
.fromConnectionFactory(redisConnectionFactory);
return builder.build();
}
}
(4)编写相关的实体类,一定要实现序列化接口用于序列化!
(5)@Cacheable和 @CachePut
@Cacheable可以标记在一个方法上,也可以标记在一个类上。当标记在一个方法上时表示该方法是支持缓存的,当标记在一个类上时则表示该类所有的方法都是支持缓存的。对于一个支持缓存的方法,Spring会在其被调用后将其返回值缓存起来,以保证下次利用同样的参数来执行该方法时可以直接从缓存中获取结果,而不需要再次执行该方法。Spring在缓存方法的返回值时是以键值对进行缓存的,值就是方法的返回结果,至于键的话,Spring又支持两种策略,默认策略和自定义策略。@Cacheable可以指定三个属性,value、key和condition。
//根据key从缓存获取数据,缓存中有数据,从换缓存获取;缓存没数据,从数据库中获取,并保存在缓存
@Cacheable(value = CACHE_NAME,key = "#phone")
public Admins getAdmins(String phone){
Admins admins = adminService.queryByPhone(phone);
System.out.println("从数据库中获取数据");
return admins;
}
@Cacheable标注方法,Spring在每次执行前都会检查Cache中是否存在相同key的缓存元素,如果存在就不再执行该方法,而是直接从缓存中获取结果进行返回,否则才会执行并将返回结果存入指定的缓存中。@CachePut也可以声明一个方法支持缓存功能。与@Cacheable不同的是使用@CachePut标注的方法在执行前不会去检查缓存中是否存在之前执行过的结果,而是每次都会执行该方法,并将执行结果以键值对的形式存入指定的缓存中。
@CachePut(value = CACHE_NAME,key = "#admins.phone")
public Admins putAdmins(Admins admins){
return admins;
}
(6)使用单元测试,执行的结果
(7)查询添加在redis缓存的数据
info Keyspace 查看存储数据的相关信息
keys * 查看所有的 key
del key 删除单个
get key 查询单个key值