-
Notifications
You must be signed in to change notification settings - Fork 2
/
SummonerHistory.java
57 lines (44 loc) · 1.66 KB
/
SummonerHistory.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
package com.omarathon.riotapicrawler.src.lib;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.merakianalytics.orianna.types.core.match.MatchHistory;
import com.merakianalytics.orianna.types.core.summoner.Summoner;
import java.util.Random;
import java.util.concurrent.TimeUnit;
public class SummonerHistory {
private Cache<Summoner, MatchHistory> cache;
public SummonerHistory() {
this(1, TimeUnit.DAYS);
}
public SummonerHistory(long rememberDuration, TimeUnit rememberDurationUnit) {
this(CacheBuilder.newBuilder()
.expireAfterWrite(rememberDuration, rememberDurationUnit)
.maximumSize(100000));
}
public SummonerHistory(CacheBuilder<Object, Object> builder) {
cache = builder.build();
}
public void addVisitedSummoner(Summoner summoner, MatchHistory matchHistory) {
cache.put(summoner, matchHistory);
}
public void removeVisitedSummoner(Summoner summoner) {
cache.invalidate(summoner);
}
public boolean wasVisited(Summoner summoner) {
return getMatchHistory(summoner) != null;
}
public MatchHistory getMatchHistory(Summoner summoner) {
return cache.getIfPresent(summoner);
}
public MatchHistory getRandomMatchHistory() {
Object[] values = cache.asMap().values().toArray();
if (values.length == 0) return null;
return (MatchHistory) values[new Random().nextInt(values.length)];
}
public Cache<Summoner, MatchHistory> getCache() {
return cache;
}
public void setCache(Cache<Summoner, MatchHistory> cache) {
this.cache = cache;
}
}