/*
 * Copyright 2015-2016 http://hsweb.me
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.hsweb.concureent.cache.monitor;

import org.hsweb.web.core.cache.monitor.MonitorCache;
import org.springframework.cache.concurrent.ConcurrentMapCache;

import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;

public class SimpleMonitorCache extends ConcurrentMapCache implements MonitorCache {

    private final AtomicInteger totalTimes = new AtomicInteger(0);
    private final AtomicInteger hitTimes = new AtomicInteger(0);
    private final AtomicInteger putTimes = new AtomicInteger(0);

    public SimpleMonitorCache(String name) {
        super(name,false);
    }

    @Override
    public Set<Object> keySet() {
        return getNativeCache().keySet();
    }

    @Override
    public int size() {
        return getNativeCache().size();
    }

    @Override
    public long getTotalTimes() {
        return totalTimes.get();
    }

    @Override
    public long getHitTimes() {
        return hitTimes.get();
    }

    @Override
    public ValueWrapper get(Object key) {
        ValueWrapper wrapper = super.get(key);
        totalTimes.addAndGet(1);
        if (wrapper != null) {
            hitTimes.addAndGet(1);
        }
        return wrapper;
    }

    @Override
    public <T> T get(Object key, Class<T> type) {
        T value = super.get(key, type);
        totalTimes.addAndGet(1);
        if (value != null) {
            hitTimes.addAndGet(1);
        }
        return value;
    }

    @Override
    public void put(Object key, Object value) {
        if (key == null || value == null) return;
        putTimes.addAndGet(1);
        super.put(key, value);
    }

    @Override
    public ValueWrapper putIfAbsent(Object key, Object value) {
        if (key == null || value == null) return null;
        putTimes.addAndGet(1);
        return super.putIfAbsent(key, value);
    }

    @Override
    public long getPutTimes() {
        return putTimes.get();
    }
}

@@ -762,16 +762,84 @@
 Cache;%0A%0A
+import java.lang.ref.Reference;%0Aimport java.lang.ref.SoftReference;%0A
 import j
@@ -852,16 +852,16 @@
 il.Set;%0A
-
 import j
@@ -1251,16 +1251,17 @@
 er(name,
+ 
 false);%0A
@@ -1613,32 +1613,364 @@
 s.get();%0A    %7D%0A%0A
+    @Override%0A    protected Object lookup(Object key) %7B%0A        Object value = super.lookup(key);%0A        if (value != null && value instanceof Reference) %7B%0A            Reference reference = (Reference) value;%0A            if (reference != null) return reference.get();%0A            evict(key);%0A        %7D%0A        return value;%0A    %7D%0A%0A
     @Override%0A  
@@ -2428,32 +2428,128 @@
 n value;%0A    %7D%0A%0A
+    protected Object buildValue(Object value) %7B%0A        return new SoftReference(value);%0A    %7D%0A%0A
     @Override%0A  
@@ -2694,30 +2694,42 @@
 er.put(key, 
+buildValue(
 value)
+)
 ;%0A    %7D%0A%0A   
@@ -2927,22 +2927,34 @@
 nt(key, 
+buildValue(
 value)
+)
 ;%0A    %7D%0A
@@ -3031,16 +3031,17 @@
 .get();%0A
-
     %7D%0A
+%0A
 %7D%0A

优化,使用SoftReference作为value,在内存不够的时候,自动释放缓存