Exemplo n.º 1
0
        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;
                    }
                }
            }
        }
Exemplo n.º 2
0
        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);
            }
        }
Exemplo n.º 3
0
 public virtual void put(string id, object resource)
 {
     cache[id] = new HalResourceCacheEntry(id, resource);
     ensureCapacityLimit();
 }
Exemplo n.º 4
0
 protected internal virtual bool expired(HalResourceCacheEntry entry)
 {
     return(entry.CreateTime + secondsToLive * 1000 < ClockUtil.CurrentTime.Ticks);
 }