| Method from org.apache.naming.resources.ResourceCache Detail: |
public boolean allocate(int space) {
int toFree = space - (cacheMaxSize - cacheSize);
if (toFree < = 0) {
return true;
}
// Increase the amount to free so that allocate won't have to run right
// away again
toFree += (cacheMaxSize / 20);
int size = notFoundCache.size();
if (size > spareNotFoundEntries) {
notFoundCache.clear();
cacheSize -= size;
toFree -= size;
}
if (toFree < = 0) {
return true;
}
int attempts = 0;
int entriesFound = 0;
long totalSpace = 0;
int[] toRemove = new int[maxAllocateIterations];
while (toFree > 0) {
if (attempts == maxAllocateIterations) {
// Give up, no changes are made to the current cache
return false;
}
if (toFree > 0) {
// Randomly select an entry in the array
int entryPos = -1;
boolean unique = false;
while (!unique) {
unique = true;
entryPos = random.nextInt(cache.length) ;
// Guarantee uniqueness
for (int i = 0; i < entriesFound; i++) {
if (toRemove[i] == entryPos) {
unique = false;
}
}
}
long entryAccessRatio =
((cache[entryPos].accessCount * 100) / accessCount);
if (entryAccessRatio < desiredEntryAccessRatio) {
toRemove[entriesFound] = entryPos;
totalSpace += cache[entryPos].size;
toFree -= cache[entryPos].size;
entriesFound++;
}
}
attempts++;
}
// Now remove the selected entries
java.util.Arrays.sort(toRemove, 0, entriesFound);
CacheEntry[] newCache = new CacheEntry[cache.length - entriesFound];
int pos = 0;
int n = -1;
if (entriesFound > 0) {
n = toRemove[0];
for (int i = 0; i < cache.length; i++) {
if (i == n) {
if ((pos + 1) < entriesFound) {
n = toRemove[pos + 1];
pos++;
} else {
pos++;
n = -1;
}
} else {
newCache[i - pos] = cache[i];
}
}
}
cache = newCache;
cacheSize -= totalSpace;
return true;
}
|
public long getAccessCount() {
// ------------------------------------------------------------- Properties
return accessCount;
}
Return the access count.
Note: Update is not synced, so the number may not be completely
accurate. |
public int getCacheMaxSize() {
return cacheMaxSize;
}
Return the maximum size of the cache in KB. |
public int getCacheSize() {
return cacheSize;
}
Return the current cache size in KB. |
public long getDesiredEntryAccessRatio() {
return desiredEntryAccessRatio;
}
Return desired entry access ratio. |
public long getHitsCount() {
return hitsCount;
}
Return the number of cache hits.
Note: Update is not synced, so the number may not be completely
accurate. |
public int getMaxAllocateIterations() {
return maxAllocateIterations;
}
Return the maximum amount of iterations during a space allocation. |
public int getSpareNotFoundEntries() {
return spareNotFoundEntries;
}
Return the amount of spare not found entries. |
public void load(CacheEntry entry) {
if (entry.exists) {
if (insertCache(entry)) {
cacheSize += entry.size;
}
} else {
int sizeIncrement = (notFoundCache.get(entry.name) == null) ? 1 : 0;
notFoundCache.put(entry.name, entry);
cacheSize += sizeIncrement;
}
}
|
public CacheEntry lookup(String name) {
CacheEntry cacheEntry = null;
CacheEntry[] currentCache = cache;
accessCount++;
int pos = find(currentCache, name);
if ((pos != -1) && (name.equals(currentCache[pos].name))) {
cacheEntry = currentCache[pos];
}
if (cacheEntry == null) {
try {
cacheEntry = (CacheEntry) notFoundCache.get(name);
} catch (Exception e) {
// Ignore: the reliability of this lookup is not critical
}
}
if (cacheEntry != null) {
hitsCount++;
}
return cacheEntry;
}
|
public void setCacheMaxSize(int cacheMaxSize) {
this.cacheMaxSize = cacheMaxSize;
}
Set the maximum size of the cache in KB. |
public void setDesiredEntryAccessRatio(long desiredEntryAccessRatio) {
this.desiredEntryAccessRatio = desiredEntryAccessRatio;
}
Set the desired entry access ratio. |
public void setMaxAllocateIterations(int maxAllocateIterations) {
this.maxAllocateIterations = maxAllocateIterations;
}
Set the maximum amount of iterations during a space allocation. |
public void setSpareNotFoundEntries(int spareNotFoundEntries) {
this.spareNotFoundEntries = spareNotFoundEntries;
}
Set the amount of spare not found entries. |
public boolean unload(String name) {
CacheEntry removedEntry = removeCache(name);
if (removedEntry != null) {
cacheSize -= removedEntry.size;
return true;
} else if (notFoundCache.remove(name) != null) {
cacheSize--;
return true;
}
return false;
}
|