时间统计 和 内存缓存
This commit is contained in:
4
pom.xml
4
pom.xml
@@ -34,6 +34,10 @@
|
|||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-aop</artifactId>
|
||||||
|
</dependency>
|
||||||
<!--
|
<!--
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.cloud</groupId>
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
|||||||
42
src/main/java/com/flagnote/note/aop/AopConfig.java
Normal file
42
src/main/java/com/flagnote/note/aop/AopConfig.java
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
package com.flagnote.note.aop;
|
||||||
|
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import org.aspectj.lang.JoinPoint;
|
||||||
|
import org.aspectj.lang.ProceedingJoinPoint;
|
||||||
|
import org.aspectj.lang.annotation.AfterReturning;
|
||||||
|
import org.aspectj.lang.annotation.Around;
|
||||||
|
import org.aspectj.lang.annotation.Aspect;
|
||||||
|
import org.aspectj.lang.annotation.Before;
|
||||||
|
import org.aspectj.lang.annotation.Pointcut;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.util.StopWatch;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
@Aspect
|
||||||
|
@Component
|
||||||
|
@Slf4j
|
||||||
|
public class AopConfig {
|
||||||
|
@Pointcut("@annotation(com.flagnote.note.aop.CountTime)")
|
||||||
|
public void logTime() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private ThreadLocal<StopWatch> stopWatchLocal = new ThreadLocal<>();
|
||||||
|
|
||||||
|
@Before("logTime()")
|
||||||
|
public void before() {
|
||||||
|
StopWatch stopWatch = new StopWatch();
|
||||||
|
stopWatchLocal.set(stopWatch);
|
||||||
|
stopWatch.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterReturning(value = "logTime()", returning = "val")
|
||||||
|
public void afterReturning(JoinPoint joinPoint, Object val) {
|
||||||
|
StopWatch stopWatch = stopWatchLocal.get();
|
||||||
|
stopWatch.stop();
|
||||||
|
log.info("method:"+ joinPoint.getSignature().getName()+">>> " + stopWatch.getLastTaskTimeMillis() + " ms.");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
12
src/main/java/com/flagnote/note/aop/CountTime.java
Normal file
12
src/main/java/com/flagnote/note/aop/CountTime.java
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package com.flagnote.note.aop;
|
||||||
|
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
import static java.lang.annotation.ElementType.METHOD;
|
||||||
|
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||||
|
|
||||||
|
@Retention(RUNTIME)
|
||||||
|
@Target(METHOD)
|
||||||
|
public @interface CountTime {
|
||||||
|
}
|
||||||
27
src/main/java/com/flagnote/note/cache/NoteCache.java
vendored
Normal file
27
src/main/java/com/flagnote/note/cache/NoteCache.java
vendored
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
package com.flagnote.note.cache;
|
||||||
|
|
||||||
|
import com.flagnote.note.entity.Note;
|
||||||
|
|
||||||
|
import cn.hutool.cache.CacheUtil;
|
||||||
|
import cn.hutool.cache.impl.LFUCache;
|
||||||
|
|
||||||
|
public class NoteCache {
|
||||||
|
|
||||||
|
private static final LFUCache<String, Note> CACHE = CacheUtil.newLFUCache(256,30*60*1000);
|
||||||
|
|
||||||
|
public static Note getNote(String mixKey) {
|
||||||
|
return CACHE.get(mixKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void save(Note note) {
|
||||||
|
CACHE.put(note.getId(), note);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void deleteNote(String mixKey) {
|
||||||
|
Note note = CACHE.get(mixKey);
|
||||||
|
if(null!=note) {
|
||||||
|
note.setState(0);
|
||||||
|
note.setTextBytes(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -12,6 +12,7 @@ import org.springframework.data.domain.Pageable;
|
|||||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||||
import org.springframework.scheduling.annotation.Scheduled;
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
|
|
||||||
|
import com.flagnote.note.cache.NoteCache;
|
||||||
import com.flagnote.note.entity.Note;
|
import com.flagnote.note.entity.Note;
|
||||||
import com.flagnote.note.repository.NoteMongoRepository;
|
import com.flagnote.note.repository.NoteMongoRepository;
|
||||||
|
|
||||||
@@ -22,7 +23,7 @@ public class NoteSchedule {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private NoteMongoRepository noteMongoRepository;
|
private NoteMongoRepository noteMongoRepository;
|
||||||
|
|
||||||
@Scheduled(cron = "0/10 * * * * ?")
|
@Scheduled(cron = "*/30 * * * * ?")
|
||||||
public void updateExpiredNote() {
|
public void updateExpiredNote() {
|
||||||
|
|
||||||
while(true) {
|
while(true) {
|
||||||
@@ -34,6 +35,7 @@ public class NoteSchedule {
|
|||||||
for (Note note : noteList) {
|
for (Note note : noteList) {
|
||||||
note.setState(0);
|
note.setState(0);
|
||||||
note.setTextBytes(null);
|
note.setTextBytes(null);
|
||||||
|
NoteCache.deleteNote(note.getId());
|
||||||
}
|
}
|
||||||
noteMongoRepository.saveAll(noteList);
|
noteMongoRepository.saveAll(noteList);
|
||||||
}else {
|
}else {
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
|
import com.flagnote.note.aop.CountTime;
|
||||||
|
import com.flagnote.note.cache.NoteCache;
|
||||||
import com.flagnote.note.entity.Note;
|
import com.flagnote.note.entity.Note;
|
||||||
import com.flagnote.note.repository.NoteMongoRepository;
|
import com.flagnote.note.repository.NoteMongoRepository;
|
||||||
import com.flagnote.note.utils.BizKeyUtils;
|
import com.flagnote.note.utils.BizKeyUtils;
|
||||||
@@ -26,11 +28,15 @@ public class NoteServiceImpl implements NoteService {
|
|||||||
private NoteMongoRepository noteMongoRepository;
|
private NoteMongoRepository noteMongoRepository;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@CountTime
|
||||||
public Note getNote(String key) {
|
public Note getNote(String key) {
|
||||||
|
|
||||||
String mixKey = BizKeyUtils.mixKey(key);
|
String mixKey = BizKeyUtils.mixKey(key);
|
||||||
|
|
||||||
Note note = noteMongoRepository.findById(mixKey).orElse(null);
|
Note note = NoteCache.getNote(mixKey);
|
||||||
|
if (null == note) {
|
||||||
|
note = noteMongoRepository.findById(mixKey).orElse(null);
|
||||||
|
}
|
||||||
|
|
||||||
if (null == note) {
|
if (null == note) {
|
||||||
return null;
|
return null;
|
||||||
@@ -52,6 +58,7 @@ public class NoteServiceImpl implements NoteService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@CountTime
|
||||||
public String getNoteText(String key) {
|
public String getNoteText(String key) {
|
||||||
Note note = this.getNote(key);
|
Note note = this.getNote(key);
|
||||||
if (null == note || ArrayUtil.isEmpty(note.getTextBytes())) {
|
if (null == note || ArrayUtil.isEmpty(note.getTextBytes())) {
|
||||||
@@ -61,6 +68,7 @@ public class NoteServiceImpl implements NoteService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@CountTime
|
||||||
public void saveNote(Note note) {
|
public void saveNote(Note note) {
|
||||||
String mixKey = BizKeyUtils.mixKey(note.getKey());
|
String mixKey = BizKeyUtils.mixKey(note.getKey());
|
||||||
|
|
||||||
@@ -78,6 +86,8 @@ public class NoteServiceImpl implements NoteService {
|
|||||||
|
|
||||||
if (null == exists) {
|
if (null == exists) {
|
||||||
noteMongoRepository.save(note);
|
noteMongoRepository.save(note);
|
||||||
|
|
||||||
|
NoteCache.save(note);
|
||||||
} else {
|
} else {
|
||||||
if (!exists.getMd5().equals(note.getMd5())) {
|
if (!exists.getMd5().equals(note.getMd5())) {
|
||||||
throw new RuntimeException("E:800001");
|
throw new RuntimeException("E:800001");
|
||||||
@@ -88,8 +98,11 @@ public class NoteServiceImpl implements NoteService {
|
|||||||
@Override
|
@Override
|
||||||
public void deleteNote(String key) {
|
public void deleteNote(String key) {
|
||||||
String mixKey = BizKeyUtils.mixKey(key);
|
String mixKey = BizKeyUtils.mixKey(key);
|
||||||
|
|
||||||
|
NoteCache.deleteNote(mixKey);
|
||||||
|
|
||||||
Note note = noteMongoRepository.findById(mixKey).orElse(null);
|
Note note = noteMongoRepository.findById(mixKey).orElse(null);
|
||||||
if (null != note && ArrayUtil.isNotEmpty(note.getTextBytes())) {
|
if (null != note) {
|
||||||
note.setState(0);
|
note.setState(0);
|
||||||
note.setTextBytes(null);
|
note.setTextBytes(null);
|
||||||
noteMongoRepository.save(note);
|
noteMongoRepository.save(note);
|
||||||
|
|||||||
@@ -40,3 +40,11 @@ management:
|
|||||||
exposure:
|
exposure:
|
||||||
include: shutdown,health
|
include: shutdown,health
|
||||||
|
|
||||||
|
logging:
|
||||||
|
level:
|
||||||
|
org:
|
||||||
|
springframework:
|
||||||
|
data:
|
||||||
|
mongodb:
|
||||||
|
core:
|
||||||
|
MongoTemplate: DEBUG
|
||||||
Reference in New Issue
Block a user