Anthony
June 17th, 2010, 19:32
/*
* Copyright (C) 2010 OpenRune Development Team
*
* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* Only the registered members can see the link. for more details.
*/
package org.openrune.test;
import java.io.*;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
/**
* A caching pool that caches objects to the disk
* when they have been untouched in twenty minutes.
* Purpose is to save memory.
*
* @author Anthony Snavely
*/
public class CachingPool<T extends Cacheable> extends Thread {
/* The amount of time in milliseconds between checking expiry. */
private static final int CYCLE_TIME = 5000;
/* The amount of time in milliseconds to store cached objects */
private static final int STORE_TIME = 1200000; // 1200000 ms. e.g. twenty minutes
/* Whether or not the cache is current running. */
private boolean running = true;
/* The current cached objects. */
private Map<String, T> cachedObjects = new ConcurrentHashMap<String, T>();
/* The list of current files on the disk */
private List<String> diskFiles = new CopyOnWriteArrayList<String>();
public CachingPool() {
super("CachingThread");
start();
}
/**
* Restart the cache.
*/
public void startRunning() {
this.running = true;
this.cachedObjects = new ConcurrentHashMap<String, T>();
this.diskFiles = new CopyOnWriteArrayList<String>();
start();
}
/**
* Stop the cache.
*/
public void stopRunning() {
this.running = false;
this.cachedObjects = null; // So the gc can collect the list and cached objects.
this.diskFiles = null;
}
/**
* Cache an object.
*
* @param id The identifier
* @param cacheable The cacheable object.
*/
public void cacheObject(String id, T cacheable) {
if (this.running)
cachedObjects.put(id, cacheable);
}
/**
* Get a cached object either from the dish or from memory.
*
* @param name The identifier
* @return The cached object.
*/
public T getCachedObject(String name) {
if (!this.running)
return null;
if(diskFiles.contains(name)) {
diskFiles.remove(name);
try {
File file = new File("./cache/" + name);
ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(file));
try {
T object = (T) inputStream.readObject();
object.setLastAccessed(0);
cacheObject(name, object);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
file.delete();
} catch (IOException e) {
e.printStackTrace();
}
}
return cachedObjects.get(name);
}
/**
* Go through all the cached objects and see if it is
* their turn to be discarded.
*/
@Override
public void run() {
File file = new File("./cache/");
for(File subfile : file.listFiles()) {
if(!subfile.isDirectory()) {
subfile.delete();
}
}
while (running) {
for (String s : cachedObjects.keySet()) {
Cacheable cacheable = cachedObjects.get(s);
if(cacheable.getLastAccessed() >= STORE_TIME) {
cachedObjects.remove(s);
try {
ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("./cache/" + s));
outputStream.writeObject(cacheable);
} catch (IOException e) {
e.printStackTrace();
}
diskFiles.add(s);
}
}
try {
sleep(CYCLE_TIME);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/*
* Copyright (C) 2010 OpenRune Development Team
*
* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* Only the registered members can see the link. for more details.
*/
package org.openrune.test;
/**
* A cacheable object.
*
* @author Anthony Snavely
*/
public class Cacheable {
private long lastAccessed = 0;
public void setLastAccessed(long lastAccessed) {
this.lastAccessed = lastAccessed;
}
public long getLastAccessed() {
return lastAccessed;
}
}
Idea originally given to me by sir pk p00n/sean.
* Copyright (C) 2010 OpenRune Development Team
*
* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* Only the registered members can see the link. for more details.
*/
package org.openrune.test;
import java.io.*;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
/**
* A caching pool that caches objects to the disk
* when they have been untouched in twenty minutes.
* Purpose is to save memory.
*
* @author Anthony Snavely
*/
public class CachingPool<T extends Cacheable> extends Thread {
/* The amount of time in milliseconds between checking expiry. */
private static final int CYCLE_TIME = 5000;
/* The amount of time in milliseconds to store cached objects */
private static final int STORE_TIME = 1200000; // 1200000 ms. e.g. twenty minutes
/* Whether or not the cache is current running. */
private boolean running = true;
/* The current cached objects. */
private Map<String, T> cachedObjects = new ConcurrentHashMap<String, T>();
/* The list of current files on the disk */
private List<String> diskFiles = new CopyOnWriteArrayList<String>();
public CachingPool() {
super("CachingThread");
start();
}
/**
* Restart the cache.
*/
public void startRunning() {
this.running = true;
this.cachedObjects = new ConcurrentHashMap<String, T>();
this.diskFiles = new CopyOnWriteArrayList<String>();
start();
}
/**
* Stop the cache.
*/
public void stopRunning() {
this.running = false;
this.cachedObjects = null; // So the gc can collect the list and cached objects.
this.diskFiles = null;
}
/**
* Cache an object.
*
* @param id The identifier
* @param cacheable The cacheable object.
*/
public void cacheObject(String id, T cacheable) {
if (this.running)
cachedObjects.put(id, cacheable);
}
/**
* Get a cached object either from the dish or from memory.
*
* @param name The identifier
* @return The cached object.
*/
public T getCachedObject(String name) {
if (!this.running)
return null;
if(diskFiles.contains(name)) {
diskFiles.remove(name);
try {
File file = new File("./cache/" + name);
ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(file));
try {
T object = (T) inputStream.readObject();
object.setLastAccessed(0);
cacheObject(name, object);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
file.delete();
} catch (IOException e) {
e.printStackTrace();
}
}
return cachedObjects.get(name);
}
/**
* Go through all the cached objects and see if it is
* their turn to be discarded.
*/
@Override
public void run() {
File file = new File("./cache/");
for(File subfile : file.listFiles()) {
if(!subfile.isDirectory()) {
subfile.delete();
}
}
while (running) {
for (String s : cachedObjects.keySet()) {
Cacheable cacheable = cachedObjects.get(s);
if(cacheable.getLastAccessed() >= STORE_TIME) {
cachedObjects.remove(s);
try {
ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("./cache/" + s));
outputStream.writeObject(cacheable);
} catch (IOException e) {
e.printStackTrace();
}
diskFiles.add(s);
}
}
try {
sleep(CYCLE_TIME);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/*
* Copyright (C) 2010 OpenRune Development Team
*
* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* Only the registered members can see the link. for more details.
*/
package org.openrune.test;
/**
* A cacheable object.
*
* @author Anthony Snavely
*/
public class Cacheable {
private long lastAccessed = 0;
public void setLastAccessed(long lastAccessed) {
this.lastAccessed = lastAccessed;
}
public long getLastAccessed() {
return lastAccessed;
}
}
Idea originally given to me by sir pk p00n/sean.