protected internal virtual void ensureCapacityLimit() { if (size() > Capacity) { IList <HalResourceCacheEntry> resources = new List <HalResourceCacheEntry>(cache.Values); NavigableSet <HalResourceCacheEntry> remainingResources = new SortedSet <HalResourceCacheEntry>(COMPARATOR); // remove expired resources foreach (HalResourceCacheEntry resource in resources) { if (expired(resource)) { remove(resource.Id); } else { remainingResources.add(resource); } if (size() <= Capacity) { // abort if capacity is reached return; } } // if still exceed capacity remove oldest while (remainingResources.size() > capacity) { HalResourceCacheEntry resourceToRemove = remainingResources.pollFirst(); if (resourceToRemove != null) { remove(resourceToRemove.Id); } else { break; } } } }
public virtual object get(string id) { HalResourceCacheEntry cacheEntry = cache[id]; if (cacheEntry != null) { if (expired(cacheEntry)) { remove(cacheEntry.Id); return(null); } else { return(cacheEntry.Resource); } } else { return(null); } }
public virtual void put(string id, object resource) { cache[id] = new HalResourceCacheEntry(id, resource); ensureCapacityLimit(); }
protected internal virtual bool expired(HalResourceCacheEntry entry) { return(entry.CreateTime + secondsToLive * 1000 < ClockUtil.CurrentTime.Ticks); }