/// <summary>
 /// Observe changes to the contacts.
 /// </summary>
 /// <param name="cache">target cache</param>
 public virtual void Observe(INamedCache cache)
 {
     Console.WriteLine("------ObserverExample begins------");
     m_listener = new ContactChangeListener();
     cache.AddCacheListener(m_listener);
     Console.WriteLine("------ContactChangleListener added------");
 }
        public virtual void TestNamedCacheEntryCollection()
        {
            INamedCache cache = CacheFactory.GetCache(CacheName);

            object[] keys   = { GetKeyObject("key1"), GetKeyObject("key2"), GetKeyObject("key3"), GetKeyObject("key4") };
            string[] values = { "value1", "value2", "value3", "value4" };
            cache.Clear();
            IDictionary h = new Hashtable();

            h.Add(keys[0], values[0]);
            h.Add(keys[1], values[1]);
            h.Add(keys[2], values[2]);
            h.Add(keys[3], values[3]);
            cache.InsertAll(h);

            SafeNamedCache        safeCache = (SafeNamedCache)cache;
            IDictionaryEnumerator de        = safeCache.NamedCache.GetEnumerator();
            ArrayList             al        = new ArrayList(h.Values);

            for (; de.MoveNext();)
            {
                Assert.IsTrue(al.Contains(de.Value));
                Assert.IsTrue(h.Contains(de.Key));
            }

            CacheFactory.ReleaseCache(cache);
            CacheFactory.Shutdown();
        }
        public void TestReplace()
        {
            INamedCache cache = CacheFactory.GetCache(CacheName);

            cache.Clear();

            Hashtable ht = new Hashtable();

            ht.Add("key4", 0);
            ht.Add("key3", -10);
            ht.Add("key2", 45);
            ht.Add("key1", 398);
            cache.InsertAll(ht);

            object result = cache.Replace("key1", 400);

            Assert.AreEqual(398, result);
            Assert.AreEqual(400, cache["key1"]);

            result = cache.Replace("key1", 300, 450);
            Assert.AreEqual(false, result);

            result = cache.Replace("key1", 400, 450);
            Assert.AreEqual(true, result);
            Assert.AreEqual(450, cache["key1"]);
        }
Exemple #4
0
 /// <summary>
 /// Construct a <b>NearCache</b>, using a <i>back</i>
 /// <see cref="INamedCache"/> as the complete (back) storage and
 /// <i>front</i> <see cref="ICache"/> as a near (front) storage using
 /// the <see cref="CompositeCacheStrategyType"/> invalidation
 /// strategy.
 /// </summary>
 /// <param name="front">
 /// <b>ICache</b> to put in front of the back cache.
 /// </param>
 /// <param name="back">
 /// <b>INamedCache</b> to put behind the front cache.
 /// </param>
 /// <param name="strategy">
 /// Specifies the strategy used for the front cache
 /// invalidation; valid values are:
 /// <see cref="CompositeCacheStrategyType.ListenNone"/>
 /// <see cref="CompositeCacheStrategyType.ListenPresent"/>
 /// <see cref="CompositeCacheStrategyType.ListenAll"/>
 /// <see cref="CompositeCacheStrategyType.ListenAuto"/>
 /// </param>
 /// <since>Coherence 2.3</since>
 public NearCache(ICache front, INamedCache back, CompositeCacheStrategyType strategy)
     : base(front, back, strategy)
 {
     m_name    = back.CacheName;
     m_service = back.CacheService;
     RegisterBackServiceMemberEventHandler();
 }
        public void TestConfiguredTriggerListener()
        {
            IConfigurableCacheFactory ccf = CacheFactory.ConfigurableCacheFactory;

            IXmlDocument config = XmlHelper.LoadXml("assembly://Coherence.Tests/Tangosol.Resources/s4hc-extend-maptrigger-cache-config.xml");

            ccf.Config = config;

            INamedCache cache = CacheFactory.GetCache(CACHE_NAME);

            cache.Clear();

            SimplePerson pIn = new SimplePerson("123-45-6789", "Eddie", "Van Halen", 1955,
                                                "987-65-4321", new String[] { "456-78-9123" });

            try
            {
                cache.Insert(1, pIn);
                Object[] keys = cache.GetKeys(AlwaysFilter.Instance);
                Assert.AreEqual(keys.Length, 1);

                SimplePerson pOut = (SimplePerson)cache[1];

                Assert.AreEqual(pIn.LastName.ToUpper(), pOut.LastName);
            }
            finally
            {
                CacheFactory.Shutdown();
            }
        }
        public void TestGetValues()
        {
            IConfigurableCacheFactory ccf = CacheFactory.ConfigurableCacheFactory;

            IXmlDocument config = XmlHelper.LoadXml("assembly://Coherence.Tests/Tangosol.Resources/s4hc-local-cache-config.xml");

            ccf.Config = config;

            INamedCache cache = CacheFactory.GetCache("local-default");

            cache.Clear();

            Hashtable ht = new Hashtable();

            ht.Add("key1", 100.0);
            ht.Add("key2", 80.5);
            ht.Add("key3", 19.5);
            ht.Add("key4", 2.0);

            cache.InsertAll(ht);
            Assert.AreEqual(4, cache.Count);

            IFilter lessFilter = new LessFilter(IdentityExtractor.Instance, 80.5);

            object[] result = cache.GetValues(lessFilter);
            Assert.AreEqual(2, result.Length);
            foreach (object o in result)
            {
                Assert.IsTrue((Convert.ToDouble(o) == 19.5) || (Convert.ToDouble(o) == 2.0));
            }

            CacheFactory.Shutdown();
        }
        public void TestEntryTouch()
        {
            IConfigurableCacheFactory ccf = CacheFactory.ConfigurableCacheFactory;

            IXmlDocument config = XmlHelper.LoadXml("assembly://Coherence.Tests/Tangosol.Resources/s4hc-local-cache-config.xml");

            ccf.Config = config;

            INamedCache cache = CacheFactory.GetCache("local-default");

            cache.Clear();
            Hashtable ht = new Hashtable();

            ht.Add("key1", 100.0);
            ht.Add("key2", 80.5);
            ht.Add("key3", 19.5);
            ht.Add("key4", 2.0);

            cache.InsertAll(ht);

            object result = cache["key2"];

            result = cache["key2"];
            result = cache["key2"];
            result = cache["key1"];

            ICacheEntry[] results = cache.GetEntries(new EqualsFilter(new KeyExtractor(IdentityExtractor.Instance), "key2"));
// TODO : we don't get back the actual entry!!!
//            Assert.AreEqual(3, (results[0] as LocalCache.Entry).TouchCount);

            results = cache.GetEntries(new EqualsFilter(new KeyExtractor(IdentityExtractor.Instance), "key1"));
//            Assert.AreEqual(1, (results[0] as LocalCache.Entry).TouchCount);

            CacheFactory.Shutdown();
        }
Exemple #8
0
        public void TestPreloadRequest()
        {
            IEntryProcessor processor  = PreloadRequest.Instance;
            IEntryProcessor processor1 = PreloadRequest.Instance;

            Assert.AreEqual(processor, processor1);
            Assert.AreEqual(processor.GetHashCode(), processor1.GetHashCode());
            Assert.AreEqual(processor.ToString(), processor1.ToString());

            INamedCache cache = CacheFactory.GetCache(CacheName);

            cache.Clear();

            Temperature bgd = new Temperature(25, 'c', 12);
            Temperature nyc = new Temperature(99, 'f', 12);

            cache.Insert("BGD", bgd);
            cache.Insert("NYC", nyc);

            object o = cache.Invoke("BGD", processor);

            Assert.IsNull(o);

            CacheFactory.Shutdown();
        }
        public void TestPofExtractorWithValueChangeEventFilter2()
        {
            // Testing on remote cache using Address, which is defined on
            // Java side.
            INamedCache cache = CacheFactory.GetCache(CacheName);

            cache.Clear();

            Hashtable ht       = new Hashtable();
            Address   address1 = new Address("Street1", "City1", "State1", "Zip1");
            Address   address2 = new Address("Street2", "City2", "State2", "Zip2");

            ht.Add("key1", address1);
            ht.Add("key2", address2);
            cache.InsertAll(ht);

            SyncListener listener = new SyncListener();
            IFilter      filter   = new ValueChangeEventFilter(new PofExtractor(typeof(String), 0));

            cache.AddCacheListener(listener,
                                   filter,
                                   false);
            Assert.IsNull(listener.CacheEvent);

            cache["key1"] = new Address("Street1", "City1a", "State1a", "Zip1a");
            Assert.IsNull(listener.CacheEvent);

            cache["key1"] = new Address("Street1a", "City1", "State1", "Zip1");
            Assert.IsNotNull(listener.CacheEvent);

            CacheFactory.Shutdown();
        }
        /// <summary>
        /// Obtain an <see cref="INamedCache"/> interface that provides a view
        /// of resources shared among members of a cluster.
        /// </summary>
        /// <remarks>
        /// The view is identified by name within this ICacheService.
        /// Typically, repeated calls to this method with the same view name
        /// will result in the same view reference being returned.
        /// </remarks>
        /// <param name="name">
        /// The name, within this ICacheService, that uniquely identifies a
        /// view; <c>null</c> is legal, and may imply a default name.
        /// </param>
        /// <returns>
        /// An <b>INamedCache</b> interface which can be used to access the
        /// resources of the specified view.
        /// </returns>
        /// <exception cref="InvalidOperationException">
        /// If the service is not running.
        /// </exception>
        public INamedCache EnsureCache(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                name = "Default";
            }

            ScopedReferenceStore storeCache = StoreSafeNamedCache;
            SafeNamedCache       cacheSafe  = (SafeNamedCache)storeCache.GetCache(name);

            if (cacheSafe == null)
            {
                lock (storeCache)
                {
                    INamedCache cache = RunningCacheService.EnsureCache(name);

                    cacheSafe = new SafeNamedCache
                    {
                        SafeCacheService = this,
                        CacheName        = name,
                        NamedCache       = cache,
                        Principal        = Thread.CurrentPrincipal
                    };

                    storeCache.PutCache(cacheSafe);
                }
            }

            return(cacheSafe);
        }
        /// <summary>
        /// Release and destroy the specified cache.
        /// </summary>
        /// <remarks>
        /// <b>Warning:</b> This method is used to completely destroy the
        /// specified cache across the cluster. All references in the entire
        /// cluster to this cache will be invalidated, the cached data will
        /// be cleared, and all resources will be released.
        /// </remarks>
        /// <param name="cache">
        /// The cache object to be released.
        /// </param>
        public virtual void DestroyCache(INamedCache cache)
        {
            SafeNamedCache cacheSafe = (SafeNamedCache)cache;

            RemoveCacheReference(cacheSafe);

            ICacheService service = CacheService;

            try
            {
                INamedCache cacheWrapped = cacheSafe.NamedCache;
                if (cacheWrapped == null)
                {
                    throw new InvalidOperationException("Cache is already released");
                }
                else
                {
                    service.DestroyCache(cacheWrapped);
                }
            }
            catch (Exception)
            {
                if (service != null && service.IsRunning)
                {
                    throw;
                }
            }
        }
Exemple #12
0
        public void TestNamedDefaultPofSerializer()
        {
            IXmlElement xmlConfig = XmlHelper.LoadXml("assembly://Coherence.Tests/Tangosol.Resources/s4hc-extend-named-pof-default-serializer-cache-config.xml");

            CacheFactory.Configure(xmlConfig, null);
            INamedCache cache = CacheFactory.GetCache("dist-default");

            cache.Clear();

            // create a key, and value
            String sKey   = "hello";
            String sValue = "grid";

            // insert the pair into the cache
            cache.Add(sKey, sValue);

            // get the key and value back
            IDictionary entries = cache.GetAll(cache.Keys);

            Assert.AreEqual(1, entries.Count);
            Assert.IsInstanceOf(typeof(IDictionary), entries);
            Assert.AreEqual(sValue, ((String)((IDictionary)entries)[sKey]));

            CacheFactory.ReleaseCache(cache);
        }
        public void PofCircularReference()
        {
            CacheFactory.DefaultPofConfigPath = "//Coherence.Tests/Tangosol.Resources/s4hc-test-reference-config.xml";
            CacheFactory.DefaultPofConfig     = XmlHelper.LoadResource(ResourceLoader.GetResource(
                                                                           "assembly://Coherence.Tests/Tangosol.Resources/s4hc-test-reference-config.xml"), "POF configuration");
            IConfigurableCacheFactory ccf = CacheFactory.ConfigurableCacheFactory;

            ccf.Config = XmlHelper.LoadXml(
                "assembly://Coherence.Tests/Tangosol.Resources/s4hc-cache-config-reference.xml");
            ICacheService service = (ICacheService)ccf.EnsureService("ExtendTcpCacheService");
            INamedCache   cache   = service.EnsureCache("dist-extend-reference");

            var joe  = new PortablePerson("Joe Smith", new DateTime(78, 4, 25));
            var jane = new PortablePerson("Jane Smith", new DateTime(80, 5, 22));

            joe.Spouse  = jane;
            jane.Spouse = joe;

            cache.Add("Key1", joe);
            cache.Invoke("Key1", new ConditionalPut(new AlwaysFilter(), joe, false));

            CacheFactory.DefaultPofConfigPath = "//Coherence.Tests/Tangosol.Resources/s4hc-test-config.xml";
            CacheFactory.DefaultPofConfig     = XmlHelper.LoadResource(ResourceLoader.GetResource(
                                                                           "assembly://Coherence.Tests/Tangosol.Resources/s4hc-test-config.xml"), "POF configuration");
            CacheFactory.Shutdown();
        }
        /*
         * common method for tests of this suite
         */
        private void runTest(IXmlDocument config, string cacheName, string serializerName)
        {
            IConfigurableCacheFactory ccf = CacheFactory.ConfigurableCacheFactory;
            IXmlElement originalConfig    = ccf.Config;

            ccf.Config = config;

            INamedCache cache = ccf.EnsureCache(cacheName);

            cache.Clear();

            // create a key, and value
            String sKey   = "hello";
            String sValue = "grid";

            // insert the pair into the cache
            cache.Insert(sKey, sValue);

            // read back the value, custom serializer should have converted
            Assert.AreEqual(cache.Count, 1);
            Assert.AreEqual(cache[sKey], serializerName);

            ccf.DestroyCache(cache);
            ccf.Config = originalConfig;
        }
        public void TestCustomUnitCalculator()
        {
            IConfigurableCacheFactory ccf = CacheFactory.ConfigurableCacheFactory;

            IXmlDocument config = XmlHelper.LoadXml("assembly://Coherence.Tests/Tangosol.Resources/s4hc-local-cache-config.xml");

            ccf.Config = config;

            INamedCache cache = CacheFactory.GetCache("local-custom-unit-calculator");

            Assert.IsTrue(cache is LocalNamedCache);

            LocalNamedCache localNamedCache = cache as LocalNamedCache;

            Assert.IsNotNull(localNamedCache);

            LocalCache localCache = localNamedCache.LocalCache;

            Assert.IsNotNull(localCache);
            Assert.IsNotNull(localCache.UnitCalculator);

            localCache.Insert("key1", "value1");
            localCache.Insert("key2", "value2");
            LocalCache.Entry entry = localCache.GetEntry("key1");

            //evaluates total units in the cache and each entry's units
            Assert.AreEqual(localCache.CalculatorType, LocalCache.UnitCalculatorType.External);
            Assert.IsInstanceOf(typeof(TestUnitCalculator), localCache.UnitCalculator);
            Assert.AreEqual(localCache.Units, 4);
            Assert.AreEqual(entry.Units, 2);

            CacheFactory.Shutdown();
        }
        public void TestExtractorEventTransformer()
        {
            //testing on remote cache
            INamedCache cache = CacheFactory.GetCache(CacheName);

            cache.Clear();

            Hashtable ht       = new Hashtable();
            Address   address1 = new Address("Street1", "City1", "State1", "Zip1");
            Address   address2 = new Address("Street2", "City2", "State2", "Zip2");

            ht.Add("key1", address1);
            ht.Add("key2", address2);
            cache.InsertAll(ht);

            SyncListener           listener    = new SyncListener();
            IFilter                filter      = new ValueChangeEventFilter("getStreet");
            IValueExtractor        extractor   = IdentityExtractor.Instance;
            ICacheEventTransformer transformer = new ExtractorEventTransformer(null, extractor);

            cache.AddCacheListener(listener,
                                   new CacheEventTransformerFilter(filter,
                                                                   transformer),
                                   false);
            Assert.IsNull(listener.CacheEvent);

            cache["key1"] = new Address("Street1", "City1a", "State1a", "Zip1a");
            Assert.IsNull(listener.CacheEvent);

            cache["key1"] = new Address("Street1a", "City1a", "State1a", "Zip1a");
            Assert.IsNotNull(listener.CacheEvent);

            CacheFactory.Shutdown();
        }
        public void TestClearAndCount()
        {
            IConfigurableCacheFactory ccf = CacheFactory.ConfigurableCacheFactory;

            IXmlDocument config = XmlHelper.LoadXml("assembly://Coherence.Tests/Tangosol.Resources/s4hc-local-cache-config.xml");

            ccf.Config = config;

            INamedCache cache = CacheFactory.GetCache("local-default");

            cache.Clear();
            Assert.AreEqual(0, cache.Count);

            cache.Insert("key1", "value1");
            Assert.AreEqual(1, cache.Count);

            cache.Insert("key2", "value2");
            Assert.AreEqual(2, cache.Count);

            cache.Insert("key3", "value3");
            cache.Insert("key4", "value4");
            Assert.AreEqual(4, cache.Count);

            cache.Clear();
            Assert.AreEqual(0, cache.Count);

            CacheFactory.Shutdown();
        }
        public void TestPofExtractorWithValueChangeEventFilter1()
        {
            // Testing on remote cache using CustomerKeyClass, which is not
            // defined on Java side
            INamedCache cache = CacheFactory.GetCache(CacheName);

            cache.Clear();

            // CustomerKeyClass is not defined on the Java side
            //
            Hashtable      ht   = new Hashtable();
            CustomKeyClass key1 = new CustomKeyClass("Customer1");
            CustomKeyClass key2 = new CustomKeyClass("Customer2");

            ht.Add("key1", key1);
            ht.Add("key2", key2);
            cache.InsertAll(ht);

            SyncListener listener = new SyncListener();
            IFilter      filter   = new ValueChangeEventFilter(new PofExtractor(typeof(String), 0));

            cache.AddCacheListener(listener,
                                   filter,
                                   false);
            Assert.IsNull(listener.CacheEvent);

            cache["key1"] = new CustomKeyClass("Customer1");
            Assert.IsNull(listener.CacheEvent);

            cache["key1"] = new CustomKeyClass("Customer12");
            Assert.IsNotNull(listener.CacheEvent);

            CacheFactory.Shutdown();
        }
        public void TestGetEnumerator()
        {
            IConfigurableCacheFactory ccf = CacheFactory.ConfigurableCacheFactory;

            IXmlDocument config = XmlHelper.LoadXml("assembly://Coherence.Tests/Tangosol.Resources/s4hc-local-cache-config.xml");

            ccf.Config = config;

            INamedCache cache = CacheFactory.GetCache("local-default");

            cache.Clear();

            Hashtable ht = new Hashtable();

            ht.Add("key1", 100.0);
            ht.Add("key2", 80.5);
            ht.Add("key3", 19.5);
            ht.Add("key4", 2.0);

            cache.InsertAll(ht);

            foreach (ICacheEntry entry in cache)
            {
                Assert.IsTrue(CollectionUtils.Contains(new ArrayList(ht.Values), entry.Value));
            }

            CacheFactory.Shutdown();
        }
Exemple #20
0
        /// <summary>
        /// Dispatch event.
        /// </summary>
        public void Run()
        {
            CacheEventArgs evt   = CacheEvent;
            INamedCache    cache = (INamedCache)evt.Cache;

            if (cache.IsActive)
            {
                CacheListenerSupport support = CacheListenerSupport;
                if (support == null)
                {
                    Listeners listeners = Listeners;
                    if (listeners == null)
                    {
                        CacheListenerSupport.Dispatch(evt, CacheListener);
                    }
                    else
                    {
                        CacheListenerSupport.Dispatch(evt, listeners, true);
                    }
                }
                else
                {
                    support.FireEvent(evt, true);
                }
            }
        }
        public void TestInsertWithMillis()
        {
            IConfigurableCacheFactory ccf = CacheFactory.ConfigurableCacheFactory;

            IXmlDocument config = XmlHelper.LoadXml("assembly://Coherence.Tests/Tangosol.Resources/s4hc-local-cache-config.xml");

            ccf.Config = config;

            INamedCache cache = CacheFactory.GetCache("local-default");

            Assert.IsTrue(cache is LocalNamedCache);

            LocalNamedCache localNamedCache = cache as LocalNamedCache;

            Assert.IsNotNull(localNamedCache);
            localNamedCache.Insert("key1", new LocalCache.Entry(localNamedCache.LocalCache, "key1", "value1"), 300);

            Assert.IsNotNull(localNamedCache["key1"]);

            lock (this)
            {
                Monitor.Wait(this, 400);
            }

            Assert.IsNull(localNamedCache["key1"]);

            CacheFactory.Shutdown();
        }
Exemple #22
0
        public override void TestNamedCacheProperties()
        {
            INamedCache cache = CacheFactory.GetCache(CacheName);
            string      key   = "testNamedCachePropertiesKey";
            string      value = "testNamedCachePropertiesValue";

            // INamedCache
            Assert.IsTrue(cache.IsActive);

            cache.Clear();
            Assert.AreEqual(cache.Count, 0);

            cache.Insert(GetKeyObject(key), value);
            Assert.AreEqual(cache.Count, 1);
            Assert.AreEqual(cache[GetKeyObject(key)], value);

            // BundlingNamedCache
            BundlingNamedCache bundleCache = (BundlingNamedCache)cache;

            Assert.IsFalse(bundleCache.IsFixedSize);
            Assert.IsFalse(bundleCache.IsReadOnly);
            Assert.IsTrue(bundleCache.IsSynchronized);

            // RemoteNamedCache
            SafeNamedCache safeCache = (SafeNamedCache)bundleCache.NamedCache;

            Assert.IsTrue(safeCache.IsActive);
            CacheFactory.ReleaseCache(cache);
            CacheFactory.Shutdown();
        }
Exemple #23
0
        /// <summary>
        /// Remove the cache reference from the store.
        /// </summary>
        /// <param name="cache">
        /// The cache reference.
        /// </param>
        /// <returns>
        /// Whether the item was found.
        /// </returns>
        public virtual bool ReleaseCache(INamedCache cache)
        {
            IDictionary mapByName  = m_mapByName;
            string      sCacheName = cache.CacheName;
            bool        fFound     = false;

            object oHolder = mapByName[sCacheName];

            if (oHolder == cache)
            {
                // remove the mapping
                mapByName.Remove(sCacheName);
                fFound = true;
            }
            else if (oHolder is PrincipalScopedReference)
            {
                PrincipalScopedReference scopedRef = (PrincipalScopedReference)
                                                     oHolder;

                if (scopedRef.Get() == cache)
                {
                    scopedRef.Remove();
                    fFound = true;
                }
            }
            return(fFound);
        }
Exemple #24
0
        /// <summary>
        /// Load contacts from the Stream and insert them into the cache.
        /// </summary>
        /// <param name="cache">
        /// The target cache.
        /// </param>
        /// <param name="reader">
        /// The stream containing contacts.
        /// </param>
        /// <throws>
        /// IOException on read error.
        /// </throws>
        public virtual void Load(INamedCache cache, StreamReader reader)
        {
            IDictionary dictBatch = new Hashtable(BATCH_SIZE);
            int         cContact  = 0;
            Contact     contact;

            Console.WriteLine("------LoaderExample begins------");
            while ((contact = ReadContact(reader)) != null)
            {
                // Add contact to batch
                dictBatch.Add(new ContactId(contact.FirstName,
                                            contact.LastName), contact);
                ++cContact;

                // When reached the BATCH_SIZE threashold transfer the records
                // to the cache.
                if (cContact % BATCH_SIZE == 0)
                {
                    // minimize the network roundtrips by using InsertAll()
                    cache.InsertAll(dictBatch);
                    dictBatch.Clear();
                    Console.Write('.');
                }
            }

            if (dictBatch.Count > 0)
            {
                // load final batch
                cache.InsertAll(dictBatch);
            }

            Console.WriteLine("Added " + cContact + " entries to cache");
            Console.WriteLine("------LoaderExample completed------");
        }
        public virtual void TestNamedCacheLock()
        {
            INamedCache cache = CacheFactory.GetCache(CacheName);
            string      key   = "testNamedCacheInterfaceMethodsKey";
            string      value = "testNamedCacheInterfaceMethodsValue";

            object[] keys   = { GetKeyObject("key1"), GetKeyObject("key2"), GetKeyObject("key3") };
            string[] values = { "value1", "value2", "value3" };

            cache.Clear();
            Assert.IsTrue(cache.Count == 0);

            cache.Add(GetKeyObject(key), value);
            Assert.AreEqual(cache.Count, 1);
            Assert.AreEqual(cache[GetKeyObject(key)], value);
            Assert.IsTrue(cache.Contains(GetKeyObject(key)));

            cache.Lock(key);
            Assert.AreEqual(cache[GetKeyObject(key)], value);
            cache.Unlock(key);

            cache.Lock(key);
            Assert.AreEqual(cache[GetKeyObject(key)], value);
            cache.Unlock(key);

            CacheFactory.ReleaseCache(cache);
            CacheFactory.Shutdown();
        }
        public void TestIndexes()
        {
            IConfigurableCacheFactory ccf = CacheFactory.ConfigurableCacheFactory;

            IXmlDocument config = XmlHelper.LoadXml("assembly://Coherence.Tests/Tangosol.Resources/s4hc-local-cache-config.xml");

            ccf.Config = config;

            INamedCache cache = CacheFactory.GetCache("local-default");

            Assert.IsTrue(cache is LocalNamedCache);

            LocalNamedCache localNamedCache = cache as LocalNamedCache;

            Assert.IsNotNull(localNamedCache);
            localNamedCache.AddIndex(IdentityExtractor.Instance, true, null);
            localNamedCache.RemoveIndex(IdentityExtractor.Instance);

            localNamedCache.AddIndex(IdentityExtractor.Instance, true, SafeComparer.Instance);
            localNamedCache.RemoveIndex(IdentityExtractor.Instance);

            localNamedCache.AddIndex(IdentityExtractor.Instance, false, null);
            localNamedCache.RemoveIndex(IdentityExtractor.Instance);

            localNamedCache.AddIndex(IdentityExtractor.Instance, false, SafeComparer.Instance);
            localNamedCache.RemoveIndex(IdentityExtractor.Instance);
            CacheFactory.Shutdown();
        }
        public void TestGetKeys()
        {
            INamedCache cache = CacheFactory.GetCache(CacheName);

            cache.Clear();

            IDictionary dict = new Hashtable();

            for (int i = 1; i <= 10; i++)
            {
                dict.Add(GetKeyObject("key" + i), i);
            }
            cache.InsertAll(dict);

            IFilter filter = new GreaterFilter(IdentityExtractor.Instance, 5);

            object[] keys = cache.GetKeys(filter);
            Assert.AreEqual(keys.Length, 5);
            for (int i = 6; i <= 10; i++)
            {
                Assert.Contains(GetKeyObject("key" + i), keys);
            }

            CacheFactory.Shutdown();
        }
        public void TestCustomEvictionPolicy()
        {
            IConfigurableCacheFactory ccf = CacheFactory.ConfigurableCacheFactory;

            IXmlDocument config = XmlHelper.LoadXml("assembly://Coherence.Tests/Tangosol.Resources/s4hc-local-cache-config.xml");

            ccf.Config = config;

            INamedCache cache = CacheFactory.GetCache("local-custom-eviction");

            Assert.IsTrue(cache is LocalNamedCache);

            LocalNamedCache localNamedCache = cache as LocalNamedCache;

            Assert.IsNotNull(localNamedCache);

            LocalCache localCache = localNamedCache.LocalCache;

            Assert.IsNotNull(localCache);

            Assert.AreEqual(localCache.EvictionType, LocalCache.EvictionPolicyType.External);
            Assert.IsInstanceOf(typeof(TestEvictionPolicy), localCache.EvictionPolicy);

            CacheFactory.Shutdown();
        }
        public virtual void TestNamedCacheProperties()
        {
            INamedCache cache = CacheFactory.GetCache(CacheName);
            string      key   = "testNamedCachePropertiesKey";
            string      value = "testNamedCachePropertiesValue";

            // INamedCache
            Assert.IsTrue(cache.IsActive);

            cache.Clear();
            Assert.AreEqual(cache.Count, 0);

            cache.Insert(GetKeyObject(key), value);
            Assert.AreEqual(cache.Count, 1);
            Assert.AreEqual(cache[GetKeyObject(key)], value);

            // SafeNamedCache
            SafeNamedCache safeCache = (SafeNamedCache)cache;

            Assert.IsFalse(safeCache.IsReleased);
            Assert.IsFalse(safeCache.IsFixedSize);
            Assert.IsFalse(safeCache.IsReadOnly);
            Assert.IsTrue(safeCache.IsSynchronized);

            // RemoteNamedCache
            RemoteNamedCache remoteCache = (RemoteNamedCache)safeCache.NamedCache;

            Assert.IsTrue(remoteCache.Channel.IsOpen);
            Assert.IsInstanceOf(typeof(NamedCacheProtocol), remoteCache.Protocol);

            CacheFactory.ReleaseCache(cache);
            CacheFactory.Shutdown();
        }
Exemple #30
0
        public void TestNearCacheTruncate()
        {
            LocalNamedCache localcache = new LocalNamedCache();
            INamedCache     safecache  = CacheFactory.GetCache(CacheName);
            NearCache       nearcache  = new NearCache(localcache, safecache, CompositeCacheStrategyType.ListenNone);

            nearcache.Clear();

            Hashtable ht = new Hashtable();

            ht.Add("Aleks", 1);
            ht.Add("Ana", 2);
            ht.Add("Goran", 3);
            ht.Add("Ivan", 4);
            nearcache.InsertAll(ht);

            IFilter filter = new BetweenFilter(IdentityExtractor.Instance, 2, 10);

            object[] result = nearcache.GetKeys(filter);
            Assert.AreEqual(3, result.Length);

            nearcache.Truncate();
            Assert.IsTrue(nearcache.IsActive);

            Thread.Sleep(2000);
            result = nearcache.GetKeys(filter);
            Assert.AreEqual(0, result.Length);

            nearcache.Release();
            Assert.IsFalse(nearcache.IsActive);

            CacheFactory.Shutdown();
        }