package com.mars.test.guava;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.Maps;
import com.google.common.math.DoubleMath;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.util.Map;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * Created by mars on 2016/8/31.
 */
@Slf4j
public class TestGuavaCache {

    int INTERVAL_MINUTE = 1;

    final Cache<String, Long> normalCache = CacheBuilder.newBuilder()
            .expireAfterWrite(INTERVAL_MINUTE, TimeUnit.SECONDS)
            .build();

    @Test
    public void testCache() throws ExecutionException {
        normalCache.put("pt111",System.currentTimeMillis());
        normalCache.put("mg112",System.currentTimeMillis());
        normalCache.put("qt113",System.currentTimeMillis());
        ConcurrentMap<String, Long> map = normalCache.asMap();
        for(String key : map.keySet()){
            log.info("map key = " + key +" , value = " + map.get(key));
        }
        try {
            Thread.sleep(2000);                 //1000 milliseconds is one second.
        } catch(InterruptedException ex) {
            Thread.currentThread().interrupt();
        }
        map = normalCache.asMap();
        for(String key : map.keySet()){
            log.info("map key = " + key +" , value = " + map.get(key));
        }
        log.info("map size = " + map.size());
        log.info("map get pt111 " + normalCache.asMap().get("pt111"));
        log.info("done");
    }


    final Map<String,Long> mockDao = Maps.newConcurrentMap();
    final LoadingCache<String, Long> loadingCache = CacheBuilder.newBuilder()
            .maximumSize(10000)
            .expireAfterWrite(1, TimeUnit.SECONDS)
            .build(
                    new CacheLoader<String, Long>() {
                        @Override
                        public Long load(String key) throws Exception {
                            return mockDao.get(key);
                        }
                    });

    @Test
    public void testLoadingCache() throws ExecutionException, InterruptedException {
        long timeStamp = System.currentTimeMillis();
        mockDao.put("pt111",timeStamp);
        // update pt111 value
        Thread.sleep(500);                 //1000 milliseconds is one second.
        long newTimeStamp = System.currentTimeMillis();
        mockDao.put("pt111",newTimeStamp);
        long cachedTimeStamp = loadingCache.get("pt111");
        Thread.sleep(2000);                 //1000 milliseconds is one second.
        cachedTimeStamp = loadingCache.get("pt111");
        log.info("done");
    }


}
@@ -1077,38 +1077,49 @@
     
-for(String key : map.keySet())
+normalCache.asMap().forEach((key,value)-%3E
 %7B%0A  
@@ -1177,129 +1177,177 @@
 %22 + 
-map.get(key));%0A        %7D%0A        try %7B%0A            Thread.sleep(2000);                 //1000 milliseconds is one second.
+value);%0A        %7D);%0A        log.info(%22map get pt111 %22 + normalCache.asMap().get(%22pt111%22));%0A        // test after time limit%0A        try %7B%0A            Thread.sleep(2000);
 %0A   
@@ -1451,22 +1451,16 @@
 %0A       
- map =
  normalC
@@ -1475,48 +1475,30 @@
 ap()
-;%0A        for(String key : map.keySet())
+.forEach((key,value)-%3E
 %7B%0A  
@@ -1556,20 +1556,13 @@
 %22 + 
-map.get(key)
+value
 );%0A 
@@ -1561,32 +1561,34 @@
 alue);%0A        %7D
+);
 %0A        log.inf

add demo code