001package org.cache2k.impl; 002 003/* 004 * #%L 005 * cache2k core package 006 * %% 007 * Copyright (C) 2000 - 2015 headissue GmbH, Munich 008 * %% 009 * This program is free software: you can redistribute it and/or modify 010 * it under the terms of the GNU General Public License as 011 * published by the Free Software Foundation, either version 3 of the 012 * License, or (at your option) any later version. 013 * 014 * This program is distributed in the hope that it will be useful, 015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 017 * GNU General Public License for more details. 018 * 019 * You should have received a copy of the GNU General Public 020 * License along with this program. If not, see 021 * <http://www.gnu.org/licenses/gpl-3.0.html>. 022 * #L% 023 */ 024 025/** 026 * Random eviction without usage counters. 027 * 028 * @author Jens Wilke; created: 2013-12-22 029 */ 030public class RandomCache<K, T> extends LockFreeCache<Entry, K, T> { 031 032 int evictionIndex = 0; 033 034 @Override 035 protected void recordHit(Entry entry) { } 036 037 @Override 038 protected void removeEntryFromReplacementList(Entry e) { 039 e.removedFromList(); 040 } 041 042 @Override 043 protected void insertIntoReplacementList(Entry e) { 044 e.next = e; 045 } 046 047 @Override 048 protected Entry newEntry() { 049 return new Entry(); 050 } 051 052 /** 053 * Start at arbitrary hash slot and evict the next best entry. 054 */ 055 @Override 056 protected Entry findEvictionCandidate() { 057 Entry[] h0 = mainHash; 058 Entry[] h1 = refreshHash; 059 int idx = evictionIndex % (h0.length + h1.length); 060 if (idx >= h0.length) { 061 idx -= h0.length; 062 Entry[] t = h0; 063 h0 = h1; 064 h1 = t; 065 } 066 while (h0[idx] == null) { 067 idx++; 068 if (idx >= h0.length) { 069 Entry[] t = h0; 070 h0 = h1; 071 h1 = t; 072 idx = 0; 073 } 074 } 075 evictionIndex += h0[idx].hashCode; 076 if (evictionIndex < 0) { 077 evictionIndex = -evictionIndex; 078 } 079 return h0[idx]; 080 } 081 082 @Override 083 public long getHitCnt() { 084 return 0; 085 } 086 087}