SpringBoot入门(五)Redis数据缓存
来源:互联网 发布:线切割机床的编程格式 编辑:程序博客网 时间:2024/06/06 05:14
Redis了解
一、缓存的应用场景
什么是缓存?
在互联网场景下,尤其 2C 端大流量场景下,需要将一些经常展现和不会频繁变更的数据,存放在存取速率更快的地方。缓存就是一个存储器,在技术选型中,常用 Redis 作为缓存数据库。缓存主要是在获取资源方便性能优化的关键方面。
Redis 是一个高性能的 key-value 数据库。GitHub 地址:https://github.com/antirez/redis 。Github 是这么描述的:
Redis is an in-memory database that persists on disk. The data model is key-value, but many different kind of values are supported: Strings, Lists, Sets, Sorted Sets, Hashes, HyperLogLogs, Bitmaps.
缓存的应用场景有哪些呢?
比如常见的电商场景,根据商品 ID 获取商品信息时,店铺信息和商品详情信息就可以缓存在 Redis,直接从 Redis 获取。减少了去数据库查询的次数。但会出现新的问题,就是如何对缓存进行更新?这就是下面要讲的。
二、更新缓存的策略
参考《缓存更新的套路》http://coolshell.cn/articles/17416.html,缓存更新的模式有四种:Cache aside, Read through, Write through, Write behind caching。
这里我们使用的是 Cache Aside 策略,从三个维度:(摘自 耗子叔叔博客)
失效:应用程序先从cache取数据,没有得到,则从数据库中取数据,成功后,放到缓存中。
命中:应用程序从cache中取数据,取到后返回。
更新:先把数据存到数据库中,成功后,再让缓存失效。
大致流程如下:
获取商品详情举例
a. 从商品 Cache 中获取商品详情,如果存在,则返回获取 Cache 数据返回。
b. 如果不存在,则从商品 DB 中获取。获取成功后,将数据存到 Cache 中。则下次获取商品详情,就可以从 Cache 就可以得到商品详情数据。
c. 从商品 DB 中更新或者删除商品详情成功后,则从缓存中删除对应商品的详情缓存
Redis安装及密码修改
redis安装及密码修改
redis修改密码(1)与2相互参考
redis修改密码(2)
项目配置
pom.xml中添加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId></dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId></dependency><dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId></dependency>
application.yml
spring: profiles: active: - dev datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/dbperson username: root password: 123456 jpa: hibernate: ddl-auto: create hbn2ddl: auto: update show-sql: true redis:#redis数据库名称 从0到15,默认为db0 database: 1 host: 127.0.0.1 password: 123456#redis服务器连接端口号 port: 6379#redis连接池设置 pool: max-active: 8 max-idle: 8 max-wait: -1 min-idle: 0 timeout: 60000
StringRedisController
@RestControllerpublic class StringRedisController { protected static Logger logger=LoggerFactory.getLogger(StringRedisController.class); @Resource StringRedisTemplate stringRedisTemplate; @Resource(name="stringRedisTemplate") ValueOperations<String, String> valOpsStr; @RequestMapping("set") public String setkayAndValue(String key,String value) { logger.info("访问set:key={},value={}",key,value); valOpsStr.set(key, value); return "key:"+key+",value:"+value; } @RequestMapping("get") public String getKey(String key) { logger.info("访问get:key{}",key); return valOpsStr.get(key); }}
运行redis+运行项目
http://localhost:8088/set?key=123&&value=456
使用示例:
Fruit
public class Fruit implements Serializable { private static final long serialVersonUID=1L; private Integer id; private String name; private Integer price; public Fruit() { super(); } public Fruit(Integer id, String name, Integer price) { super(); this.id = id; this.name = name; this.price = price; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getPrice() { return price; } public void setPrice(Integer price) { this.price = price; } }
FruitDao
@Repositorypublic class FruitDao { @Autowired RedisTemplate<Object, Object> redisTemplate; @Resource(name="redisTemplate") ValueOperations<Object,Object> valOps; public void save(Fruit fruit) { valOps.set(fruit.getId(), fruit); } public Fruit getFruits(Integer id){ return (Fruit) valOps.get(id); }}
FruitRedisController
@RestControllerpublic class FruitRedisController { protected static Logger logger=LoggerFactory.getLogger(FruitRedisController.class); @Autowired FruitDao fruitDao; @RequestMapping(value="setFruit") public void setFruit(Integer id,String name,Integer price) { logger.info("id={},name={},price={}",id,name,price); Fruit fruit=new Fruit(id,name,price); fruitDao.save(fruit); } @RequestMapping(value="getFruit") public Fruit getFruits(Integer id){ return fruitDao.getFruits(id); }}
运行项目,浏览器输入:
http://localhost:8088/setFruit?id=1&&name=苹果&&price=4
http://localhost:8088/setFruit?id=2&&name=香蕉&&price=3
http://localhost:8088/setFruit?id=3&&name=橘子&&price=3
插入数据后进行查询
http://localhost:8088/getFruit?id=1
- SpringBoot入门(五)Redis数据缓存
- springboot redis 数据缓存
- SpringBoot--使用redis缓存(1)
- SpringBoot--使用redis做缓存(2)
- springboot+redis作缓存
- springboot整合redis缓存
- Springboot整合Redis缓存
- SpringBoot--使用Redis缓存
- SpringBoot--使用Redis缓存
- SpringBoot入门(五)数据库操作入门
- 6 SpringBoot数据缓存Cache [Guava和Redis实现]
- springboot缓存篇(二)-redis 做缓存
- SpringBoot入门系列: redis(四)
- SpringBoot的入门搭建(五)
- SpringBoot学习笔记(6) SpringBoot数据缓存Cache [Guava和Redis实现]
- SpringBoot学习笔记(6) SpringBoot数据缓存Cache [Guava和Redis实现]
- Hibernate入门五(缓存)
- springboot【16】缓存之Redis
- leetcode 547. Friend Circles
- 由海量数据想到的一点小事
- String比较常用的方法
- MySQL5.7完全卸载
- Oracle数据库,数字强制显示2位小数
- SpringBoot入门(五)Redis数据缓存
- php批量获取首字母
- MMU和TLB
- Android开发,关于ZxUtils的使用(二)
- awk工具
- iOS隐藏系统导航栏左侧返回按钮上的标题
- 利用WNet系列函数枚举和映射网络资源
- ES6基础之Class的继承
- order by 2