001// --------------------------------------------------------------------------------
002// Copyright 2002-2026 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.slf4j.Logger;
023import org.slf4j.LoggerFactory;
024
025public class LoggingValueCacheImpl
026        implements ValueCache {
027
028    protected final Logger log = LoggerFactory.getLogger(this.getClass());
029
030    private String threadName = Thread.currentThread().getName();
031    private ValueCache wrappedValueCache;
032
033    private int putCount;
034    private int getCount;
035    private int getCacheHitCount;
036    private int removeCount;
037
038    /** Creates a new instance of LoggingValueCacheImpl */
039    public LoggingValueCacheImpl(ValueCache wrappedValueCache) {
040        this.wrappedValueCache = wrappedValueCache;
041    }
042
043    @Override
044    public void put(BaseValue baseValue) {
045        if(PersistenceDebugFlags.LogValueCacheActions) {
046            log.info(threadName + " put " + baseValue.getPrimaryKey());
047        }
048        
049        wrappedValueCache.put(baseValue);
050        putCount++;
051    }
052
053    @Override
054    public BaseValue get(BasePK basePK) {
055        if(PersistenceDebugFlags.LogValueCacheActions) {
056            log.info(threadName + " get " + basePK);
057        }
058
059        var baseValue = wrappedValueCache.get(basePK);
060        getCount++;
061        if(baseValue != null) {
062            getCacheHitCount++;
063        }
064        
065        return baseValue;
066    }
067
068    @Override
069    public void remove(BasePK basePK) {
070        if(PersistenceDebugFlags.LogValueCacheActions) {
071            log.info(threadName + " remove " + basePK);
072        }
073        
074        wrappedValueCache.remove(basePK);
075        removeCount++;
076    }
077
078    @Override
079    public String toString() {
080        return threadName + " putCount = " + putCount + ", getCount = " +
081                getCount + ", getCacheHitCount = " + getCacheHitCount + ", removeCount = " + removeCount;
082    }
083
084}