博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringBoot入门之缓存
阅读量:6942 次
发布时间:2019-06-27

本文共 3113 字,大约阅读时间需要 10 分钟。

hot3.png

缓存对减轻服务器压力相当重要,这一篇简单记录一下。

缓存这一块,主要用到了这4个注解:@EnableCaching, @Cacheable,@CachePut,@CacheEvict 。
其中@EnableCaching告诉Spring框架开启缓存能力,@Cacheable来配置访问路径是否缓存,@CachePut来更新缓存,@CacheEvict来清理缓存,具体用法看下面的代码。
1.开启Spring的缓存功能

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cache.annotation.EnableCaching;@SpringBootApplication@EnableCaching//开启缓存public class Application {    public static void main(String[] args) {        SpringApplication.run(Application.class, args);    }}

2.对访问路径(URL)配置缓存信息

@GetMapping("/{id}")    @Cacheable("users")//设置缓存用户信息,缓存的名称为users    public User getUserInfo(@PathVariable int id) {        User user = new User();        user.setId(id);        int random=new Random().nextInt(999);        user.setName("user " + random);        System.out.println("random is "+random);        return user;    }

3.更新缓存信息

@PostMapping("/update")    @CachePut(value ="users",key = "#user.getId()")//更新指定的用户缓存信息    public boolean updateUserInfo( User user){        System.out.println("updateUserInfo "+user.toString());        return true;    }

4.清除缓存信息

@PostMapping("/remove")    @CacheEvict(value = "users",key = "#user.getId()")//清除指定的用户缓存信息    public boolean removeUserInfoCache(@RequestBody User user){        System.out.println("removeUserInfoCache "+user.toString());        return true;    }    @PostMapping("/remove/all")    @CacheEvict(value = "users",allEntries = true)//清除所有的用户缓存信息    public boolean removeAllUserInfoCache(){        System.out.println("removeAllUserInfoCache ");        return true;    }

下面放一个完整的缓存类demo

package com.spring.cache;import com.spring.beans.User;import org.springframework.cache.annotation.CacheEvict;import org.springframework.cache.annotation.CachePut;import org.springframework.cache.annotation.Cacheable;import org.springframework.web.bind.annotation.*;import java.util.Random;/** * Created by zhangyi on 2017/4/13. * * 缓存demo * */@RestController@RequestMapping("/cache/user")public class CacheController {    @GetMapping("/{id}")    @Cacheable("users")//设置缓存用户信息,缓存的名称为users    public User getUserInfo(@PathVariable int id) {        User user = new User();        user.setId(id);        int random=new Random().nextInt(999);        user.setName("user " + random);        System.out.println("random is "+random);        return user;    }    @PostMapping("/update")    @CachePut(value ="users",key = "#user.getId()")//更新指定的用户缓存信息    public boolean updateUserInfo( User user){        System.out.println("updateUserInfo "+user.toString());        return true;    }    @PostMapping("/remove")    @CacheEvict(value = "users",key = "#user.getId()")//清除指定的用户缓存信息    public boolean removeUserInfoCache(@RequestBody User user){        System.out.println("removeUserInfoCache "+user.toString());        return true;    }    @PostMapping("/remove/all")    @CacheEvict(value = "users",allEntries = true)//清除所有的用户缓存信息    public boolean removeAllUserInfoCache(){        System.out.println("removeAllUserInfoCache ");        return true;    }}

转载于:https://my.oschina.net/u/2606060/blog/879155

你可能感兴趣的文章
兼容FF IE的回车事件
查看>>
scp传输文件(好技能)
查看>>
冒泡排序,快速排序, 二叉树,一致性哈希
查看>>
使用tornado和angularjs搭建网站
查看>>
linux 文件类型 文件权限
查看>>
转:cocos2d-x 瓦片地图的黑线及地图抖动解决方案
查看>>
静态变量的陷阱
查看>>
redis原理
查看>>
floyd 记录最短路径路线
查看>>
HashMap原理源码分析
查看>>
字符串作业1
查看>>
如何学习新框架
查看>>
【NOI2018模拟5】三角剖分Bsh
查看>>
【JSWC2019】 小X的咒语
查看>>
sdut 1451 括号东东 (dp或模拟)
查看>>
POJ1002 487-3279
查看>>
呼号 (CALL SIGN)
查看>>
Visual Studio 2012+jQuery-1.7.1
查看>>
Java对象的序列化和反序列化(转)
查看>>
Appium 在 Android UI 测试中的应用
查看>>