• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

alibaba / jetcache / #405

16 Apr 2024 05:58AM UTC coverage: 0.0% (-88.9%) from 88.866%
#405

push

areyouok
add encoding to fix coverage report

0 of 5353 relevant lines covered (0.0%)

0.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/jetcache-core/src/main/java/com/alicp/jetcache/support/CacheNotifyMonitor.java
1
/**
2
 * Created on 2022-05-04.
3
 */
4
package com.alicp.jetcache.support;
5

6
import com.alicp.jetcache.AbstractCache;
7
import com.alicp.jetcache.Cache;
8
import com.alicp.jetcache.CacheManager;
9
import com.alicp.jetcache.CacheMonitor;
10
import com.alicp.jetcache.CacheUtil;
11
import com.alicp.jetcache.MultiLevelCache;
12
import com.alicp.jetcache.anno.CacheConsts;
13
import com.alicp.jetcache.embedded.AbstractEmbeddedCache;
14
import com.alicp.jetcache.event.CacheEvent;
15
import com.alicp.jetcache.event.CachePutAllEvent;
16
import com.alicp.jetcache.event.CachePutEvent;
17
import com.alicp.jetcache.event.CacheRemoveAllEvent;
18
import com.alicp.jetcache.event.CacheRemoveEvent;
19

20
import java.util.function.Function;
21

22
/**
23
 * @author huangli
24
 */
25
public class CacheNotifyMonitor implements CacheMonitor {
26
    private final BroadcastManager broadcastManager;
27
    private final String area;
28
    private final String cacheName;
29
    private final String sourceId;
30

31
    public CacheNotifyMonitor(CacheManager cacheManager, String area, String cacheName) {
×
32
        this.broadcastManager = cacheManager.getBroadcastManager(area);
×
33
        this.area = area;
×
34
        this.cacheName = cacheName;
×
35
        if (broadcastManager != null) {
×
36
            this.sourceId = broadcastManager.getSourceId();
×
37
        } else {
38
            this.sourceId = null;
×
39
        }
40
    }
×
41

42
    public CacheNotifyMonitor(CacheManager cacheManager, String cacheName) {
43
        this(cacheManager, CacheConsts.DEFAULT_AREA, cacheName);
×
44
    }
×
45

46
    private Object convertKey(Object key, AbstractEmbeddedCache localCache) {
47
        Function keyConvertor = localCache.config().getKeyConvertor();
×
48
        if (keyConvertor == null) {
×
49
            return key;
×
50
        } else {
51
            return keyConvertor.apply(key);
×
52
        }
53
    }
54

55
    private AbstractEmbeddedCache getLocalCache(AbstractCache absCache) {
56
        if (!(absCache instanceof MultiLevelCache)) {
×
57
            return null;
×
58
        }
59
        for (Cache c : ((MultiLevelCache) absCache).caches()) {
×
60
            if (c instanceof AbstractEmbeddedCache) {
×
61
                return (AbstractEmbeddedCache) c;
×
62
            }
63
        }
64
        return null;
×
65
    }
66

67
    @Override
68
    public void afterOperation(CacheEvent event) {
69
        if (this.broadcastManager == null) {
×
70
            return;
×
71
        }
72
        AbstractCache absCache = CacheUtil.getAbstractCache(event.getCache());
×
73
        if (absCache.isClosed()) {
×
74
            return;
×
75
        }
76
        AbstractEmbeddedCache localCache = getLocalCache(absCache);
×
77
        if (localCache == null) {
×
78
            return;
×
79
        }
80
        if (event instanceof CachePutEvent) {
×
81
            CacheMessage m = new CacheMessage();
×
82
            m.setArea(area);
×
83
            m.setCacheName(cacheName);
×
84
            m.setSourceId(sourceId);
×
85
            CachePutEvent e = (CachePutEvent) event;
×
86
            m.setType(CacheMessage.TYPE_PUT);
×
87
            m.setKeys(new Object[]{convertKey(e.getKey(), localCache)});
×
88
            broadcastManager.publish(m);
×
89
        } else if (event instanceof CacheRemoveEvent) {
×
90
            CacheMessage m = new CacheMessage();
×
91
            m.setArea(area);
×
92
            m.setCacheName(cacheName);
×
93
            m.setSourceId(sourceId);
×
94
            CacheRemoveEvent e = (CacheRemoveEvent) event;
×
95
            m.setType(CacheMessage.TYPE_REMOVE);
×
96
            m.setKeys(new Object[]{convertKey(e.getKey(), localCache)});
×
97
            broadcastManager.publish(m);
×
98
        } else if (event instanceof CachePutAllEvent) {
×
99
            CacheMessage m = new CacheMessage();
×
100
            m.setArea(area);
×
101
            m.setCacheName(cacheName);
×
102
            m.setSourceId(sourceId);
×
103
            CachePutAllEvent e = (CachePutAllEvent) event;
×
104
            m.setType(CacheMessage.TYPE_PUT_ALL);
×
105
            if (e.getMap() != null) {
×
106
                m.setKeys(e.getMap().keySet().stream().map(k -> convertKey(k, localCache)).toArray());
×
107
            }
108
            broadcastManager.publish(m);
×
109
        } else if (event instanceof CacheRemoveAllEvent) {
×
110
            CacheMessage m = new CacheMessage();
×
111
            m.setArea(area);
×
112
            m.setCacheName(cacheName);
×
113
            m.setSourceId(sourceId);
×
114
            CacheRemoveAllEvent e = (CacheRemoveAllEvent) event;
×
115
            m.setType(CacheMessage.TYPE_REMOVE_ALL);
×
116
            if (e.getKeys() != null) {
×
117
                m.setKeys(e.getKeys().stream().map(k -> convertKey(k, localCache)).toArray());
×
118
            }
119
            broadcastManager.publish(m);
×
120
        }
121
    }
×
122
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc