databases. This cache now takes up 10% of all cache sizes, 90% goes to the node routing cache we had so far. git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@2069 6c8d7289-2bf4-0310-a012-ef5d649a1542pull/1/head
parent
0dfef19559
commit
bab9ee38e7
@ -0,0 +1,186 @@
|
||||
// kelondroObjectCache.java
|
||||
// ------------------------
|
||||
// (C) by Michael Peter Christen; mc@anomic.de
|
||||
// first published on http://www.anomic.de
|
||||
// Frankfurt, Germany, 2006
|
||||
//
|
||||
// This is a part of the kelondro database, which is a part of YaCy
|
||||
//
|
||||
// $LastChangedDate: 2006-04-02 22:40:07 +0200 (So, 02 Apr 2006) $
|
||||
// $LastChangedRevision: 1986 $
|
||||
// $LastChangedBy: orbiter $
|
||||
//
|
||||
//
|
||||
// LICENSE
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation; either version 2 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
//
|
||||
//
|
||||
// A NOTE FROM THE AUTHOR TO THE USERS:
|
||||
//
|
||||
// Using this software in any meaning (reading, learning, copying, compiling,
|
||||
// running) means that you agree that the Author(s) is (are) not responsible
|
||||
// for cost, loss of data or any harm that may be caused directly or indirectly
|
||||
// by usage of this softare or this documentation. The usage of this software
|
||||
// is on your own risk. The installation and usage (starting/running) of this
|
||||
// software may allow other people or application to access your computer and
|
||||
// any attached devices and is highly dependent on the configuration of the
|
||||
// software which must be done by the user of the software; the author(s) is
|
||||
// (are) also not responsible for proper configuration and usage of the
|
||||
// software, even if provoked by documentation provided together with
|
||||
// the software.
|
||||
//
|
||||
//
|
||||
// A NOTE FROM THE AUTHOR TO DEVELOPERS:
|
||||
//
|
||||
// Contributions and changes to the program code should be marked as such:
|
||||
// Please enter your own (C) notice below; they must be compatible with the GPL.
|
||||
// Please mark also all changes in the code; if you don't mark them then they
|
||||
// can't be identified; thus all unmarked code belong to the copyright holder
|
||||
// as mentioned above. A good documentation of code authorities will also help
|
||||
// to maintain the code and the project.
|
||||
// A re-distribution must contain the intact and unchanged copyright statement.
|
||||
|
||||
|
||||
package de.anomic.kelondro;
|
||||
|
||||
import java.util.TreeMap;
|
||||
|
||||
public class kelondroObjectCache {
|
||||
|
||||
private final TreeMap cache;
|
||||
private final kelondroMScoreCluster ages;
|
||||
private long startTime;
|
||||
private int maxSize;
|
||||
private long maxAge;
|
||||
private long minMem;
|
||||
private int readHit, readMiss, writeUnique, writeDouble;
|
||||
|
||||
public kelondroObjectCache(int maxSize, long maxAge, long minMem) {
|
||||
this.cache = new TreeMap();
|
||||
this.ages = new kelondroMScoreCluster();
|
||||
this.startTime = System.currentTimeMillis();
|
||||
this.maxAge = maxAge;
|
||||
this.minMem = minMem;
|
||||
this.readHit = 0;
|
||||
this.readMiss = 0;
|
||||
this.writeUnique = 0;
|
||||
this.writeDouble = 0;
|
||||
}
|
||||
|
||||
public void setMaxAge(long maxAge) {
|
||||
this.maxAge = maxAge;
|
||||
}
|
||||
|
||||
public void setMaxSize(int maxSize) {
|
||||
this.maxSize = maxSize;
|
||||
}
|
||||
|
||||
public void setMinMem(int minMem) {
|
||||
this.minMem = minMem;
|
||||
}
|
||||
|
||||
public long minAge() {
|
||||
if (ages.size() == 0) return 0;
|
||||
return System.currentTimeMillis() - longEmit(ages.getMaxScore());
|
||||
}
|
||||
|
||||
public long maxAge() {
|
||||
if (ages.size() == 0) return 0;
|
||||
return System.currentTimeMillis() - longEmit(ages.getMinScore());
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return cache.size();
|
||||
}
|
||||
|
||||
private int intTime(long longTime) {
|
||||
return (int) Math.max(0, ((longTime - startTime) / 1000));
|
||||
}
|
||||
|
||||
private long longEmit(int intTime) {
|
||||
return (((long) intTime) * (long) 1000) + startTime;
|
||||
}
|
||||
|
||||
public void put(byte[] key, Object value) {
|
||||
if (key != null) put(new String(key), value);
|
||||
}
|
||||
|
||||
public void put(String key, Object value) {
|
||||
if (key == null) return;
|
||||
Object prev = null;
|
||||
synchronized(cache) {
|
||||
prev = cache.put(key, value);
|
||||
ages.setScore(key, intTime(System.currentTimeMillis()));
|
||||
}
|
||||
if (prev == null) this.writeUnique++; else this.writeDouble++;
|
||||
flush();
|
||||
}
|
||||
|
||||
public Object get(byte[] key) {
|
||||
if (key == null) return null;
|
||||
Object r = cache.get(new String(key));
|
||||
flush();
|
||||
if (r == null) this.readMiss++; else this.readHit++;
|
||||
return r;
|
||||
}
|
||||
|
||||
public Object get(String key) {
|
||||
if (key == null) return null;
|
||||
Object r = cache.get(key);
|
||||
flush();
|
||||
if (r == null) this.readMiss++; else this.readHit++;
|
||||
return r;
|
||||
}
|
||||
|
||||
public void remove(byte[] key) {
|
||||
remove(new String(key));
|
||||
}
|
||||
|
||||
public void remove(String key) {
|
||||
if (key == null) return;
|
||||
synchronized(cache) {
|
||||
cache.remove(key);
|
||||
ages.deleteScore(key);
|
||||
}
|
||||
flush();
|
||||
}
|
||||
|
||||
public void flush() {
|
||||
String k;
|
||||
synchronized(cache) {
|
||||
while ((ages.size() > 0) &&
|
||||
((size() > maxSize) ||
|
||||
(longEmit(ages.getMinScore()) > maxAge) ||
|
||||
(Runtime.getRuntime().freeMemory() < minMem)) &&
|
||||
((k = bestFlush()) != null)) {
|
||||
cache.remove(k);
|
||||
ages.deleteScore(k);
|
||||
if (Runtime.getRuntime().freeMemory() < minMem) System.gc(); // prevent unnecessary loops
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String bestFlush() {
|
||||
if (cache.size() == 0) return null;
|
||||
try {
|
||||
synchronized (cache) {
|
||||
return (String) ages.getMinObject(); // flush oldest entries
|
||||
}
|
||||
} catch (Exception e) {}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue