Example #1
0
        public static void Main(string[] args)
        {
                        #pragma warning disable 219
            int     size  = 20;
            MyCache cache = new MyCache(size);
            mco = new MyCacheableObject("0");
            cache.addEntry("0", mco);

            for (int i = 1; i < 5; i++)
            {
                MyCacheableObject temp = new MyCacheableObject(i.ToString());
                cache.addEntry(i.ToString(), temp);
                temp = null;                //csc and dotnet require this in order to work correctly, dmcs does not
            }

            sleepAndPrintCount(cache);


            GC.Collect();                                 //All but one entry will be finalized
            MyCacheableObject mco2 = cache.getEntry("3"); //Example of getting object before finalize
            sleepAndPrintCount(cache);
            Console.WriteLine("One entry reachable from root set, another entry gotten after gc before finalize so it is not tracked.");

            mco2 = null;
            GC.Collect();
            sleepAndPrintCount(cache);
            Console.WriteLine("One entry reachable from root set, second chance added to lru tracking");


            mco = null;
            GC.Collect();
            sleepAndPrintCount(cache);
            Console.WriteLine("All entries should now be lru tracked");
                        #pragma warning restore 219
        }
Example #2
0
        public override bool Equals(Object obj)
        {
            MyCacheableObject mco = obj as MyCacheableObject;

            if (mco == null)
            {
                return(false);
            }

            return(_value.Equals(mco.ToString()));
        }
Example #3
0
 public MyCacheableObject getEntry(string key)
 {
     if (_cache.ContainsKey(key))
     {
         MyCacheableObject mco = _cache[key].Target as MyCacheableObject;
         GC.SuppressFinalize(mco);
         GC.ReRegisterForFinalize(mco);
         _lru.Remove(mco);
         mco.setGet(true);
         return(mco);
     }
     return(null);
 }
Example #4
0
 public void addEntry(string key, MyCacheableObject value)
 {
     try
     {
         value.setCache(this);
         // ressurection tracking enabled, allow calls to get during EWR
         //GC.SuppressFinalize(this);
         //GC.ReRegisterForFinalize(this);
         _cache.Add(key, new WeakReference(value, true));                //ressurection tracking enabled
     }
     catch (ArgumentException)
     {
         _lru.Remove(value);                //does nothing if does not exist, O(n)
     }
 }
Example #5
0
 public void LRU(MyCacheableObject mco)
 {
     GC.SuppressFinalize(mco);
     _lru.Insert(0, mco);
     evict();
 }