package project.ddos.internal;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.concurrent.ConcurrentHashMap;
|
|
import kernel.web.ApplicationUtil;
|
import project.ddos.DdosRedisKeys;
|
import project.ddos.UrlSpecialService;
|
import project.ddos.model.UrlSpecial;
|
import project.redis.RedisHandler;
|
|
public class UrlSpecialServiceImpl implements UrlSpecialService {
|
|
private RedisHandler redisHandler;
|
|
@Override
|
public void save(UrlSpecial entity) {
|
if(null==entity) return;
|
if(null==entity.getId()) entity.setId(ApplicationUtil.getCurrentTimeUUID());
|
|
ApplicationUtil.executeInsert(entity);
|
redisHandler.setSync(DdosRedisKeys.URL_SPECIAL_ID + entity.getId().toString(), entity);
|
|
Map<String, String> cacheMap = (Map<String, String>) redisHandler.get(DdosRedisKeys.URL_SPECIAL_URL_MAP);
|
if (cacheMap == null) cacheMap = new ConcurrentHashMap<String, String>();
|
cacheMap.put(entity.getId().toString(), entity.getUrl());
|
redisHandler.setSync(DdosRedisKeys.URL_SPECIAL_URL_MAP, cacheMap);
|
}
|
|
@Override
|
public void update(UrlSpecial entity) {
|
if(null==entity) return;
|
|
ApplicationUtil.executeUpdate(entity,"WHERE UUID=?",new Object[] {entity.getId()});
|
redisHandler.setSync(DdosRedisKeys.URL_SPECIAL_ID + entity.getId().toString(), entity);
|
|
Map<String, String> cacheMap = (Map<String, String>) redisHandler.get(DdosRedisKeys.URL_SPECIAL_URL_MAP);
|
if (cacheMap == null) cacheMap = new ConcurrentHashMap<String, String>();
|
cacheMap.put(entity.getId().toString(), entity.getUrl());
|
redisHandler.setSync(DdosRedisKeys.URL_SPECIAL_URL_MAP, cacheMap);
|
}
|
|
@Override
|
public void delete(UrlSpecial entity) {
|
if(null==entity) return;
|
|
ApplicationUtil.executeDelete(UrlSpecial.class, "WHERE UUID=?", new Object[] {entity.getId()});
|
redisHandler.remove(DdosRedisKeys.URL_SPECIAL_ID + entity.getId().toString());
|
|
Map<String, String> cacheMap = (Map<String, String>) redisHandler.get(DdosRedisKeys.URL_SPECIAL_URL_MAP);
|
if (cacheMap != null && !cacheMap.isEmpty()) cacheMap.remove(entity.getId().toString());
|
redisHandler.setSync(DdosRedisKeys.URL_SPECIAL_URL_MAP, cacheMap);
|
}
|
|
@Override
|
public UrlSpecial cacheById(String id) {
|
return (UrlSpecial) redisHandler.get(DdosRedisKeys.URL_SPECIAL_ID + id);
|
}
|
|
public List<String> cacheAllUrls() {
|
Map<String, String> cacheMap = (Map<String, String>) redisHandler.get(DdosRedisKeys.URL_SPECIAL_URL_MAP);
|
if (cacheMap == null || cacheMap.isEmpty()) {
|
return new ArrayList<String>();
|
} else {
|
return new ArrayList<String>(cacheMap.values());
|
}
|
}
|
|
public void setRedisHandler(RedisHandler redisHandler) {
|
this.redisHandler = redisHandler;
|
}
|
|
}
|