package com.avairebot.orion.cache.adapters;

import com.avairebot.orion.cache.CacheItem;
import com.avairebot.orion.contracts.cache.CacheAdapter;
import com.avairebot.orion.contracts.cache.CacheClosure;

import java.util.Collections;
import java.util.Map;
import java.util.WeakHashMap;

public class MemoryAdapter extends CacheAdapter {

    private final Map<String, CacheItem> caches = Collections.synchronizedMap(new WeakHashMap<String, CacheItem>());

    @Override
    public synchronized boolean put(String token, Object value, int seconds) {
        caches.put(token, new CacheItem(token, value, System.currentTimeMillis() + (seconds * 1000)));
        return true;
    }

    @Override
    public synchronized Object remember(String token, int seconds, CacheClosure closure) {
        if (has(token)) {
            return get(token);
        }

        CacheItem item = new CacheItem(token, closure.run(), System.currentTimeMillis() + (seconds * 1000));
        caches.put(token, item);

        return item.getValue();
    }

    @Override
    public synchronized boolean forever(String token, Object value) {
        caches.put(token, new CacheItem(token, value, -1));

        return true;
    }

    @Override
    public synchronized Object get(String token) {
        if (!has(token)) {
            return null;
        }

        CacheItem item = getRaw(token);
        if (item == null) {
            return null;
        }
        return item.getValue();
    }

    @Override
    public synchronized CacheItem getRaw(String token) {
        if (!has(token)) {
            return null;
        }
        return caches.getOrDefault(token, null);
    }

    @Override
    public synchronized boolean has(String token) {
        return caches.containsKey(token) && caches.get(token).isExpired();
    }

    @Override
    public synchronized CacheItem forget(String token) {
        return caches.remove(token);
    }

    @Override
    public synchronized boolean flush() {
        caches.clear();
        return true;
    }
}

@@ -343,16 +343,23 @@
 private 
+static 
 final Ma
@@ -379,22 +379,22 @@
 heItem%3E 
-caches
+CACHES
  = Colle
@@ -549,38 +549,38 @@
 onds) %7B%0A        
-caches
+CACHES
 .put(token, new 
@@ -962,38 +962,38 @@
 1000));%0A        
-caches
+CACHES
 .put(token, item
@@ -1127,22 +1127,22 @@
         
-caches
+CACHES
 .put(tok
@@ -1626,22 +1626,22 @@
  return 
-caches
+CACHES
 .getOrDe
@@ -1748,22 +1748,22 @@
  return 
-caches
+CACHES
 .contain
@@ -1777,22 +1777,22 @@
 ken) && 
-caches
+CACHES
 .get(tok
@@ -1901,22 +1901,22 @@
  return 
-caches
+CACHES
 .remove(
@@ -1998,14 +1998,14 @@
     
-caches
+CACHES
 .cle

Change memory cache objects to static