Ejemplo n.º 1
0
 /// <summary>
 /// Release this cache, optionally destroying it.
 /// </summary>
 /// <param name="fDestroy">
 /// If true, destroy the cache as well.
 /// </param>
 protected virtual void Release(Boolean fDestroy)
 {
     try
     {
         INamedCache cache = BackCache;
         UnregisterBackServiceMemberEventHandler();
         base.Release();
         if (fDestroy)
         {
             cache.Destroy();
         }
         else
         {
             cache.Release();
         }
     }
     catch (Exception)
     {
         // one of the following should be ignored:
         // IllegalOperationException("Cache is not active");
         // Exception("Storage is not configured");
         // Exception("Service has been terminated");
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Demonstrate role based access to the cache.
        /// </summary>
        public static void AccessCache()
        {
            Console.WriteLine("------cache access control example begins------");
            Console.WriteLine();

            // Someone with writer role can write and read
            IPrincipal principal        = SecurityExampleHelper.Login("JohnWhorfin");
            IPrincipal principalCurrent = Thread.CurrentPrincipal;

            try
            {
                Thread.CurrentPrincipal = principal;
                INamedCache cache = CacheFactory.GetCache(
                    SecurityExampleHelper.SECURITY_CACHE_NAME);

                cache["myKey"] = "myValue";
                string sValue = (string)cache["myKey"];
                Console.WriteLine();
                Console.WriteLine("    Success: read and write allowed");
                Console.WriteLine();
            }
            catch (Exception e)
            {
                // get exception if not allowed to perform the operation
                Console.WriteLine(e.StackTrace);
            }
            finally
            {
                Thread.CurrentPrincipal = principalCurrent;
            }

            // Someone with reader role can read but not write
            principal = SecurityExampleHelper.Login("JohnBigboote");

            try
            {
                Thread.CurrentPrincipal = principal;
                INamedCache cache = CacheFactory.GetCache(
                    SecurityExampleHelper.SECURITY_CACHE_NAME);
                string sValue = (string)cache["myKey"];
                Console.WriteLine();
                Console.WriteLine("    Success: read allowed");
                Console.WriteLine();
                cache["anotherKey"] = "anotherValue";
            }
            catch (Exception)
            {
                // get exception if not allowed to perform the operation
                Console.WriteLine();
                Console.WriteLine("    Success: Correctly cannot write");
                Console.WriteLine();
            }
            finally
            {
                Thread.CurrentPrincipal = principalCurrent;
            }

            // Someone with writer role cannot call destroy
            principal = SecurityExampleHelper.Login("JohnWhorfin");

            try
            {
                Thread.CurrentPrincipal = principal;
                INamedCache cache = CacheFactory.GetCache(
                    SecurityExampleHelper.SECURITY_CACHE_NAME);
                cache.Destroy();
            }
            catch (Exception)
            {
                // get exception if not allowed to perform the operation
                Console.WriteLine();
                Console.WriteLine("    Success: Correctly cannot destroy the cache");
                Console.WriteLine();
            }
            finally
            {
                Thread.CurrentPrincipal = principalCurrent;
            }

            // Someone with admin role can call destroy
            principal = SecurityExampleHelper.Login("BuckarooBanzai");

            try
            {
                Thread.CurrentPrincipal = principal;
                INamedCache cache = CacheFactory.GetCache(
                    SecurityExampleHelper.SECURITY_CACHE_NAME);
                cache.Destroy();
                Console.WriteLine();
                Console.WriteLine("    Success: Correctly allowed to destroy the cache");
                Console.WriteLine();
            }
            catch (Exception e)
            {
                // get exception if not allowed to perform the operation
                Console.WriteLine(e.StackTrace);
            }
            finally
            {
                Thread.CurrentPrincipal = principalCurrent;
            }
            Console.WriteLine();
            Console.WriteLine("------cache access control example completed------");
            Console.WriteLine();
        }
Ejemplo n.º 3
0
        public void Coh8796()
        {
            LocalNamedCache localcache = new LocalNamedCache();
            INamedCache     safecache  = CacheFactory.GetCache("dist-extend-direct");
            NearCache       nearcache  = new NearCache(localcache, safecache, CompositeCacheStrategyType.ListenLogical);

            safecache.Clear();

            ICache cacheFront = nearcache.FrontCache;
            ICache cacheBack  = nearcache.BackCache;
            int    cPuts      = 1000;

            for (int i = 0; i < cPuts; i++)
            {
                cacheBack.Insert(i, i, 10000);
                if (i % 2 == 0)
                {
                    Object o = nearcache[i];
                    Assert.AreEqual(i, o);
                }
            }

            Assert.AreEqual(cPuts / 2, cacheFront.Count);
            Assert.AreEqual(cPuts, cacheBack.Count);

            // expire the back map
            Thread.Sleep(15000);

            // calling Count expires the entries in the back and sends out synthetic deletes
            Assert.AreEqual(0, cacheBack.Count);

            // ensure that synthetic deletes are filtered out;
            // front map values for evens are still there
            for (int i = 0; i < cPuts; i++)
            {
                if (i % 2 == 0)
                {
                    Assert.AreEqual(i, cacheFront[i]);
                }
                else
                {
                    Assert.IsNull(cacheFront[i]);
                }
            }

            Assert.AreEqual(cPuts / 2, cacheFront.Count); // 0, 2, 4, ...
            Assert.AreEqual(0, cacheBack.Count);

            // ensure that Insert works, and that a value update is properly
            // raised to both the front and back maps
            for (int i = 0; i < cPuts; i++)
            {
                int nKey = i * 4;

                nearcache.Insert(nKey, nKey);

                Assert.AreEqual(nKey, cacheFront[nKey]);
                Assert.AreEqual(nKey, cacheBack[nKey]);

                cacheBack.Insert(nKey, nKey + 1);

                Assert.IsNull(cacheFront[nKey]);
                Assert.AreEqual(nKey + 1, cacheBack[nKey]);

                nearcache.Insert(nKey, nKey);

                Assert.AreEqual(nKey, cacheFront[nKey]);
                Assert.AreEqual(nKey, cacheBack[nKey]);

                cacheBack.Remove(nKey);

                Assert.IsFalse(cacheBack.Contains(nKey));
                Assert.IsFalse(cacheFront.Contains(nKey));
                Assert.IsNull(cacheBack[nKey]);
                Assert.IsNull(cacheFront[nKey]);
            }
            nearcache.Release();
            // fresh reference to the cache
            safecache = CacheFactory.GetCache(CacheName);
            safecache.Destroy();
            CacheFactory.Shutdown();
        }