Skip to content

Commit

Permalink
完善文章管理
Browse files Browse the repository at this point in the history
  • Loading branch information
hjljy committed May 12, 2022
1 parent 9179d97 commit 422a931
Show file tree
Hide file tree
Showing 51 changed files with 493 additions and 235 deletions.
16 changes: 8 additions & 8 deletions src/main/java/cn/hjljy/mlog/cache/AbstractCacheStore.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cn.hjljy.mlog.cache;

import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
Expand All @@ -25,7 +26,6 @@ public abstract class AbstractCacheStore<V> implements CacheStore<V> {
* @param key key不能为空
* @return {@link Optional}<{@link CacheWrapper}<{@link V}>>
*/
@NonNull
protected abstract Optional<CacheWrapper<V>> getInternal(@NonNull String key);


Expand Down Expand Up @@ -56,13 +56,13 @@ public abstract class AbstractCacheStore<V> implements CacheStore<V> {
* @return {@link Optional}<{@link V}>
*/
@Override
public Optional<V> get(String key) {
public Optional<V> get(@NotNull String key) {
Assert.hasText(key, "缓存的key不能为空");
return getInternal(key).map(cacheWrapper -> {
// 判断缓存是否过期
if (cacheWrapper.isExpire()) {
// 过期删除缓存 并返回null
log.info("缓存key: {} 已过期", key);
log.info("缓存key:{} 已过期,过期时间是:{}", key,cacheWrapper.getExpireTime());
delete(key);
return null;
}
Expand All @@ -75,10 +75,10 @@ public Optional<V> get(String key) {
* @param key key不能为空
* @param value value不能为空
* @param timeout 超时时间 如果为0表示永不过期 ,不能为负数 ,否者抛出异常
* @param timeUnit 时间单位 不能为空
* @param timeUnit 时间单位
*/
@Override
public void put(String key, V value, long timeout, ChronoUnit timeUnit) {
public void put(@NotNull String key, @NotNull V value, long timeout, ChronoUnit timeUnit) {
putInternal(key, buildCacheWrapper(key, value, timeout, timeUnit));
}
/**
Expand All @@ -88,7 +88,7 @@ public void put(String key, V value, long timeout, ChronoUnit timeUnit) {
* @param value value不能为空
*/
@Override
public void put(String key, V value) {
public void put(@NotNull String key, @NotNull V value) {
put(key,value,0,null);
}

Expand All @@ -98,11 +98,11 @@ public void put(String key, V value) {
* @param key key不能为空
* @param value value不能为空
* @param timeout 超时时间 如果为0表示永不过期 ,不能为负数 ,否者抛出异常
* @param timeUnit 时间单位 不能为空
* @param timeUnit 时间单位
* @return {@link Boolean}
*/
@Override
public Boolean putIfAbsent(String key, V value, long timeout, ChronoUnit timeUnit) {
public Boolean putIfAbsent(@NotNull String key, @NotNull V value, long timeout, ChronoUnit timeUnit) {
return putInternalIfAbsent(key, buildCacheWrapper(key, value, timeout, timeUnit));
}

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/cn/hjljy/mlog/cache/CacheStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public interface CacheStore<V> {
* @param key key不能为空
* @param value value不能为空
* @param timeout 超时时间 如果为0表示永不过期 ,不能为负数 ,否者抛出异常
* @param timeUnit 时间单位 不能为空
* @param timeUnit 时间单位
*/
void put(@NonNull String key, @NonNull V value, long timeout, @NonNull ChronoUnit timeUnit) ;
void put(@NonNull String key, @NonNull V value, long timeout, ChronoUnit timeUnit) ;


/**
Expand All @@ -50,10 +50,10 @@ public interface CacheStore<V> {
* @param key key不能为空
* @param value value不能为空
* @param timeout 超时时间 如果为0表示永不过期 ,不能为负数 ,否者抛出异常
* @param timeUnit 时间单位 不能为空
* @param timeUnit 时间单位
* @return {@link Boolean}
*/
Boolean putIfAbsent(@NonNull String key, @NonNull V value, long timeout, @NonNull ChronoUnit timeUnit) ;
Boolean putIfAbsent(@NonNull String key, @NonNull V value, long timeout, ChronoUnit timeUnit) ;

/**
* 删除缓存
Expand Down
1 change: 0 additions & 1 deletion src/main/java/cn/hjljy/mlog/cache/CacheWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Date;

/**
* 针对缓存数据的封装
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/cn/hjljy/mlog/cache/InMemoryCacheStore.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cn.hjljy.mlog.cache;

import org.springframework.stereotype.Component;
import org.jetbrains.annotations.NotNull;

import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
Expand All @@ -16,17 +16,19 @@ public class InMemoryCacheStore extends JsonCacheStore {
private static final ConcurrentHashMap<String, CacheWrapper<String>> CACHE_CONTAINER = new ConcurrentHashMap<>();

@Override
protected Optional<CacheWrapper<String>> getInternal(String key) {
@NotNull
protected Optional<CacheWrapper<String>> getInternal(@NotNull String key) {
return Optional.ofNullable(CACHE_CONTAINER.get(key));
}

@Override
protected void putInternal(String key, CacheWrapper<String> cacheWrapper) {
protected void putInternal(@NotNull String key, @NotNull CacheWrapper<String> cacheWrapper) {
CACHE_CONTAINER.put(key, cacheWrapper);
}

@Override
protected Boolean putInternalIfAbsent(String key, CacheWrapper<String> cacheWrapper) {
@NotNull
protected Boolean putInternalIfAbsent(@NotNull String key, @NotNull CacheWrapper<String> cacheWrapper) {
CacheWrapper<String> wrapper = CACHE_CONTAINER.get(key);
//对象锁,保证线程安全
synchronized (this) {
Expand All @@ -39,7 +41,7 @@ protected Boolean putInternalIfAbsent(String key, CacheWrapper<String> cacheWrap
}

@Override
public void delete(String key) {
public void delete(@NotNull String key) {
CACHE_CONTAINER.remove(key);
}
}
3 changes: 1 addition & 2 deletions src/main/java/cn/hjljy/mlog/cache/JsonCacheStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

import cn.hjljy.mlog.common.utils.JacksonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;

import java.util.Optional;

/**
* json数据缓存
* json格式数据缓存
*
* @author hjljy
* @date 2021/10/14
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/cn/hjljy/mlog/cache/RedisCacheStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;

Expand All @@ -26,7 +27,8 @@ public RedisCacheStore(RedisConnectionFactory redisConnectionFactory) {


@Override
protected Optional<CacheWrapper<String>> getInternal(String key) {
@NotNull
protected Optional<CacheWrapper<String>> getInternal(@NotNull String key) {
String value = "";
try {
value = redisTemplate.opsForValue().get(key);
Expand All @@ -37,22 +39,23 @@ protected Optional<CacheWrapper<String>> getInternal(String key) {
}

@Override
protected void putInternal(String key, CacheWrapper<String> cacheWrapper) {
protected void putInternal(@NotNull String key, @NotNull CacheWrapper<String> cacheWrapper) {
try {
LocalDateTime expireTime = cacheWrapper.getExpireTime();
if (expireTime != null) {
Duration between = LocalDateTimeUtil.between(cacheWrapper.getCreateTime(), expireTime);
redisTemplate.opsForValue().set(key, cacheWrapper2Json(cacheWrapper), between);
} else {
redisTemplate.opsForValue().set(key, cacheWrapper.getData());
redisTemplate.opsForValue().set(key, cacheWrapper2Json(cacheWrapper));
}
} catch (Exception e) {
log.warn("存取缓存信息失败,key:{},失败原因:{}", key, e.getMessage());
}
}

@Override
protected Boolean putInternalIfAbsent(String key, CacheWrapper<String> cacheWrapper) {
@NotNull
protected Boolean putInternalIfAbsent(@NotNull String key, @NotNull CacheWrapper<String> cacheWrapper) {
Boolean hasKey = redisTemplate.hasKey(key);
if (null != hasKey && hasKey) {
return false;
Expand All @@ -62,7 +65,7 @@ protected Boolean putInternalIfAbsent(String key, CacheWrapper<String> cacheWrap
}

@Override
public void delete(String key) {
public void delete(@NotNull String key) {
try {
redisTemplate.delete(key);
} catch (Exception e) {
Expand Down
49 changes: 49 additions & 0 deletions src/main/java/cn/hjljy/mlog/common/constants/UrlConstant.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package cn.hjljy.mlog.common.constants;

/**
* url常数
*
* @author hjljy
* @date 2022/03/18
*/
public class UrlConstant {

/**
* 特殊请求路径地址
*/
public static class Special {
public static final String CATEGORIES="/categories";
}


/**
* 后台管理接口地址
*/
public static final String ADMIN_API="/admin";

public static class Admin {
public static final String ARTICLE_PAGE="/article/page";
}

/**
* 开放api接口地址
*/
public static final String OPEN_API="/open";
public static class Open{
/**
* 登录背景图像
*/
public static final String LOGIN_BACKGROUND_IMAGE ="/login/background/image";

/**
* 博客基础信息
*/
public static final String BLOG_BASE_INFO ="/blog/base/info";

/**
* 博客的统计数据
*/
public static final String BLOG_STATISTICS="/blog/statistics";

}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.hjljy.mlog.model.enums;
package cn.hjljy.mlog.common.enums;

/**
* 状态枚举
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.hjljy.mlog.model.enums;
package cn.hjljy.mlog.common.enums;

/**
* 文件存储类型的枚举
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.hjljy.mlog.model.enums;
package cn.hjljy.mlog.common.enums;

import com.baomidou.mybatisplus.annotation.EnumValue;
import lombok.Getter;
Expand All @@ -16,10 +16,17 @@ public enum SettingOptionKeyEnum {
* 博客标题
*/
BLOG_TITLE("blogTitle","mlog博客"),

/**
* 博客主题
*/
THEME("theme","anatole"),

/**
* 登录背景图像
*/
LOGIN_BACKGROUND_IMAGE("loginBackgroundImage","https://image.hjljy.cn/image/default.jpg"),

/**
* 博客默认favicon url
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.hjljy.mlog.model.enums;
package cn.hjljy.mlog.common.enums;

/**
* 设置类型
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.hjljy.mlog.model.enums;
package cn.hjljy.mlog.common.enums;

/**
* 用户类型枚举
Expand Down
1 change: 1 addition & 0 deletions src/main/java/cn/hjljy/mlog/common/support/ResultCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public enum ResultCode {

//
FILE_STORAGE_NOT_EXISTS(40000,"文件存储方式不存在"),
MD_FILE_IMPORT(40001,"Markdown文件导入失败"),
//服务端代码异常
SQL_EXCEPTION(90000,"服务器异常,请联系管理员"),
NPE_EXCEPTION(90001,"服务器异常,请联系管理员");
Expand Down
21 changes: 1 addition & 20 deletions src/main/java/cn/hjljy/mlog/common/utils/MarkdownUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.apache.commons.lang3.StringUtils;
import org.yaml.snakeyaml.Yaml;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -97,29 +96,11 @@ public static String findTime(String str) {
}

/**
* 得到网页中图片的地址
* 得到网页中首张图片的地址
*
* @param htmlStr html str 网页数据使用字符串传入
* @return {@link Set}<{@link String}> 图片地址集合
*/
public static Set<String> getImgStr(String htmlStr) {
Set<String> pics = new HashSet<>();
String regExImg = "<img.*src\\s*=\\s*(.*?)[^>]*?>";
Pattern pImage = Pattern.compile
(regExImg, Pattern.CASE_INSENSITIVE);
Matcher mImage = pImage.matcher(htmlStr);
String exImg = "src\\s*=\\s*\"?(.*?)(\"|>|\\s+)";
while (mImage.find()) {
// 得到<img />数据
String img = mImage.group();
// 匹配<img>中的src数据
Matcher m = Pattern.compile(exImg).matcher(img);
while (m.find()) {
pics.add(m.group(1));
}
}
return pics;
}
public static String getFirstImgStr(String htmlStr) {
String str="";
String regExImg = "<img.*src\\s*=\\s*(.*?)[^>]*?>";
Expand Down
Loading

0 comments on commit 422a931

Please sign in to comment.