001
002package org.cache2k.impl;
003
004/*
005 * #%L
006 * cache2k core package
007 * %%
008 * Copyright (C) 2000 - 2015 headissue GmbH, Munich
009 * %%
010 * This program is free software: you can redistribute it and/or modify
011 * it under the terms of the GNU General Public License as
012 * published by the Free Software Foundation, either version 3 of the 
013 * License, or (at your option) any later version.
014 * 
015 * This program is distributed in the hope that it will be useful,
016 * but WITHOUT ANY WARRANTY; without even the implied warranty of
017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
018 * GNU General Public License for more details.
019 * 
020 * You should have received a copy of the GNU General Public 
021 * License along with this program.  If not, see
022 * <http://www.gnu.org/licenses/gpl-3.0.html>.
023 * #L%
024 */
025
026/**
027 * Cache implementation with LRU eviction algorithm.
028 *
029 * @author Jens Wilke
030 */
031public class LruCache<K, T> extends BaseCache<LruCache.Entry, K, T> {
032
033  Entry<K,T> head;
034  long hitCnt;
035
036  @Override
037  public long getHitCnt() {
038    return hitCnt;
039  }
040
041  @Override
042  protected void recordHit(Entry e) {
043    removeEntryFromReplacementList(e);
044    insertInList(head, e);
045    hitCnt++;
046  }
047
048  @Override
049  protected void insertIntoReplacementList(Entry e) {
050    insertInList(head, e);
051  }
052
053  @Override
054  protected Entry newEntry() {
055    return new Entry<K, T>();
056  }
057
058
059  @Override
060  protected Entry findEvictionCandidate() {
061    return head.prev;
062  }
063
064  @Override
065  protected void initializeHeapCache() {
066    super.initializeHeapCache();
067    head = new Entry<K,T>().shortCircuit();
068  }
069
070  @Override
071  protected IntegrityState getIntegrityState() {
072    synchronized (lock) {
073      return super.getIntegrityState()
074        .checkEquals("size = list entry count", getLocalSize() , getListEntryCount(head));
075    }
076  }
077
078  protected static class Entry<K,T> extends org.cache2k.impl.Entry<Entry<K,T>, K, T> {
079
080  }
081
082}