parent
4c3cdd2eba
commit
d2f7468acd
30 changed files with 350 additions and 154 deletions
@ -0,0 +1,27 @@ |
||||
package cc.bnblogs.common; |
||||
|
||||
import lombok.Data; |
||||
import org.springframework.boot.context.properties.ConfigurationProperties; |
||||
import org.springframework.stereotype.Component; |
||||
import org.thymeleaf.util.StringUtils; |
||||
|
||||
import java.util.List; |
||||
|
||||
/** |
||||
* @author zfp@bnblogs.cc |
||||
* @createTime: 2022/10/20 |
||||
*/ |
||||
@Component |
||||
@Data |
||||
@ConfigurationProperties(prefix = "default-images") |
||||
public class DefaultImages { |
||||
private List<String> images; |
||||
|
||||
public String cover(String imgUrl) { |
||||
if (StringUtils.isEmptyOrWhitespace(imgUrl)) { |
||||
// 返回一张随机图
|
||||
return images.get((int)(Math.random() * images.size())); |
||||
} |
||||
return imgUrl; |
||||
} |
||||
} |
@ -0,0 +1,52 @@ |
||||
package cc.bnblogs.config; |
||||
|
||||
import org.springframework.cache.annotation.CachingConfigurerSupport; |
||||
import org.springframework.cache.interceptor.KeyGenerator; |
||||
import org.springframework.context.annotation.Configuration; |
||||
import org.thymeleaf.util.StringUtils; |
||||
|
||||
import java.io.Serializable; |
||||
import java.util.Arrays; |
||||
|
||||
/** |
||||
* @author zfp@bnblogs.cc |
||||
* @createTime: 2022/10/20 |
||||
*/ |
||||
@Configuration |
||||
public class CacheConfig extends CachingConfigurerSupport { |
||||
@Override |
||||
public KeyGenerator keyGenerator() { |
||||
return (target, method, params) -> new BlogCacheKey(target.getClass().getName(), method.getName(), params); |
||||
} |
||||
|
||||
static class BlogCacheKey implements Serializable { |
||||
private final String className; |
||||
private final String methodName; |
||||
private final Object[] params; |
||||
private final int hashCode; |
||||
|
||||
public BlogCacheKey(String className, String methodName, Object[] params) { |
||||
this.className = className; |
||||
this.methodName = methodName; |
||||
this.params = params; |
||||
String sign = className + "_" + methodName + "_" + Arrays.deepHashCode(params); |
||||
this.hashCode = sign.hashCode(); |
||||
} |
||||
|
||||
@Override |
||||
public int hashCode() { |
||||
return this.hashCode; |
||||
} |
||||
|
||||
@Override |
||||
public boolean equals(Object obj) { |
||||
if (obj == this) return true; |
||||
if (!(obj instanceof BlogCacheKey)) return false; |
||||
BlogCacheKey other = (BlogCacheKey) obj; |
||||
if (this.hashCode == other.hashCode) return true; |
||||
return StringUtils.equals(this.className, other.className) |
||||
&& StringUtils.equals(this.methodName, other.methodName) |
||||
&& Arrays.deepEquals(this.params, other.params); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,25 @@ |
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false"> |
||||
|
||||
<!-- 磁盘缓存位置 --> |
||||
<diskStore path="./cache"/> |
||||
|
||||
<!-- maxElementsInMemory: 在内存中缓存的element的最大数目。--> |
||||
<!-- eternal:elements是否永久有效,如果为true,timeouts将被忽略,element将永不过期 --> |
||||
<!-- timeToIdleSeconds:发呆秒数,发呆期间未访问缓存立即过期,当eternal为false时,这个属性才有效,0为不限制 --> |
||||
<!-- timeToLiveSeconds:总存活秒数,当eternal为false时,这个属性才有效,0为不限制 --> |
||||
<!-- overflowToDisk: 如果内存中数据超过内存限制,是否要缓存到磁盘上 --> |
||||
<!-- statistics:是否收集统计信息。如果需要监控缓存使用情况,应该打开这个选项。默认为关闭(统计会影响性能)。设置statistics="true"开启统计 --> |
||||
|
||||
<!-- |
||||
默认缓存 |
||||
无过期时间,但 3600 秒内无人访问缓存立即过期 |
||||
--> |
||||
<cache name="blog-cache" |
||||
maxElementsInMemory="1000" |
||||
timeToIdleSeconds="3600" |
||||
timeToLiveSeconds="0" |
||||
overflowToDisk="true"> |
||||
</cache> |
||||
</ehcache> |
@ -0,0 +1,27 @@ |
||||
package cc.bnblogs.test; |
||||
|
||||
import cc.bnblogs.Application; |
||||
import cc.bnblogs.common.DefaultImages; |
||||
import org.junit.Test; |
||||
import org.junit.runner.RunWith; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.boot.test.context.SpringBootTest; |
||||
import org.springframework.test.context.junit4.SpringRunner; |
||||
|
||||
/** |
||||
* @author zfp@bnblogs.cc |
||||
* @createTime: 2022/10/20 |
||||
*/ |
||||
@RunWith(SpringRunner.class) |
||||
@SpringBootTest(classes = {Application.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) |
||||
public class ImageTest { |
||||
@Autowired |
||||
private DefaultImages images; |
||||
|
||||
@Test |
||||
public void getImage() { |
||||
System.out.println(images.cover("111")); |
||||
System.out.println(images.cover("")); |
||||
|
||||
} |
||||
} |
Loading…
Reference in new issue