将 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> </properties>
<dependencies> <dependencies>
<!-- guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.1-jre</version>
</dependency>
<!-- zy-shorturl --> <!-- zy-shorturl -->
<dependency> <dependency>
<groupId>cn.zzzykj</groupId> <groupId>cn.zzzykj</groupId>

View File

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

View File

@ -34,6 +34,7 @@ public class ShortUrlAutoConfiguration {
} }
@Bean @Bean
@ConditionalOnMissingBean(ShortUrlUtil.class)
@ConditionalOnBean(ShortUrlConfig.class) @ConditionalOnBean(ShortUrlConfig.class)
public ShortUrlUtil shortUrlUtil(ShortUrlConfig config) { public ShortUrlUtil shortUrlUtil(ShortUrlConfig config) {
return new ShortUrlUtil(config); return new ShortUrlUtil(config);
@ -41,10 +42,11 @@ public class ShortUrlAutoConfiguration {
@Bean @Bean
@ConditionalOnMissingBean(ShortUrlStore.class) @ConditionalOnMissingBean(ShortUrlStore.class)
@ConditionalOnBean(ShortUrlUtil.class)
@ConditionalOnProperty(prefix = "shorturl.store", name = "store-type", havingValue = "redis") @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()); 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 * @author Lenovo
*/ */
public interface ShortUrlStore { public abstract class ShortUrlStore {
protected ShortUrlUtil util;
/** /**
* 添加短连接(外部访问) * 添加短连接(外部访问)
* @param url 原链接 * @param url 原链接
* @return 短连接 * @return 短连接
*/ */
default ShortUrl add(String url) { public ShortUrl add(String url) {
return add(url, null); return add(url, null);
} }
@ -26,8 +28,8 @@ public interface ShortUrlStore {
* @param expireTime 过期时间 * @param expireTime 过期时间
* @return 短连接 * @return 短连接
*/ */
default ShortUrl add(String url, Date expireTime) { public ShortUrl add(String url, Date expireTime) {
Integer max = getConfig().getRetryMax(); Integer max = util.getConfig().getRetryMax();
if (max == null) { if (max == null) {
max = 5; max = 5;
} }
@ -37,6 +39,7 @@ public interface ShortUrlStore {
try { try {
shortUrl.setHash(hash); shortUrl.setHash(hash);
add(shortUrl); add(shortUrl);
return shortUrl;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -48,26 +51,22 @@ public interface ShortUrlStore {
* 添加短链接(内部访问, 请勿外部访问) * 添加短链接(内部访问, 请勿外部访问)
* @param shortUrl 短链接 * @param shortUrl 短链接
*/ */
ShortUrl add(ShortUrl shortUrl); protected abstract ShortUrl add(ShortUrl shortUrl);
/** /**
* 获取短链接 * 获取短链接
* @param hash 短链接 * @param hash 短链接
* @return 原链接 * @return 原链接
*/ */
ShortUrl get(String hash); public abstract ShortUrl get(String hash);
/** /**
* 对URL进行Hash编码 * 对URL进行Hash编码
* @param url 原链接 * @param url 原链接
* @return 短链接 * @return 短链接
*/ */
String hash(String url); public String hash(String url) {
return util.gen(url);
/** }
* 获取配置对象
* @return 配置对象
*/
ShortUrlConfig getConfig();
} }

View File

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