时间统计 和 内存缓存
This commit is contained in:
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.Scheduled;
|
||||
|
||||
import com.flagnote.note.cache.NoteCache;
|
||||
import com.flagnote.note.entity.Note;
|
||||
import com.flagnote.note.repository.NoteMongoRepository;
|
||||
|
||||
@@ -22,7 +23,7 @@ public class NoteSchedule {
|
||||
@Autowired
|
||||
private NoteMongoRepository noteMongoRepository;
|
||||
|
||||
@Scheduled(cron = "0/10 * * * * ?")
|
||||
@Scheduled(cron = "*/30 * * * * ?")
|
||||
public void updateExpiredNote() {
|
||||
|
||||
while(true) {
|
||||
@@ -34,6 +35,7 @@ public class NoteSchedule {
|
||||
for (Note note : noteList) {
|
||||
note.setState(0);
|
||||
note.setTextBytes(null);
|
||||
NoteCache.deleteNote(note.getId());
|
||||
}
|
||||
noteMongoRepository.saveAll(noteList);
|
||||
}else {
|
||||
|
||||
@@ -9,6 +9,8 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
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.repository.NoteMongoRepository;
|
||||
import com.flagnote.note.utils.BizKeyUtils;
|
||||
@@ -26,12 +28,16 @@ public class NoteServiceImpl implements NoteService {
|
||||
private NoteMongoRepository noteMongoRepository;
|
||||
|
||||
@Override
|
||||
@CountTime
|
||||
public Note getNote(String 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) {
|
||||
return null;
|
||||
}
|
||||
@@ -52,6 +58,7 @@ public class NoteServiceImpl implements NoteService {
|
||||
}
|
||||
|
||||
@Override
|
||||
@CountTime
|
||||
public String getNoteText(String key) {
|
||||
Note note = this.getNote(key);
|
||||
if (null == note || ArrayUtil.isEmpty(note.getTextBytes())) {
|
||||
@@ -61,6 +68,7 @@ public class NoteServiceImpl implements NoteService {
|
||||
}
|
||||
|
||||
@Override
|
||||
@CountTime
|
||||
public void saveNote(Note note) {
|
||||
String mixKey = BizKeyUtils.mixKey(note.getKey());
|
||||
|
||||
@@ -78,6 +86,8 @@ public class NoteServiceImpl implements NoteService {
|
||||
|
||||
if (null == exists) {
|
||||
noteMongoRepository.save(note);
|
||||
|
||||
NoteCache.save(note);
|
||||
} else {
|
||||
if (!exists.getMd5().equals(note.getMd5())) {
|
||||
throw new RuntimeException("E:800001");
|
||||
@@ -88,8 +98,11 @@ public class NoteServiceImpl implements NoteService {
|
||||
@Override
|
||||
public void deleteNote(String key) {
|
||||
String mixKey = BizKeyUtils.mixKey(key);
|
||||
|
||||
NoteCache.deleteNote(mixKey);
|
||||
|
||||
Note note = noteMongoRepository.findById(mixKey).orElse(null);
|
||||
if (null != note && ArrayUtil.isNotEmpty(note.getTextBytes())) {
|
||||
if (null != note) {
|
||||
note.setState(0);
|
||||
note.setTextBytes(null);
|
||||
noteMongoRepository.save(note);
|
||||
|
||||
Reference in New Issue
Block a user