第一步:配置redis

  这里使用的是yml类型的配置文件

 mybatis:
  mapper-locations: classpath:mapping/*.xml
spring:
  datasource:
    name: miaosha
    url: jdbc:mysql://127.0.0.1:3306/miaosha?serverTimezone=UTC
    username: root
    password: 1234
    type: com.alibaba.druid.pool.DruidDataSource #数据源
    driverClassName: com.mysql.jdbc.Driver
  redis:
    host: 10.0.75.1 #地址
    port: 6379 #端口号
    timeout: 20000 #连接超时时间
  cache: #缓存类型
    type: redis

第二步:在启动类上添加 @EnableCaching 注解

 @SpringBootApplication(scanBasePackages = {"com.miaoshaproject"})
@MapperScan("com.miaoshaproject.dao")
@EnableCaching
public class App { public static void main( String[] args ) {
ConfigurableApplicationContext run = SpringApplication.run(App.class, args);
}
}

第三步:在需要缓存的方法上添加 @Cacheable 注解

@Service
@CacheConfig(cacheNames = {"itemService"})
public class ItemServiceImpl implements ItemService { @Override
@Cacheable(value = {"item"},key ="#p0")
public String getItemById(Integer id) {
String name = "123";
return name;
}
}

注:关于springboot缓存名的说明:

  使用SpringBoot缓存必须配置名字可以使用@CacheConfig(cacheNames = {"itemService"})在类上配置该类公用的名字,也可以使用@Cacheable(value=”item”)在方法上配置只适用于该方法的名字。如果类和方法上都有配置,以方法上的为准。

  springBoot会自动拼装缓存名,规则是:配置的名字+两个冒号+方法的实参;

注:关于@CacheConfig和@Cacheable注解的说明:

  @Cacheable(value=”item”),这个注释的意思是,当调用这个方法的时候,会从一个名叫 item 的缓存中查询,如果没有,则执行实际的方法(即查询数据库),并将执行的结果存入缓存中,否则返回缓存中的对象。
  在上面代码示例中@Cacheable注解设置了两个参数一个是value,一个是key。key的值"#p0"在执行过程中会被getItemById方法的实参所替换,例如id的值3 那么缓存的名字就会是"item::3";如果不设置key,系统会自动也会是这个效果。

  

  如果是无参方法:

  @CacheConfig is a class-level annotation that allows to share the cache names,如果你在你的方法写别的名字,那么依然以方法的名字为准。

SpringBoot缓存之redis--最简单的使用方式的更多相关文章

  1. Spring Boot 2.x 缓存应用 Redis注解与非注解方式入门教程

    Redis 在 Spring Boot 2.x 中相比 1.5.x 版本,有一些改变.redis 默认链接池,1.5.x 使用了 jedis,而2.x 使用了 lettuce Redis 接入 Spr ...

  2. SpringBoot缓存技术

    一.SpringBoot整合Ehhcache 添加maven依赖 <dependency> <groupId>org.springframework.boot</grou ...

  3. SpringBoot使用@Cacheable实现最简单的Redis缓存

    前言 之前我们使用过RedisTemplate来实现redis缓存,然后使用工具类来实现操作redis的存储.这样的方式好处是很自由,但是还不是最简单的处理方式.对于一些简单的应用来说,其实redis ...

  4. SpringBoot中Shiro缓存使用Redis、Ehcache

    在SpringBoot中Shiro缓存使用Redis.Ehcache实现的两种方式实例 SpringBoot 中配置redis作为session 缓存器. 让shiro引用 本文是建立在你是使用这sh ...

  5. 在springboot中使用redis缓存,将缓存序列化为json格式的数据

    背景 在springboot中使用redis缓存结合spring缓存注解,当缓存成功后使用gui界面查看redis中的数据 原因 springboot缓存默认的序列化是jdk提供的 Serializa ...

  6. springboot 2 集成 redis 缓存 序列化

    springboot 缓存 为了实现是在数据中查询数据还是在缓存中查询数据,在application.yml 中将mybatis 对应的mapper 包日志设置为debug . spring: dat ...

  7. SpringBoot缓存篇Ⅱ --- 整合Redis以及序列化机制

    一.Redis环境搭建 系统默认是使用ConcurrentMapCacheManager,然后获取和创建ConcurrentMapCache类型的缓存组件,再将数据保存在ConcurrentMap中 ...

  8. SpringBoot缓存管理(二) 整合Redis缓存实现

    SpringBoot支持的缓存组件 在SpringBoot中,数据的缓存管理存储依赖于Spring框架中cache相关的org.springframework.cache.Cache和org.spri ...

  9. 由浅入深学习springboot中使用redis

    很多时候,我们会在springboot中配置redis,但是就那么几个配置就配好了,没办法知道为什么,这里就详细的讲解一下 这里假设已经成功创建了一个springboot项目. redis连接工厂类 ...

随机推荐

  1. centos下安装python3.7.0以上版本时报错ModuleNotFoundError: No module named '_ctypes'

    centos下安装python3.7.0以上版本时报错ModuleNotFoundError: No module named '_ctypes'的解决办法 3.7版本需要一个新的包libffi-de ...

  2. C语言编译器CL.exe

    下载地址CL.7z版权问题:仅供学习交流,请于24小时内删除,本人不承担版权问题… 基本使用: 1.解压缩,例如解压缩到E盘根目录下 2.打开cmd命令行界面(快捷方式win+R输入cmd回车)cd命 ...

  3. BZOJ.5092.[Lydsy1711月赛]分割序列(高维前缀和)

    题目链接 \(Description\) \(Solution\) 首先处理\(a_i\)的前缀异或和\(s_i\).那么在对于序列\(a_1,...,a_n\),在\(i\)位置处分开的价值为:\( ...

  4. jade模版js中接收express的res.render

    router: router.get('/', function(req, res, next) { res.render('index', { title:{name:'aaa',age:23} } ...

  5. How to change Eclipse loading image

    Eclipse IDE has many customize components, the splash welcome image (purple color loading image) is ...

  6. select应用

    服务端源码 #!/usr/bin/env python3 # -*- coding: utf-8 -*- """ @author: zengchunyun "& ...

  7. mysql完整性约束

    第一:完整性约束介绍 为了防止不符合规范的数据进入数据库,在用户对数据进行插入.修改.删除等操作时,DBMS自动按照一定的约束条件对数据进行监测,使不符合规范的数据不能写入数据库,以确保数据库中存储的 ...

  8. ECMA Script 6_对象的扩展

    对象 1. ES6 允许直接写入变量和函数,作为对象的属性和方法 const foo = 'bar'; /*****************属性的优化********************/ con ...

  9. 10.1jihe

    两种操作,1是加入数字,二是找最接近的 用set或者平衡二叉树就好了 只写了二叉树的,套版子就好 #include<bits/stdc++.h> #define sf scanf #def ...

  10. 一键安装metasploit(linux,os x)

    curl https://raw.githubusercontent.com/rapid7/metasploit-omnibus/master/config/templates/metasploit- ...