Skip to content

Commit 36c5060

Browse files
committed
layering cache Spring boot demo
1 parent 6290e89 commit 36c5060

File tree

7 files changed

+71
-61
lines changed

7 files changed

+71
-61
lines changed

spring-boot-student-cache-redis-caffeine/src/main/java/com/xiaolyuh/cache/listener/RedisMessageListener.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
*/
2323
@Component
2424
public class RedisMessageListener extends MessageListenerAdapter {
25-
private static final Logger log = LoggerFactory.getLogger(RedisPublisher.class);
25+
private static final Logger logger = LoggerFactory.getLogger(RedisPublisher.class);
2626

2727
@Autowired
2828
CacheManager cacheManager;
@@ -31,7 +31,7 @@ public class RedisMessageListener extends MessageListenerAdapter {
3131
public void onMessage(Message message, byte[] pattern) {
3232
super.onMessage(message, pattern);
3333
ChannelTopicEnum channelTopic = ChannelTopicEnum.getChannelTopicEnum(new String(message.getChannel()));
34-
log.info("redis消息订阅者接收到频道【{}】发布的消息。消息内容:{}", channelTopic.getChannelTopicStr(), message.toString().getBytes());
34+
logger.info("redis消息订阅者接收到频道【{}】发布的消息。消息内容:{}", channelTopic.getChannelTopicStr(), message.toString().getBytes());
3535
// 解析订阅发布的信息,获取缓存的名称和缓存的key
3636
String ms = new String(message.getBody());
3737
@SuppressWarnings("unchecked")
@@ -48,17 +48,17 @@ public void onMessage(Message message, byte[] pattern) {
4848
case REDIS_CACHE_DELETE_TOPIC:
4949
// 获取一级缓存,并删除一级缓存数据
5050
((LayeringCache) cache).getFirstCache().evict(key);
51-
log.info("删除一级缓存{}数据,key:{}", cacheName, key.toString().getBytes());
51+
logger.info("删除一级缓存{}数据,key:{}", cacheName, key.toString().getBytes());
5252
break;
5353

5454
case REDIS_CACHE_CLEAR_TOPIC:
5555
// 获取一级缓存,并删除一级缓存数据
5656
((LayeringCache) cache).getFirstCache().clear();
57-
log.info("清除一级缓存{}数据", cacheName);
57+
logger.info("清除一级缓存{}数据", cacheName);
5858
break;
5959

6060
default:
61-
log.info("接收到没有定义的订阅消息频道数据");
61+
logger.info("接收到没有定义的订阅消息频道数据");
6262
break;
6363
}
6464

spring-boot-student-cache/src/main/java/com/xiaolyuh/SpringBootStudentCacheApplication.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
import org.springframework.cache.annotation.EnableCaching;
66

77
@SpringBootApplication
8-
@EnableCaching// 开启缓存,需要显示的指定
8+
// 开启缓存,需要显示的指定
9+
@EnableCaching
910
public class SpringBootStudentCacheApplication {
1011

11-
public static void main(String[] args) {
12-
SpringApplication.run(SpringBootStudentCacheApplication.class, args);
13-
}
12+
public static void main(String[] args) {
13+
SpringApplication.run(SpringBootStudentCacheApplication.class, args);
14+
}
1415
}

spring-boot-student-layering-cache/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@
5555

5656
<dependency>
5757
<groupId>com.xiaolyuh</groupId>
58-
<artifactId>layering-cache-aspectj</artifactId>
59-
<version>1.0.0</version>
58+
<artifactId>layering-cache-starter</artifactId>
59+
<version>1.0.0-SNAPSHOT</version>
6060
</dependency>
6161

6262
</dependencies>

spring-boot-student-layering-cache/src/main/java/com/xiaolyuh/SpringBootStudentLayeringCacheApplication.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
55
import org.springframework.boot.autoconfigure.SpringBootApplication;
6-
import org.springframework.cache.annotation.EnableCaching;
6+
import org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration;
77

88
@SpringBootApplication
99
public class SpringBootStudentLayeringCacheApplication {

spring-boot-student-layering-cache/src/main/java/com/xiaolyuh/config/RedisConfig.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import com.xiaolyuh.serializer.StringRedisSerializer;
99
import org.springframework.context.annotation.Bean;
1010
import org.springframework.context.annotation.Configuration;
11+
import org.springframework.context.annotation.Primary;
12+
import org.springframework.data.redis.cache.RedisCacheManager;
1113
import org.springframework.data.redis.connection.RedisConnectionFactory;
1214
import org.springframework.data.redis.core.RedisTemplate;
1315
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
@@ -56,5 +58,11 @@ public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisC
5658
return redisTemplate;
5759
}
5860

61+
@Bean
62+
@Primary
63+
public RedisCacheManager cacheManager(RedisTemplate<String, Object> redisTemplate) {
64+
RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
65+
return cacheManager;
66+
}
5967

6068
}

spring-boot-student-layering-cache/src/main/java/com/xiaolyuh/entity/Person.java

Lines changed: 49 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,71 +4,72 @@
44
import javax.persistence.GeneratedValue;
55
import javax.persistence.Id;
66
import javax.persistence.NamedQuery;
7+
import java.io.Serializable;
78

89
@Entity // @Entity注解指明这是一个和数据库表映射的实体类。
910
@NamedQuery(name = "Person.withNameAndAddressNamedQuery", query = "select p from Person p where p.name=?1 and address=?2")
10-
public class Person {
11-
@Id // @Id注解指明这个属性映射为数据库的主键。
12-
@GeneratedValue // @GeneratedValue注解默认使用主键生成方式为自增,hibernate会为我们自动生成一个名为HIBERNATE_SEQUENCE的序列。
13-
private Long id;
11+
public class Person implements Serializable {
12+
@Id // @Id注解指明这个属性映射为数据库的主键。
13+
@GeneratedValue // @GeneratedValue注解默认使用主键生成方式为自增,hibernate会为我们自动生成一个名为HIBERNATE_SEQUENCE的序列。
14+
private Long id;
1415

15-
private String name;
16+
private String name;
1617

17-
private Integer age;
18+
private Integer age;
1819

19-
private String address;
20+
private String address;
2021

21-
public Person() {
22-
super();
23-
}
22+
public Person() {
23+
super();
24+
}
2425

25-
public Person(Long id, String name, Integer age, String address) {
26-
this.id = id;
27-
this.name = name;
28-
this.age = age;
29-
this.address = address;
30-
}
26+
public Person(Long id, String name, Integer age, String address) {
27+
this.id = id;
28+
this.name = name;
29+
this.age = age;
30+
this.address = address;
31+
}
3132

32-
public Person(String name, Integer age) {
33-
this.name = name;
34-
this.age = age;
35-
}
33+
public Person(String name, Integer age) {
34+
this.name = name;
35+
this.age = age;
36+
}
3637

37-
public Long getId() {
38-
return id;
39-
}
38+
public Long getId() {
39+
return id;
40+
}
4041

41-
public void setId(Long id) {
42-
this.id = id;
43-
}
42+
public void setId(Long id) {
43+
this.id = id;
44+
}
4445

45-
public String getName() {
46-
return name;
47-
}
46+
public String getName() {
47+
return name;
48+
}
4849

49-
public void setName(String name) {
50-
this.name = name;
51-
}
50+
public void setName(String name) {
51+
this.name = name;
52+
}
5253

53-
public Integer getAge() {
54-
return age;
55-
}
54+
public Integer getAge() {
55+
return age;
56+
}
5657

57-
public void setAge(Integer age) {
58-
this.age = age;
59-
}
58+
public void setAge(Integer age) {
59+
this.age = age;
60+
}
6061

61-
public String getAddress() {
62-
return address;
63-
}
62+
public String getAddress() {
63+
return address;
64+
}
6465

65-
public void setAddress(String address) {
66-
this.address = address;
67-
}
66+
public void setAddress(String address) {
67+
this.address = address;
68+
}
6869

69-
@Override
70-
public String toString() {
71-
return "Person [id=" + id + ", name=" + name + ", age=" + age + ", address=" + address + "]";
72-
}
70+
@Override
71+
public String toString() {
72+
return "Person [id=" + id + ", name=" + name + ", age=" + age + ", address=" + address + "]";
73+
}
7374

7475
}

spring-boot-student-layering-cache/src/main/java/com/xiaolyuh/service/impl/PersonServiceImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void removeAll() {
3939
@Override
4040
@Cacheable(value = "'people' + ':' + #person.id", key = "#person.id",
4141
firstCache = @FirstCache(expireTime = 4, timeUnit = TimeUnit.SECONDS),
42-
secondaryCache = @SecondaryCache(expiration = 10, preloadTime = 3, forceRefresh = true, timeUnit = TimeUnit.SECONDS))
42+
secondaryCache = @SecondaryCache(expiration = 100, preloadTime = 3, forceRefresh = true, timeUnit = TimeUnit.SECONDS))
4343
public Person findOne(Person person) {
4444
Person p = personRepository.findOne(person.getId());
4545
System.out.println("为id、key为:" + p.getId() + "数据做了缓存");

0 commit comments

Comments
 (0)