|
|
此文章由 wangbo1118 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 wangbo1118 所有!转贴必须注明作者、出处和本声明,并保持内容完整
写了个main跑个简单测试
public class CacheTestMain {
public static void main(String[] args) throws InterruptedException {
CacheImpl cache = new CacheImpl();
cache.put("Alice", "value1", 10);
cache.put("Bob", "value2", 5);
cache.put("Chris", "value3", 15);
cache.put("David", "value4", 8);
cache.put("Eric", "value5", 12);
Thread.sleep(1000);
System.out.println("--------------Time:1s---------------" );
/*
* first round, after 1s
* all the values are there
*/
print(cache);
System.out.println("--------------Time:6s---------------" );
/*
* second round, after 6s
* Bob's record is cleared from cache since its ttl is 5s
* in this round, Alice & David have been updated
* 8s & 11s start from 6s point
*/
Thread.sleep(5000);
print(cache);
cache.put("Alice", "new_value1", 8);
cache.put("David", "new_value4", 11);
System.out.println("--------------Time:11s---------------" );
/*
* third round, after 11s
* originally, Alice & David should been cleared
* BUT, they had been updated in the last round,
* So, we can see them with new value in this round
*/
Thread.sleep(5000);
print(cache);
System.out.println("--------------Time:16s---------------" );
/*
* 4th round, after 16s
* all expired/cleared except David, becasue it had been updated at 6s with 11s ttl
* 11 + 6 = 17 s , so it will expired 1s later.
*/
Thread.sleep(5000);
print(cache);
System.out.println("-------------END----------------" );
}
public static void print(CacheImpl cache){
System.out.println("Alice \t| \t" + cache.get( "Alice" ));
System.out.println("Bob \t| \t" + cache.get( "Bob" ));
System.out.println("Chris \t| \t" + cache.get( "Chris" ));
System.out.println("David \t| \t" + cache.get( "David" ));
System.out.println("Eric \t| \t" + cache.get( "Eric" ));
}
} |
|