redis 二级缓存
时间: 2023-10-29 17:00:55 浏览: 54
Redis二级缓存是一种常见的缓存策略,它使用两个层次的缓存来提高性能和减轻数据库的压力。一级缓存通常是位于应用程序内存中的快速缓存,而二级缓存则是位于Redis这样的分布式缓存中。
在使用Redis作为二级缓存时,应用程序首先会尝试从一级缓存中获取所需的数据,如果一级缓存没有命中,就会去二级缓存中查询。如果在二级缓存中找到了数据,应用程序会将数据从Redis中取出并返回给用户,并将数据同时写入一级缓存中,以便下次查询时更快地获取数据。如果在二级缓存中没有找到数据,则应用程序会直接从数据库中查询数据,并将查询结果写入一级和二级缓存中。
通过使用Redis作为二级缓存,可以大大提高应用程序的响应速度和吞吐量,减轻数据库的负载压力,并且能够有效地处理高并发的读请求。同时,Redis还提供了丰富的数据结构和功能,如集合、哈希、排序等,可以灵活地支持各种缓存需求。
相关问题
mybatis二级缓存和redis二级缓存区别
MyBatis二级缓存和Redis二级缓存有以下几点区别:
1. 存储方式不同:MyBatis二级缓存是直接存储在内存中的,而Redis二级缓存是通过网络连接访问Redis服务器进行存储的。
2. 使用方式不同:MyBatis二级缓存是在应用内部使用的,而Redis二级缓存是在应用外部使用的。
3. 缓存范围不同:MyBatis二级缓存的范围是一个SqlSessionFactory,而Redis二级缓存的范围可以跨越多个应用程序。
4. 缓存策略不同:MyBatis二级缓存是基于LRU算法实现的,在缓存空间不足时会自动清理缓存。而Redis二级缓存则可以根据业务需求来设定缓存策略,例如设置过期时间、定时刷新等。
5. 数据一致性不同:MyBatis二级缓存只保证在同一个SqlSession内数据的一致性,而Redis二级缓存则可以保证在多个应用程序之间数据的一致性。
因此,选择使用哪种二级缓存应该根据具体的业务需求和系统架构来进行决策。
mybatisplus自定义redis二级缓存
MybatisPlus自带的二级缓存默认使用了EHCache作为缓存实现,但在一些场景下可能需要使用Redis作为缓存实现。本文将介绍如何在MybatisPlus中自定义Redis二级缓存。
1. 引入Redis相关依赖
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.6.2</version>
</dependency>
```
2. 自定义RedisCache
```java
public class RedisCache implements Cache {
private String id;
private static RedisTemplate<String, Object> redisTemplate;
public RedisCache(String id) {
this.id = id;
initRedisTemplate();
}
private void initRedisTemplate() {
if (redisTemplate == null) {
synchronized (RedisCache.class) {
if (redisTemplate == null) {
redisTemplate = (RedisTemplate<String, Object>) ApplicationContextHolder.getBean("redisTemplate");
}
}
}
}
@Override
public String getId() {
return id;
}
@Override
public void putObject(Object key, Object value) {
redisTemplate.opsForHash().put(id, key.toString(), value);
}
@Override
public Object getObject(Object key) {
return redisTemplate.opsForHash().get(id, key.toString());
}
@Override
public Object removeObject(Object key) {
redisTemplate.opsForHash().delete(id, key.toString());
return null;
}
@Override
public void clear() {
redisTemplate.delete(id);
}
@Override
public int getSize() {
return redisTemplate.opsForHash().size(id).intValue();
}
@Override
public ReadWriteLock getReadWriteLock() {
return null;
}
}
```
3. 自定义RedisCacheFactory
```java
public class RedisCacheFactory implements CacheFactory {
private static final Map<String, Cache> caches = new ConcurrentHashMap<>(16);
private static RedisTemplate<String, Object> redisTemplate;
public RedisCacheFactory() {
initRedisTemplate();
}
private void initRedisTemplate() {
if (redisTemplate == null) {
synchronized (RedisCacheFactory.class) {
if (redisTemplate == null) {
redisTemplate = (RedisTemplate<String, Object>) ApplicationContextHolder.getBean("redisTemplate");
}
}
}
}
@Override
public Cache getCache(String id) {
Cache cache = caches.get(id);
if (cache == null) {
cache = new RedisCache(id);
caches.put(id, cache);
}
return cache;
}
@Override
public Set<String> getCacheNames() {
return caches.keySet();
}
}
```
4. 配置MybatisPlus
```java
@Configuration
public class MybatisPlusConfig {
@Autowired
private RedisCacheFactory redisCacheFactory;
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() throws Exception {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
Properties props = new Properties();
props.setProperty("cache-enabled", "true");
props.setProperty("local-cache-scope", "STATEMENT");
props.setProperty("local-cache-ref", "local-cache");
// 自定义RedisCacheFactory
props.setProperty("second-level-cache-enabled", "true");
props.setProperty("second-level-cache-type", "org.apache.ibatis.cache.decorators.RedisCache");
interceptor.addInnerInterceptor(new CacheInterceptor(props, redisCacheFactory));
return interceptor;
}
}
```
完成上述步骤后,即可在MybatisPlus中使用自定义的Redis二级缓存。