java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Spring 缓存在项目使用

Spring 缓存在项目中的使用详解

作者:扛麻袋的少年

Spring 缓存机制,Cache接口为缓存的组件规范定义,包扩缓存的各种操作(添加缓存、删除缓存、修改缓存等),本文给大家介绍Spring 缓存在项目中的使用,感兴趣的朋友一起看看吧

在上文介绍了 JSR-107 规范 后, 本文来介绍一下 Spring 缓存机制相关内容。

1.Spring 缓存机制介绍

       Spring 从 3.1开始,针对缓存定义了org.springframework.cache.Cache
org.springframework.cache.CacheManager接口,来统一不同的缓存技术。
并支持使用 JCache(JSR-107规范)注解来简化项目的开发。

       Spring 的缓存机制非常灵活,可以对容器中任意 Bean 或者 Bean 的方法进行缓存,因此这种缓存机制可以在 JavaEE 应用的任何层次上进行缓存。在缓存的具体实现上,Spring 缓存底层也是借助其他缓存工具来实现的,例如 EhCache(Hibernate缓存工具),上层则以统一 API 编程。

       Spring 缓存机制,Cache接口为缓存的组件规范定义,包扩缓存的各种操作 (添加缓存、删除缓存、修改缓存等) 。在 Cache 接口下,Spring 为其提供了各种 xxxCache 的实现。如:RedisCacheEhCacheCacheConcurrentMapCache等;

2.Spring 缓存用到的概念

Ⅰ.两个接口

Ⅱ.三个注解(方法层次)

Ⅲ.一个注解(功能层次)

3.工作原理

       每次调用需要缓存功能的方法时,Spring 都会检查指定参数的指定的目标方法是否已经被调用过;如果有就直接从缓存中获取方法调用后的结果,如果没有就调用方法并缓存结果后返回给用户。下次调用直接从缓存中获取。

4.缓存在项目中的使用

       我们来准备一个环境,使用 Spring Boot + MyBatis 框架,来展示一下缓存在项目中的使用。此处就不啰嗦环境的搭建过程了,直接来介绍 Cache 缓存在项目中的使用。如需项目demo,请跳转至文末代码部分。

Ⅰ.使用@EnableCaching 开启基于注解的缓存

@SpringBootApplication
@MapperScan("com.example.cache.mapper")
@EnableCaching //开启基于注解的缓存
public class CacheApplication {
    public static void main(String[] args) {
        SpringApplication.run(CacheApplication.class, args);
    }
}

Ⅱ.来个Controller

提示:此处的3个方法分别对应 @Cacheable、@CachePut、@CacheEvict 三个注解,接下来一一测试

@RestController
public class UserController {
    @Autowired
    UserService userService;
	/**
	 * 根据id获取用户信息(主要针对 @Cacheable 注解介绍)
	 */
    @GetMapping("/user/{id}")
    public User user(@PathVariable("id") Integer id){
        User user = userService.getUser(id);
        return user;
    }
    /**
     * 更新用户信息(主要针对 @CachePut 注解介绍)
     */
    @PutMapping("/user")
    public User updateUser(User user) {
        User upUser = userService.updateUser(user);
        return upUser;
    }
    /**
     * 根据id删除用户数据(主要针对 @CacheEvict 注解介绍)
     */
    @DeleteMapping("/user/{id}")
    public void deleteUser(@PathVariable("id") Integer id) {
        userService.deleteUser(id);
    }
}

Ⅲ.在需要缓存的方法上,添加@Cacheable注解,表示该方法需要被缓存

public interface UserService {
    /**
     * 根据ID获取用户信息
     * 此处 value 为 @Cacheable 属性,该注解还有很多属性,在 https://blog.csdn.net/lzb348110175/article/details/105349109 会有介绍,此处不做介绍
     * 缓存相关注解,也可以写到具体Service实现类上,此处写在了接口上
     */
    @Cacheable(value = "user"/*,key="#id"*/)
    User getUser(Integer id);
	/**
     * 更新用户信息
     */
    @CachePut(value = "user",key = "#user.id")
    User updateUser(User user);
	/**
	 * 根据ID删除用户
	 */
    @CacheEvict(value = "user", key = "#id")
    void deleteUser(Integer id);
}

Ⅳ.getUser()方法实现

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    /**
     * 根据ID获取用户信息
     */
    @Override
    public User getUser(Integer id) {
        log.info("用户"+id+"开始执行数据库查询");
        return userMapper.getUser(id);
    }
    /**
     * 更新用户信息
     */
    @Override
    public User updateUser(User user) {
        log.info("开始更新用户"+user.getId()+"的数据信息");
        userMapper.updateUser(user);
        return user;
    }
	/**
     * 删除用户(此处仅演示删除缓存数据,实际数据库数据不删除,方便演示)
     */
    @Override
    public void deleteUser(Integer id) {
		System.out.println("执行删除缓存数据操作");
    }
}

Ⅴ.测试@Cacheable缓存配置是否生效

       @Cacheable注解共有9个属性可配置,这些属性的配置可参考:@Cacheable注解属性介绍

@Cacheable注解相关属性介绍

       @Cacheable 注解提供的属性,如何配置可参考:@Cacheable注解属性介绍。

Ⅵ.测试@CachePut 是否会更新缓存

       测试结果如下:第一次请求,查询数据库返回 Mary;第二次请求,更新数据为Clark;第三次再次发送查询请求,在更新数据时缓存会同时被更改,由于缓存存在,所以不会调用数据库请求,返回的是修改后的缓存中的数据Clark。(切记:更新缓存时的 key 要与已经存在缓存中的数据 key 相同,否则缓存不会被更新。)

@CachePut 注解相关属性介绍

       @CachePut 注解提供的属性,如何配置可参考:@Cacheable注解属性介绍。(此处附的参考文章是正确的,我没有附错,因为它们配置都是一样紫的)

Ⅶ.测试@CacheEvict 是否会删除缓存

@CacheEvict 注解相关属性介绍

       @CacheEvict 注解提供的属性,如何配置可参考:@Cacheable注解属性介绍。(此处附的参考文章是正确的,我没有附错,因为它们配置都是一样紫的)(切记:删除缓存时的 key 要与已经存在缓存中的数据 key 相同,否则缓存不会被删除。)除此之外,@CacheEvict 注解还提供了额外两个属性:allEntriesbeforeInvocation。这两个属性如何使用,介绍如下:

Ⅷ.(了解)@Caching注解—应用于复杂缓存规则的指定

//可以使用@Caching 来解决满足项目开发的复杂缓存规则
@Caching(
	cacheable = {
        @Cacheable(value = "user",key = "#name")
    },
    put = {
        @CachePut(value = "user",key = "#result.id"),
        @CachePut(value = "user",key = "#result.email")
    },
    evict = {
        @CacheEvict(value = "user",key = "#name")
    }
)
User getUserByName(String name);

Ⅸ.(了解)@CacheConfig注解—应用于抽取当前类下缓存使用的公共配置

       比如 UserService 类下的所有操作,①缓存都是存在 key = “user” 下,②使用的 keyGenerator 都是我们自定义的,那么我们可以使用 @CacheConfig 注解来抽取缓存的公共配置,并将该注解标注在该 UserService类上即可。(公共配置支持:cacheNameskeyGeneratorcacheManagercacheResolver 4个属性的配置)

@CacheConfig(cacheNames="emp",cacheManager = "employeeCacheManager",keyGenerator = "myKeyGenerator",cacheResolver = "myCacheResolver") //抽取缓存的公共配置
public interface UserService{
	//代码省略
}

5.附上demo

     Spring cache 缓存使用demo

百度网盘下载地址:

链接: https://pan.baidu.com/s/1nxUOTAuQIHsiQRD7Bifi5w

提取码: 2jyz 

到此这篇关于Spring 缓存在项目中的使用的文章就介绍到这了,更多相关Spring 缓存在项目使用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文