发布于2021-05-30 19:52 阅读(1860) 评论(0) 点赞(15) 收藏(2)
针对热数据,一般我们会把他放到缓冲里,减轻数据库的压力,针对集群下的缓冲,一般我们会把缓冲放到redis里
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
</project>
package com.example.demo.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.time.Duration;
@EnableCaching
@Configuration
public class RedisConfig {
@Autowired
RedisConnectionFactory redisConnectionFactory;
@Bean
public CacheManager cacheManager() {
// 设置缓存有效期1小时
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofHours(1))
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer((new StringRedisSerializer())))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer((new GenericJackson2JsonRedisSerializer())))
.computePrefixWith((name)->name+":");
return RedisCacheManager
.builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory))
.cacheDefaults(redisCacheConfiguration).build();
}
}
package com.example.demo.dto;
import java.io.Serializable;
public class HelloRespDTO implements Serializable {
private String name;
private String address;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
package com.example.demo.service;
import com.example.demo.dto.HelloRespDTO;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
@Service
public class HelloService {
@Cacheable(cacheNames = "employee",key = "'detail'+#id")
public HelloRespDTO helloCache(String id){
HelloRespDTO hello = new HelloRespDTO();
hello.setName("ben");
hello.setAddress("广州市白云区");
hello.setAge(18);
return hello;
}
@CacheEvict(value = "employee",key = "'detail'+#id")
public HelloRespDTO helloClear(String id){
HelloRespDTO hello = new HelloRespDTO();
hello.setName("ben");
hello.setAddress("广州市白云区");
hello.setAge(18);
return hello;
}
}
package com.example.demo.controller;
import com.example.demo.dto.HelloRespDTO;
import com.example.demo.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@GetMapping("/hello")
public HelloRespDTO hello(){
String id="1";
HelloRespDTO helloRespDTO = helloService.helloCache(id);
return helloRespDTO;
}
@GetMapping("/hello2")
public HelloRespDTO helloClear(){
String id="1";
HelloRespDTO helloRespDTO = helloService.helloClear(id);
return helloRespDTO;
}
}
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
访问网址:
http://localhost:8889/hello/hello
redis里面已经有数据了
下次走helloCache接口时,从redis里拿取
https://gitee.com/null_751_0808/spring-boot-demo
分支:origin/spring-boot2-cache-manager
j2cache.properties
这里不放出来了,主要是配置一级缓存是什么,二级缓存是什么,redis广播通知节点的方式,想看的可以去官网看,或者下载此demo看
我的demo配置
一级缓存用了caffeine,二级缓存用了lettuce(连接redis的)
application.yml 配置对应j2cache
server:
port: 8889
servlet:
context-path: /hello
spring:
application:
name: hello
redis:
host: 127.0.0.1
port: 6379
password:
database: 2
cache:
type: none
j2cache:
config-location: /j2cache.properties
redis-client: lettuce
open-spring-cache: true
caffeine.properties
#########################################
# Caffeine configuration
# [name] = size, xxxx[s|m|h|d]
#########################################
default = 1000, 30m
package com.example.demo.service;
import com.example.demo.dto.HelloRespDTO;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
@Service
public class HelloService {
//加缓存
@Cacheable(value="employee",key = "'detail'+#id")
public HelloRespDTO helloCache(String id){
HelloRespDTO hello = new HelloRespDTO();
hello.setName("ben");
hello.setAddress("广州市白云区");
hello.setAge(18);
return hello;
}
//清缓存
@CacheEvict(value="employee",key = "'detail'+#id")
public HelloRespDTO helloClear(String id){
HelloRespDTO hello = new HelloRespDTO();
hello.setName("ben");
hello.setAddress("广州市白云区");
hello.setAge(18);
return hello;
}
}
package com.example.demo.dto;
import java.io.Serializable;
public class HelloRespDTO implements Serializable {
private String name;
private String address;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
package com.example.demo.controller;
import com.example.demo.dto.HelloRespDTO;
import com.example.demo.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@GetMapping("/hello")
public HelloRespDTO hello(){
String id="1";
HelloRespDTO helloRespDTO = helloService.helloCache(id);
return helloRespDTO;
}
@GetMapping("/hello2")
public HelloRespDTO helloClear(){
String id="1";
HelloRespDTO helloRespDTO = helloService.helloClear(id);
return helloRespDTO;
}
}
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
访问
http://localhost:8889/hello/hello
从源码这里可以看到j2cache缓存
AbstractCacheInvoker doGet方法
https://gitee.com/null_751_0808/spring-boot-demo/tree/spring-boot2-j2cache/
分支:origin/spring-boot2-j2cache
redis订阅与发布
SpringBoot中Cache缓存的使用
@Cacheable缓存解决双冒号::问题
原文链接:https://blog.csdn.net/qq_42745404/article/details/117376963
作者:Nxnndn
链接:http://www.phpheidong.com/blog/article/86949/2b280adc5ddf93dd0d4b/
来源:php黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 php黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-4
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!