package io.teknek.intravert.service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

import com.google.common.cache.ForwardingCache;
import com.google.common.cache.LoadingCache;

public class RequestContext {

  private static final AtomicLong SESSION_ID = new AtomicLong(0);
  private static Map<Long,Session> SAVED = new HashMap<Long,Session>();
  //private static LoadingCache<Long,Session> CACHE; 
  private Session session;
  
  
  public RequestContext(){
    
  }
  
  public Session recoverSession(Long l){
    session = SAVED.get(l);
    return session;
  }
  
  public Long saveSession(){
    Long id = SESSION_ID.getAndIncrement();
    SAVED.put(id, getSession());
    return id;
  }
  
  public Session getSession(){
    if (session == null){
      session = new Session();
    }
    return session;
  }
  
}

@@ -111,27 +111,251 @@
 ent.
-atomic.AtomicLong;%0A
+ExecutionException;%0Aimport java.util.concurrent.TimeUnit;%0Aimport java.util.concurrent.atomic.AtomicLong;%0A%0Aimport org.mortbay.io.BufferCache.CachedBuffer;%0A%0Aimport com.google.common.cache.CacheBuilder;%0Aimport com.google.common.cache.CacheLoader;
 %0Aimp
@@ -559,19 +559,28 @@
  static 
-Map
+LoadingCache
 %3CLong,Se
@@ -590,128 +590,399 @@
 on%3E 
-SAVED = new HashMap%3CLong,S
+CACHE; %0A  private Session s
 ession
-%3E()
 ;%0A  
-//private static LoadingCache%3CLong,Session%3E CACHE; %0A  private Session session
+%0A  static %7B%0A    CacheLoader %3CLong,Session%3E loader = new CacheLoader%3CLong,Session%3E()%7B%0A      @Override%0A      public Session load(Long id) throws Exception %7B%0A        return new Session();%0A      %7D%0A    %7D;%0A    CACHE = CacheBuilder.newBuilder()%0A            .maximumSize(10000)%0A            .expireAfterAccess(60, TimeUnit.SECONDS)%0A            .build(loader)
 ;%0A  
+%7D
 %0A  %0A
@@ -1057,24 +1057,36 @@
 on(Long l)%7B%0A
+    try %7B%0A  
     session 
@@ -1087,21 +1087,21 @@
 ssion = 
-SAVED
+CACHE
 .get(l);
@@ -1101,16 +1101,96 @@
 get(l);%0A
+    %7D catch (ExecutionException e) %7B%0A      throw new RuntimeException(e);%0A    %7D%0A
     retu
@@ -1289,13 +1289,13 @@
     
-SAVED
+CACHE
 .put

Use loading cache to manage sessions