/
EventCache.java
159 lines (141 loc) · 5.27 KB
/
EventCache.java
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
/*
* Copyright 2015-2019 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.dv8tion.jda.internal.handle;
import gnu.trove.iterator.TLongObjectIterator;
import gnu.trove.map.TLongObjectMap;
import gnu.trove.map.hash.TLongObjectHashMap;
import net.dv8tion.jda.api.utils.data.DataObject;
import net.dv8tion.jda.internal.utils.CacheConsumer;
import net.dv8tion.jda.internal.utils.JDALogger;
import org.slf4j.Logger;
import java.util.EnumMap;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
public class EventCache
{
public static final Logger LOG = JDALogger.getLog(EventCache.class);
/** Sequence difference after which events will be removed from cache */
public static final long TIMEOUT_AMOUNT = 100;
private final EnumMap<Type, TLongObjectMap<List<CacheNode>>> eventCache = new EnumMap<>(Type.class);
private final boolean cacheUsers;
public EventCache(boolean cacheUsers)
{
this.cacheUsers = cacheUsers;
}
public synchronized void timeout(final long responseTotal)
{
if (eventCache.isEmpty())
return;
AtomicInteger count = new AtomicInteger();
eventCache.forEach((type, map) ->
{
if (map.isEmpty())
return;
TLongObjectIterator<List<CacheNode>> iterator = map.iterator();
while (iterator.hasNext())
{
iterator.advance();
long triggerId = iterator.key();
List<CacheNode> cache = iterator.value();
//Remove when this node is more than 100 events ago
cache.removeIf(node ->
{
boolean remove = responseTotal - node.responseTotal > TIMEOUT_AMOUNT;
if (remove)
{
count.incrementAndGet();
LOG.trace("Removing type {}/{} from event cache with payload {}", type, triggerId, node.event);
}
return remove;
});
if (cache.isEmpty())
iterator.remove();
}
});
int amount = count.get();
if (amount > 0)
LOG.debug("Removed {} events from cache that were too old to be recycled", amount);
}
public synchronized void cache(Type type, long triggerId, long responseTotal, DataObject event, CacheConsumer handler)
{
if (type == Type.USER && !cacheUsers)
return;
TLongObjectMap<List<CacheNode>> triggerCache =
eventCache.computeIfAbsent(type, k -> new TLongObjectHashMap<>());
List<CacheNode> items = triggerCache.get(triggerId);
if (items == null)
{
items = new LinkedList<>();
triggerCache.put(triggerId, items);
}
items.add(new CacheNode(responseTotal, event, handler));
}
public synchronized void playbackCache(Type type, long triggerId)
{
TLongObjectMap<List<CacheNode>> typeCache = this.eventCache.get(type);
if (typeCache == null)
return;
List<CacheNode> items = typeCache.remove(triggerId);
if (items != null && !items.isEmpty())
{
EventCache.LOG.debug("Replaying {} events from the EventCache for type {} with id: {}",
items.size(), type, triggerId);
for (CacheNode item : items)
item.execute();
}
}
public synchronized int size()
{
return (int) eventCache.values().stream()
.mapToLong(typeMap ->
typeMap.valueCollection().stream().mapToLong(List::size).sum())
.sum();
}
public synchronized void clear()
{
eventCache.clear();
}
public synchronized void clear(Type type, long id)
{
TLongObjectMap<List<CacheNode>> typeCache = this.eventCache.get(type);
if (typeCache == null)
return;
List<CacheNode> events = typeCache.remove(id);
if (events != null)
LOG.debug("Clearing cache for type {} with ID {} (Size: {})", type, id, events.size());
}
public enum Type
{
USER, MEMBER, GUILD, CHANNEL, ROLE, RELATIONSHIP, CALL
}
private class CacheNode
{
private final long responseTotal;
private final DataObject event;
private final CacheConsumer callback;
public CacheNode(long responseTotal, DataObject event, CacheConsumer callback)
{
this.responseTotal = responseTotal;
this.event = event;
this.callback = callback;
}
void execute()
{
callback.execute(responseTotal, event);
}
}
}