peternameyakj
2024-10-24 7621191445c6f37f0d8253657560be6202778395
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package project.item.internal;
 
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
 
import kernel.cache.RedisLocalCache;
import kernel.util.StringUtils;
import kernel.web.ApplicationUtil;
import project.item.ItemRedisKeys;
import project.item.ItemService;
import project.item.model.Item;
import project.item.model.ItemLever;
import project.redis.RedisHandler;
 
@SuppressWarnings("unchecked")
public class ItemServiceImpl implements ItemService {
 
    private RedisHandler redisHandler;
    
    private RedisLocalCache redisLocalCache;
 
    @Override
    public Item cacheBySymbol(String symbol, boolean localcache) {
        Item item = null;
        if (localcache) {
            item = (Item) redisLocalCache.get(ItemRedisKeys.ITEM_SYMBOL + symbol);
        } else {
            item = (Item) redisHandler.get(ItemRedisKeys.ITEM_SYMBOL + symbol);
        }
 
        return item;
    }
    
    @Override
    public List<Item> cacheGetAll() {
        List<Item> list = new ArrayList<Item>();
        Map<String, Item> cache = (ConcurrentHashMap<String, Item>) redisHandler.get(ItemRedisKeys.ITEM_MAP);
        if (null == cache || cache.values().size() <= 0) {
            return list;
        }
        for (Item item : cache.values()) {
            list.add(item);
        }
        return list;
    }
 
    public List<Item> cacheGetByMarket(String symbol) {
        List<Item> cacheGetAll = cacheGetAll();
        if (StringUtils.isNullOrEmpty(symbol)) {
            return cacheGetAll;
        }
        List<Item> result = new ArrayList<Item>();
        for (Item item : cacheGetAll) {
            if (symbol.equals(item.getSymbol()))
                result.add(item);
        }
        return result;
    }
 
    @Override
    public void update(Item entity) {
        if(null==entity) return;
        ApplicationUtil.executeUpdate(entity, "WHERE UUID=?", new Object[] {entity.getId()});
        redisHandler.setSync(ItemRedisKeys.ITEM_SYMBOL + entity.getSymbol(), entity);
        redisLocalCache.put(ItemRedisKeys.ITEM_SYMBOLDATA + entity.getSymbol_data(), entity);
    }
    
    @Override
    public Item cacheBySymbolData(String symbol_data) {
        Item item = (Item) redisLocalCache.get(ItemRedisKeys.ITEM_SYMBOLDATA + symbol_data);
        if (item == null) {
            Map<String, Item> cache = (ConcurrentHashMap<String, Item>) redisHandler.get(ItemRedisKeys.ITEM_MAP);
            for (String key : cache.keySet()) {
                Item item_cache = cache.get(key);
                if (item_cache.getSymbol_data().equals(symbol_data)) {
                    redisLocalCache.put(ItemRedisKeys.ITEM_SYMBOLDATA + symbol_data, item_cache);
                    return item_cache;
                }
            }
        }
        return item;
    }
    
    @Override
    public HashMap<String,Item> cacheBySymbolDatas(Set<String> symbol_datas) {
        if(null==symbol_datas || symbol_datas.isEmpty()) return null;
        Set<String> symbolDataSet=symbol_datas.stream().map(ele->ItemRedisKeys.ITEM_SYMBOLDATA+ele).collect(Collectors.toSet());
        
        HashMap<String,Item> filterMap=new HashMap<String,Item>();
        HashMap<String,Item> redisMap=redisLocalCache.getMap(symbolDataSet);
        if(redisMap.containsValue(null)) {
            Map<String, Item> redisCache=(ConcurrentHashMap<String, Item>) redisHandler.get(ItemRedisKeys.ITEM_MAP);
            Map<String, Item> transferMap=redisCache.values().stream().collect(Collectors.toMap(item->item.getSymbol_data(),item->item,(oldVal,newVal)->newVal));
            for(Map.Entry<String,Item> entry:redisMap.entrySet()) {
                if(null!=entry.getValue()) continue;
                String prefixSymbolData=entry.getKey();
                String symbolData=prefixSymbolData.substring(ItemRedisKeys.ITEM_SYMBOLDATA.length());
                Item cacheItem=transferMap.get(symbolData);
                if(null==cacheItem) continue;
                redisLocalCache.put(prefixSymbolData, cacheItem);
                filterMap.put(symbolData, cacheItem);
            }
        }
        
        return filterMap;
    }
 
    @Override
    public void add(Item entity) {
        if(null==entity) return;
        if(null==entity.getId()) entity.setId(ApplicationUtil.getCurrentTimeUUID());
        
        ApplicationUtil.executeInsert(entity);
        redisHandler.setSync(ItemRedisKeys.ITEM_SYMBOL + entity.getSymbol(), entity);
        
        //同时添加到map
        Map<String, Item> cache = (ConcurrentHashMap<String, Item>) redisHandler.get(ItemRedisKeys.ITEM_MAP);
        if(null == cache) cache = new ConcurrentHashMap<String, Item>();
        
        cache.put(entity.getSymbol(), entity);
        redisHandler.setSync(ItemRedisKeys.ITEM_MAP, cache);
    }
 
    @Override
    public List<ItemLever> findLever(String item_id) {
        Map<String, ItemLever> map = (Map<String, ItemLever>)redisHandler.get(ItemRedisKeys.ITEM_LEVER_ID + item_id);
        if (map != null) {
            List<ItemLever> list = new ArrayList<ItemLever>(map.values());
            Collections.sort(list, new Comparator<ItemLever>() {//按倍率排序
                @Override
                public int compare(ItemLever arg0, ItemLever arg1) {
                    return new Double(arg0.getLever_rate()).compareTo(arg1.getLever_rate());
                }
            });
            return list;
        }
        return new ArrayList<ItemLever>();
    }
    
    /**
     * 获取所有币种名称
     */
    @Override
    public List<String> cacheGetAllSymbol() {
        List<Item> cacheGetAll = cacheGetAll();
        List<String> data = new ArrayList<String>();
        for (Item item : cacheGetAll) {
            data.add(item.getSymbol());
        }
        return data;
    }
 
    public void setRedisHandler(RedisHandler redisHandler) {
        this.redisHandler = redisHandler;
    }
 
    public void setRedisLocalCache(RedisLocalCache redisLocalCache) {
        this.redisLocalCache = redisLocalCache;
    }
}