-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMemcache.java
More file actions
59 lines (54 loc) · 1.61 KB
/
Copy pathMemcache.java
File metadata and controls
59 lines (54 loc) · 1.61 KB
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
package leetcode;
import java.util.*;
public class Memcache {
HashMap<Integer,Integer[]> cache;
int invalid = 2147483647;
public Memcache() {
// Initialize your data structure here.
cache = new HashMap<Integer,Integer[]>();
}
public int get(int curtTime, int key) {
// Write your code here
if(this.keyExists(curtTime,key)){
return this.cache.get(key)[0];
}
return this.invalid;
}
public void set(int curtTime, int key, int value, int ttl) {
// Write your code here
if(this.cache.containsKey(key)){
this.cache.get(key)[0] = value;
this.cache.get(key)[1] = ttl + curtTime -1;
}
else{
this.cache.put(key, new Integer[]{value,ttl+curtTime-1});
}
}
public void delete(int curtTime, int key) {
// Write your code here
if(this.cache.containsKey(key))
this.cache.remove(key);
}
public int incr(int curtTime, int key, int delta) {
// Write your code here
if(this.keyExists(curtTime,key)){
int newValue = this.cache.get(key)[0] + delta;
this.cache.get(key)[0] = newValue;
return newValue;
}
return this.invalid;
}
private boolean keyExists(int curtTime,int key){
if (this.cache.containsKey(key)){
if(this.cache.get(key)[1] < curtTime)
return true;
else
this.delete(curtTime,key);
}
return false;
}
public int decr(int curtTime, int key, int delta) {
// Write your code here
return this.incr(curtTime,key,-delta);
}
}