将 ShortUrlStore 从接口调整为 抽象类

This commit is contained in:
TheEnd 2022-07-31 17:43:12 +08:00
parent e906c5de43
commit 7ce692a2b2
6 changed files with 40 additions and 43 deletions

View File

@ -17,6 +17,13 @@
</properties>
<dependencies>
<!-- guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.1-jre</version>
</dependency>
<!-- zy-shorturl -->
<dependency>
<groupId>cn.zzzykj</groupId>

View File

@ -20,16 +20,16 @@ import javax.annotation.Resource;
@RequestMapping("/su")
public class ShortUrlController {
@Resource
private ShortUrlConfig shortUrlConfig;
@Resource
private ShortUrlStore store;
@PostConstruct
public void init() {
log.info("shortUrlConfig: {}", shortUrlConfig);
log.info("store: {}", store);
String url = "https://www.baidu.com";
String hash = store.hash(url);
log.info("hash: {}", hash);
store.add(url);
}
@GetMapping("/gen")

View File

@ -34,6 +34,7 @@ public class ShortUrlAutoConfiguration {
}
@Bean
@ConditionalOnMissingBean(ShortUrlUtil.class)
@ConditionalOnBean(ShortUrlConfig.class)
public ShortUrlUtil shortUrlUtil(ShortUrlConfig config) {
return new ShortUrlUtil(config);
@ -41,10 +42,11 @@ public class ShortUrlAutoConfiguration {
@Bean
@ConditionalOnMissingBean(ShortUrlStore.class)
@ConditionalOnBean(ShortUrlUtil.class)
@ConditionalOnProperty(prefix = "shorturl.store", name = "store-type", havingValue = "redis")
public ShortUrlStore redisShortUrlStore() {
public ShortUrlStore redisShortUrlStore(ShortUrlUtil util) {
log.info("use short-url store: {}", RedisShortUrlStore.class.getName());
return new RedisShortUrlStore();
return new RedisShortUrlStore(util);
}
}

View File

@ -9,14 +9,16 @@ import java.util.Date;
/**
* @author Lenovo
*/
public interface ShortUrlStore {
public abstract class ShortUrlStore {
protected ShortUrlUtil util;
/**
* 添加短连接(外部访问)
* @param url 原链接
* @return 短连接
*/
default ShortUrl add(String url) {
public ShortUrl add(String url) {
return add(url, null);
}
@ -26,8 +28,8 @@ public interface ShortUrlStore {
* @param expireTime 过期时间
* @return 短连接
*/
default ShortUrl add(String url, Date expireTime) {
Integer max = getConfig().getRetryMax();
public ShortUrl add(String url, Date expireTime) {
Integer max = util.getConfig().getRetryMax();
if (max == null) {
max = 5;
}
@ -37,6 +39,7 @@ public interface ShortUrlStore {
try {
shortUrl.setHash(hash);
add(shortUrl);
return shortUrl;
} catch (Exception e) {
e.printStackTrace();
}
@ -48,26 +51,22 @@ public interface ShortUrlStore {
* 添加短链接(内部访问, 请勿外部访问)
* @param shortUrl 短链接
*/
ShortUrl add(ShortUrl shortUrl);
protected abstract ShortUrl add(ShortUrl shortUrl);
/**
* 获取短链接
* @param hash 短链接
* @return 原链接
*/
ShortUrl get(String hash);
public abstract ShortUrl get(String hash);
/**
* 对URL进行Hash编码
* @param url 原链接
* @return 短链接
*/
String hash(String url);
/**
* 获取配置对象
* @return 配置对象
*/
ShortUrlConfig getConfig();
public String hash(String url) {
return util.gen(url);
}
}

View File

@ -1,16 +1,16 @@
package cn.shorturl.store.store;
import cn.shorturl.core.ShortUrl;
import cn.shorturl.core.ShortUrlConfig;
import cn.shorturl.store.ShortUrlStore;
/**
* @author Lenovo
*/
public class JdbcShortUrlStore implements ShortUrlStore {
public class JdbcShortUrlStore extends ShortUrlStore {
@Override
public ShortUrl add(ShortUrl shortUrl) {
protected ShortUrl add(ShortUrl shortUrl) {
return null;
}
@ -19,13 +19,4 @@ public class JdbcShortUrlStore implements ShortUrlStore {
return null;
}
@Override
public String hash(String url) {
return null;
}
@Override
public ShortUrlConfig getConfig() {
return null;
}
}

View File

@ -1,16 +1,23 @@
package cn.shorturl.store.store;
import cn.shorturl.core.ShortUrl;
import cn.shorturl.core.ShortUrlConfig;
import cn.shorturl.core.ShortUrlUtil;
import cn.shorturl.store.ShortUrlStore;
/**
* @author Lenovo
*/
public class RedisShortUrlStore implements ShortUrlStore {
public class RedisShortUrlStore extends ShortUrlStore {
public RedisShortUrlStore(ShortUrlUtil util) {
super.util = util;
}
@Override
public ShortUrl add(ShortUrl shortUrl) {
protected ShortUrl add(ShortUrl shortUrl) {
System.out.println(shortUrl);
return null;
}
@ -19,13 +26,4 @@ public class RedisShortUrlStore implements ShortUrlStore {
return null;
}
@Override
public String hash(String url) {
return null;
}
@Override
public ShortUrlConfig getConfig() {
return null;
}
}