001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 Echo Three, LLC
003//
004// Licensed under the Apache License, Version 2.0 (the "License");
005// you may not use this file except in compliance with the License.
006// You may obtain a copy of the License at
007//
008//     http://www.apache.org/licenses/LICENSE-2.0
009//
010// Unless required by applicable law or agreed to in writing, software
011// distributed under the License is distributed on an "AS IS" BASIS,
012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013// See the License for the specific language governing permissions and
014// limitations under the License.
015// --------------------------------------------------------------------------------
016
017package com.echothree.util.server.persistence.valuecache;
018
019import com.echothree.util.common.persistence.BasePK;
020import com.echothree.util.server.persistence.BaseValue;
021import com.echothree.util.server.persistence.PersistenceDebugFlags;
022import org.apache.commons.logging.Log;
023import org.apache.commons.logging.LogFactory;
024
025public class LoggingValueCacheImpl
026        implements ValueCache {
027
028    private Log log = LogFactory.getLog(this.getClass());
029    private String threadName = Thread.currentThread().getName();
030    private ValueCache wrappedValueCache;
031
032    private int putCount;
033    private int getCount;
034    private int getCacheHitCount;
035    private int removeCount;
036
037    /** Creates a new instance of LoggingValueCacheImpl */
038    public LoggingValueCacheImpl(ValueCache wrappedValueCache) {
039        this.wrappedValueCache = wrappedValueCache;
040    }
041
042    @Override
043    public void put(BaseValue baseValue) {
044        if(PersistenceDebugFlags.LogValueCacheActions) {
045            log.info(threadName + " put " + baseValue.getPrimaryKey());
046        }
047        
048        wrappedValueCache.put(baseValue);
049        putCount++;
050    }
051
052    @Override
053    public BaseValue get(BasePK basePK) {
054        if(PersistenceDebugFlags.LogValueCacheActions) {
055            log.info(threadName + " get " + basePK);
056        }
057        
058        BaseValue baseValue = wrappedValueCache.get(basePK);
059        getCount++;
060        if(baseValue != null) {
061            getCacheHitCount++;
062        }
063        
064        return baseValue;
065    }
066
067    @Override
068    public void remove(BasePK basePK) {
069        if(PersistenceDebugFlags.LogValueCacheActions) {
070            log.info(threadName + " remove " + basePK);
071        }
072        
073        wrappedValueCache.remove(basePK);
074        removeCount++;
075    }
076
077    @Override
078    public String toString() {
079        return new StringBuilder(threadName).append(" putCount = ").append(putCount).append(", getCount = ")
080                .append(getCount).append(", getCacheHitCount = ").append(getCacheHitCount).append(", removeCount = ").append(removeCount)
081                .toString();
082    }
083
084}