Created
April 13, 2018 22:32
-
-
Save wobondar/9e5b14087034d2b0c094f272cbed198f to your computer and use it in GitHub Desktop.
Private cache key
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
| private static class Key { | |
| private final Object key; | |
| private final long timelife; | |
| public Key(Object key, long timeout) { | |
| this.key = key; | |
| this.timelife = System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(timeout); | |
| } | |
| public Key(Object key) { | |
| this.key = key; | |
| this.timelife = TimeUnit.SECONDS.toMillis(300L); | |
| } | |
| public Object getKey() { | |
| return key; | |
| } | |
| public boolean isLive(long currentTimeMillis) { | |
| return currentTimeMillis < timelife; | |
| } | |
| @Override | |
| public boolean equals(Object obj) { | |
| if (obj == null) { | |
| return false; | |
| } | |
| if (getClass() != obj.getClass()) { | |
| return false; | |
| } | |
| final Key other = (Key) obj; | |
| if (this.key != other.key && (this.key == null || !this.key.equals(other.key))) { | |
| return false; | |
| } | |
| return true; | |
| } | |
| @Override | |
| public int hashCode() { | |
| int hash = 7; | |
| hash = 43 * hash + (this.key != null ? this.key.hashCode() : 0); | |
| return hash; | |
| } | |
| @Override | |
| public String toString() { | |
| return "Key{" + "key=" + key + '}'; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment