1
zj
2025-04-17 ff2d1f5acdadc466d7e199028ef385ae8ca277e7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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;
    }
 
}