Last active
June 12, 2017 21:33
-
-
Save naosim/bade9acd711448b60b660270c1feb99e to your computer and use it in GitHub Desktop.
Cache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javaslang.Tuple; | |
import javaslang.Tuple2; | |
import javaslang.control.Option; | |
import java.time.LocalDateTime; | |
import javaslang.collection.LinkedHashMap; | |
public class Cache<K, V> { | |
private LinkedHashMap<K, Tuple2<V, LocalDateTime>> linkedHashMap = LinkedHashMap.empty(); | |
private final int maxCount; | |
private final long timeLength;// 保存時間[s] | |
public Cache(int maxCount, long timeLength) { | |
this.maxCount = maxCount; | |
this.timeLength = timeLength; | |
} | |
public void add(K key, V value, LocalDateTime now) { | |
// すでにあったら消す | |
if(linkedHashMap.containsKey(key)) { | |
linkedHashMap = linkedHashMap.remove(key); | |
} | |
// 入れる | |
linkedHashMap = linkedHashMap.put(key, Tuple.of(value, now)); | |
// 上限超えたら消す | |
if(linkedHashMap.size() > maxCount) { | |
linkedHashMap = linkedHashMap.drop(1); | |
} | |
} | |
public Option<V> get(K key, LocalDateTime now) { | |
return linkedHashMap.get(key) | |
.filter(v -> v._2().isAfter(now.minusSeconds(timeLength)))// 保存期間内かチェック。外なら削除 | |
.map(v -> v._1()); | |
} | |
public void clear() { | |
linkedHashMap.keySet().toStream().forEach(linkedHashMap::remove); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import spock.lang.Specification | |
import spock.lang.Unroll | |
import java.time.LocalDateTime | |
@Unroll | |
class CacheSpec extends Specification { | |
def "上限"() { | |
setup: | |
def sut = new Cache<String, String>(3, 999); | |
sut.add("1", "1", LocalDateTime.now()) | |
sut.add("2", "2", LocalDateTime.now()) | |
sut.add("3", "3", LocalDateTime.now()) | |
sut.add("1", "1", LocalDateTime.now()) | |
sut.add("4", "4", LocalDateTime.now()) | |
expect: | |
sut.get("1", LocalDateTime.now()).isDefined() //後半に入れたから取れる | |
sut.get("2", LocalDateTime.now()).isEmpty() | |
sut.get("3", LocalDateTime.now()).isDefined() | |
sut.get("4", LocalDateTime.now()).isDefined() | |
} | |
def "時間制限"() { | |
setup: | |
def sut = new Cache<String, String>(3, 10); | |
sut.add("0", "0", LocalDateTime.now().minusSeconds(15)) | |
sut.add("1", "1", LocalDateTime.now().minusSeconds(11)) | |
sut.add("2", "2", LocalDateTime.now().minusSeconds(8)) | |
sut.add("0", "0", LocalDateTime.now()) | |
expect: | |
sut.get("0", LocalDateTime.now()).isDefined() // 後で入れてるから取れる | |
sut.get("1", LocalDateTime.now()).isEmpty()// 時間制限外なので取れない | |
sut.get("2", LocalDateTime.now()).isDefined() | |
} | |
def "clear"() { | |
setup: | |
def sut = new Cache<String, String>(3, 999); | |
sut.add("1", "1", LocalDateTime.now()) | |
sut.clear() | |
expect: | |
sut.get("1", LocalDateTime.now()).isEmpty() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment