springboot对缓存技术的支持

spring缓存支持

spring定义了CacheManagerCache接口用来统一不同的缓存技术。CacheManager是Spring提供的各种缓存技术抽象接口,Cache接口包含缓存的各种操作。
针对不同的缓存技术需要实现不同的CacheManager

CacheManager描述
EhCacheCacheManager使用EhCache作为缓存技术
GuavaCacheManager使用Google的GuavaCache作为缓存技术
RedisCacheManager使用Redis作为缓存技术

每种缓存技术都有很多的额外配置,但配置cacheManager是必不可少的。

声明式缓存注解

spring提供了4个注解来声明缓存规则

注解解释
@Cacheable在方法执行前spring先查看缓存中是否有数据,如果有数据,则直接返回缓存数据;若没有数据,调用方法并将方法返回值放到缓存
@CachePut无论怎样都会将方法的返回值放到缓存中
@CacheEvict将一条或多条数据从缓存中删除
@Caching可以该注解可以自定义组合多个注解

@Cacheable、@CachePut、@CacheEvict都有value属性,指定的是要使用的缓存名称;key属性指定的是数据在缓存中的key;

开启声明式缓存支持

@Configuration
@EnableCaching
public class AppConfig{}
以上缓存规则应用举例

springboot环境下,使用缓存技术只需在项目中导入相关缓存技术的依赖包,并在配置类中使用@EnableCaching开启缓存支持即可。

1、@Cacheable 的作用 主要针对方法配置,能够根据方法的请求参数对其结果进行缓存。
当调用这个方法的时候,会先从一个value为 accountCache 的缓存中查询,如果没有再执行目标方法(即查询数据库),并将执行的结果存入缓存中,否则返回缓存中的对象。

@Cacheable(value="accountCache")// 使用了一个缓存命名空间 accountCache,key为userName
public Account getAccountByName(String userName) {
   // 方法内部实现不考虑缓存逻辑,直接实现业务
   System.out.println("real query account."+userName);
   return getFromDB(userName);
}

2、@CacheEvict 的作用 主要针对方法配置,能够根据一定的条件对缓存进行清空。可以使用在类或方法上。

// 根据account.name清空accountCache 缓存
@CacheEvict(value="accountCache",key="#account.getName()")
public void updateAccount(Account account) {
   updateDB(account);
}
// 清空accountCache 缓存
@CacheEvict(value="accountCache",allEntries=true)
public void reload() {
   reloadAll()
}
// 根据条件清空缓存 accountCache
@Cacheable(value="accountCache",condition="#userName.length() <=4")
public Account getAccountByName(String userName) {
 // 方法内部实现不考虑缓存逻辑,直接实现业务
 return getFromDB(userName);
}

3、@CacheConfig
每个@Cacheable(value=“accountCache”)都要写一遍,很麻烦, 使用@CacheConfig这个配置声明一次就够了。

@CacheConfig("books")
public class BookRepositoryImpl implements BookRepository {
 
  @Cacheable
  public Book findBook(ISBN isbn) {...}
}

4、@Caching组合多个注解

@Caching(put = {
@CachePut(value = "user", key = "#user.id"),
@CachePut(value = "user", key = "#user.username"),
@CachePut(value = "user", key = "#user.email")
})
public User save(User user) {}

5、上面的写法不易理解,可以使用自定义注解

@Caching(put = {
@CachePut(value = "user", key = "#user.id"),
@CachePut(value = "user", key = "#user.username"),
@CachePut(value = "user", key = "#user.email")
})
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface UserSaveCache {
}

//下面这样引用显得代码很干净
@UserSaveCache
public User save(User user){}

6、@CachePut缓存新增的或更新的数据到缓存,其中缓存名称为accountCache,数据的key是account.name

@CachePut(value="accountCache",key="#account.getName()")
public Account updateAccount(Account account) {
  return updateDB(account);
}

7、如何设置缓存有效期

// 对基于注解的Redis缓存数据统一设置有效期为1分钟,单位毫秒
spring.cache.redis.time-to-live=60000
切换缓存技术

切换缓存技术主要是引入相关依赖或者配置。
1、EhCache

<dependency>
    <groupId>net.sf.ehcache</groupId>
    <artifactId>ehcache</artifactId>
    <version>2.10.6</version>
</dependency>

Ehcache所需的配置文件ehcache.xml只需放在类路径下,springboot会自动扫描
springboot会自动给我们配置EhCacheCacheManager的Bean
2、Guava

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>27.0-jre</version>
</dependency>

springboot会自动给我们配置GuavaCacheManager的Bean
3、Redis

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    <version>2.1.3.RELEASE</version>
</dependency>

springboot会自动配置RedisCacheManager以及RedisTemplate的Bean

StringRedisTemplate应用

spring封装了RedisTemplate来操作redis,springboot默认支持StringRedistTemplate,他继承了RedisTemplate,可以直接引入,适合key-value都是字符串的情况。两者的数据序列化策略不同,前者是string,后者为jdk的序列化

  • opsFor
redisTemplate.opsForValue().set("name","zhangsan")
redisTemplate.opsForValue().get("name")
  • opsForList
redisTemplate.opsForList().leftpush("username","zhangsan")
redisTemplate.opsForList().leftpush("username","lisi")

opsForList还有leftPush、leftPop、rightPush、rightPop等操作

  • opsForHash
redisTemplate.opsForHash().put("cache","username","zhangsan")
String username = (String)redisTemplate.opsForHash().get("cache","username")
  • 其他还有opsForSet、opsForZSet操作集合

对redis集群的支持

springboot对redis集群的支持不是很友好,可以通过bean的方式自定义注入

@Data
@ConfigurationProperties(prefix = "redis.cache")
public class RedisProperties {
    private int expireTime;
    private String clusterNames;
    private int commandTime;
}
@Configuration
public class RedisClusterConfig {
    @Autowired
    private RedisProperties redisProperties;

    @Bean
    public JedisCluster getJedisCluster() {
        List<String> list = Arrays.asList(redisProperties.getClusterNames().split(","));
        HashSet<HostAndPort> nodes = new HashSet<>();
        list.stream().forEach(node -> {
            String[] ipPortPair = node.split(":");
            nodes.add(new HostAndPort(ipPortPair[0].trim(), Integer.valueOf(ipPortPair[1].trim())));
        });
        return new JedisCluster(nodes, redisProperties.getCommandTime());
    }

}

然后在service实现类里引入即可。

@Autowire
private JedisCluster jedisCluster;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot提供了对缓存支持,可以方便地集成各种缓存技术,如Ehcache、Redis等。使用Spring Boot缓存功能,可以有效地减少数据库访问,提升系统的性能。 要使用Spring Boot缓存功能,首先要在项目的依赖添加相应的缓存技术依赖。例如,如果要使用Ehcache作为缓存技术,可以添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version>2.10.6</version> </dependency> ``` 接着,在Spring Boot配置文件配置使用缓存技术。例如,对于Ehcache,可以在`application.properties`文件添加以下配置: ```properties spring.cache.type=ehcache ``` 然后,在使用缓存的方法上添加`@Cacheable`注解,指定缓存的名称和缓存的键。例如: ```java @Cacheable(value = "userCache", key = "#id") public User getUserById(Long id) { // 从数据库获取用户信息 return userRepository.findById(id).orElse(null); } ``` 这样,在第一次调用`getUserById`方法时,会将查询结果放入名为"userCache"的缓存,并以`id`作为键。之后再次调用该方法时,如果缓存已经存在相应的数据,则直接从缓存获取,不再访问数据库。 除了`@Cacheable`注解外,Spring Boot还提供了其他的缓存相关注解,如`@CachePut`用于更新缓存,`@CacheEvict`用于清除缓存等。通过合理地使用这些注解,可以实现更加灵活和高效的缓存操作。 要注意的是,配置缓存技术使用缓存注解的具体方式可能因不同的缓存技术而有所不同。在实际使用要根据具体的求和技术选择合适的缓存配置注解使用方式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值