package com.github.ryanbrainard.richsobjects.api.client;

import com.github.ryanbrainard.richsobjects.api.model.BasicSObjectInformation;
import com.github.ryanbrainard.richsobjects.api.model.GlobalDescription;
import com.github.ryanbrainard.richsobjects.api.model.QueryResult;
import com.github.ryanbrainard.richsobjects.api.model.SObjectDescription;

import java.util.Map;

/**
 * @author Ryan Brainard
 */
@SuppressWarnings("UnusedDeclaration")
class SimpleInMemoryUserCache implements SfdcApiUserCache {
    
    private final SfdcApiClient api;
    private GlobalDescription cachedGlobalDescription;
    private Map<String,BasicSObjectInformation> cachedBasicSObjectInfos;
    private Map<String,SObjectDescription> cachedSObjectDescriptions;
    private Map<String, Map<String, ?>> cachedSObjects;

    SimpleInMemoryUserCache(SfdcApiClient api) {
        this.api = api;
        invalidate();
    }

    @Override
    public void invalidate() {
        cachedGlobalDescription = null;
        cachedBasicSObjectInfos = LruMap.newSync(5);
        cachedSObjectDescriptions = LruMap.newSync(5);
        cachedSObjects = LruMap.newSync(10);
    }

    public GlobalDescription describeGlobal() {
        if (cachedGlobalDescription != null) {
            return cachedGlobalDescription;
        } else {
            cachedGlobalDescription = api.describeGlobal();
            return cachedGlobalDescription;
        }
    }

    public BasicSObjectInformation describeSObjectBasic(String type) {
        if (cachedBasicSObjectInfos.containsKey(type)) {
            return cachedBasicSObjectInfos.get(type);
        } else {
            final BasicSObjectInformation value = api.describeSObjectBasic(type);
            cachedBasicSObjectInfos.put(type, value);
            return value;
        }
    }

    public SObjectDescription describeSObject(String type) {
        if (cachedSObjectDescriptions.containsKey(type)) {
            return cachedSObjectDescriptions.get(type);
        } else {
            final SObjectDescription value = api.describeSObject(type);
            cachedSObjectDescriptions.put(type, value);
            return value;
        }
    }

    public String createSObject(String type, Map<String, ?> record) {
        cachedBasicSObjectInfos.remove(type);
        return api.createSObject(type, record);
    }

    public void updateSObject(String type, String id, Map<String, ?> record) {
        cachedBasicSObjectInfos.remove(type);
        cachedSObjects.remove(id);
        api.updateSObject(type, id, record);
    }

    public void deleteSObject(String type, String id) {
        cachedBasicSObjectInfos.remove(type);
        cachedSObjects.remove(id);
        api.deleteSObject(type, id);
    }

    public Map<String, ?> getSObject(String type, String id) {
        if (cachedSObjects.containsKey(id)) {
            return cachedSObjects.get(id);
        } else {
            final Map<String, ?> value = api.getSObject(type, id);
            cachedSObjects.put(id, value);
            return value;
        }
    }

    public QueryResult query(String soql) {
        return api.query(soql);
    }

    public QueryResult queryMore(String nextRecordsUrl) {
        return api.queryMore(nextRecordsUrl);
    }

    public String getRawBase64Content(String contentUrl) {
        return api.getRawBase64Content(contentUrl);
    }
}

@@ -601,81 +601,8 @@
 on;%0A
-    private Map%3CString,BasicSObjectInformation%3E cachedBasicSObjectInfos;%0A
     
@@ -667,16 +667,16 @@
 ptions;%0A
+
     priv
@@ -915,61 +915,8 @@
 ll;%0A
-        cachedBasicSObjectInfos = LruMap.newSync(5);%0A
     
@@ -1378,299 +1378,46 @@
     
-if (cachedBasicSObjectInfos.containsKey(type)) %7B%0A            return cachedBasicSObjectInfos.get(type);%0A        %7D else %7B%0A            final BasicSObjectInformation value = api.describeSObjectBasic(type);%0A            cachedBasicSObjectInfos.put(type, value);%0A            return value;%0A        %7D
+return api.describeSObjectBasic(type);
 %0A   
@@ -1858,54 +1858,8 @@
 ) %7B%0A
-        cachedBasicSObjectInfos.remove(type);%0A
     
@@ -1992,54 +1992,8 @@
 ) %7B%0A
-        cachedBasicSObjectInfos.remove(type);%0A
     
@@ -2067,32 +2067,32 @@
 record);%0A    %7D%0A%0A
+
     public void 
@@ -2135,54 +2135,8 @@
 ) %7B%0A
-        cachedBasicSObjectInfos.remove(type);%0A
     

Exclude recent items from caching in simple-cache Fixes #1