/// <summary> /// Create a new <see cref="RemoteNamedCache"/> for the given /// <see cref="INamedCache"/> name. /// </summary> /// <param name="name"> /// The name of the cache. /// </param> /// <returns> /// A new <b>RemoteNamedCache</b>. /// </returns> public virtual RemoteNamedCache CreateRemoteNamedCache(string name) { IChannel channel = EnsureChannel(); IConnection connection = channel.Connection; IMessageFactory factory = channel.MessageFactory; RemoteNamedCache cache = new RemoteNamedCache(); IPrincipal principal = Thread.CurrentPrincipal; EnsureCacheRequest request = (EnsureCacheRequest)factory.CreateMessage(EnsureCacheRequest.TYPE_ID); request.CacheName = name; Uri uri; try { uri = new Uri((string)channel.Request(request)); } catch (UriFormatException e) { throw new Exception("error instantiating URI", e); } cache.CacheName = name; cache.CacheService = this; cache.DeferKeyAssociationCheck = DeferKeyAssociationCheck; cache.EventDispatcher = EnsureEventDispatcher(); connection.AcceptChannel(uri, cache, principal); return(cache); }
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(); }
/// <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 virtual INamedCache EnsureCache(string name) { if (StringUtils.IsNullOrEmpty(name)) { name = "Default"; } ScopedReferenceStore storeCache = StoreRemoteNamedCache; RemoteNamedCache cache = storeCache.GetCache(name) as RemoteNamedCache; if (cache == null || !cache.IsActive) { lock (storeCache) { cache = storeCache.GetCache(name) as RemoteNamedCache; if (cache == null || !cache.IsActive) { cache = CreateRemoteNamedCache(name); storeCache.PutCache(cache); } } } return(cache); }
/// <summary> /// Execute the action specific to the Message implementation. /// </summary> public override void Run() { IChannel channel = Channel; Debug.Assert(channel != null); RemoteNamedCache cache = (RemoteNamedCache)channel.Receiver; Debug.Assert(cache != null); if (IsTruncate) { Listeners listeners = cache.DeactivationListeners; if (!listeners.IsEmpty) { CacheEventArgs evt = new CacheEventArgs(cache, CacheEventType.Updated, null, null, null, false); CacheListenerSupport.Dispatch(evt, listeners, false); } } else { cache.BinaryCache.Dispatch(EventType, FilterIds, Key, ValueOld, ValueNew, IsSynthetic, (int)TransformState, IsPriming); } }
/// <summary> /// Destroy the given <see cref="RemoteNamedCache"/>. /// </summary> /// <param name="cache"> /// The <b>RemoteNamedCache</b> to destroy. /// </param> protected virtual void DestroyRemoteNamedCache(RemoteNamedCache cache) { ReleaseRemoteNamedCache(cache); IChannel channel = EnsureChannel(); IMessageFactory factory = channel.MessageFactory; DestroyCacheRequest request = (DestroyCacheRequest)factory.CreateMessage(DestroyCacheRequest.TYPE_ID); request.CacheName = cache.CacheName; channel.Request(request); }
/// <summary> /// Releases all the caches fetched from the store and then clears the store. /// </summary> public virtual void ReleaseCaches() { ScopedReferenceStore storeCache = StoreRemoteNamedCache; lock (storeCache) { for (IEnumerator iter = storeCache.GetAllCaches().GetEnumerator(); iter.MoveNext();) { RemoteNamedCache cache = (RemoteNamedCache)iter.Current; ReleaseRemoteNamedCache(cache as RemoteNamedCache); } storeCache.Clear(); } }
/// <summary> /// Release the given <see cref="RemoteNamedCache"/>. /// </summary> /// <param name="cache"> /// The <b>RemoteNamedCache</b> to release. /// </param> protected virtual void ReleaseRemoteNamedCache(RemoteNamedCache cache) { try { // when this is called due to certain connection error, e.g. ping // timeout, the channel could be null and closed. IChannel channel = cache.Channel; if (channel != null) { channel.Close(); } } catch (Exception) {} }
public void TestNearNamedCacheInstancing() { IConfigurableCacheFactory ccf = CacheFactory.ConfigurableCacheFactory; IXmlDocument config = XmlHelper.LoadXml("assembly://Coherence.Tests/Tangosol.Resources/s4hc-near-cache-config.xml"); ccf.Config = config; INamedCache cache = ccf.EnsureCache("dist-extend-direct"); Assert.IsNotNull(cache); Assert.IsInstanceOf(typeof(NearCache), cache); NearCache nc = cache as NearCache; Assert.IsNotNull(nc); ICache fc = nc.FrontCache; INamedCache bc = nc.BackCache; Assert.IsNotNull(fc); Assert.IsNotNull(bc); Assert.IsInstanceOf(typeof(SafeNamedCache), nc.BackCache); Assert.IsInstanceOf(typeof(LocalNamedCache), nc.FrontCache); SafeNamedCache sc = nc.BackCache as SafeNamedCache; Assert.IsNotNull(sc); Assert.AreEqual(sc.CacheName, "dist-extend-direct"); Assert.IsInstanceOf(typeof(RemoteNamedCache), sc.NamedCache); RemoteNamedCache rc = sc.NamedCache as RemoteNamedCache; Assert.IsNotNull(rc); Assert.AreEqual(rc.CacheName, sc.CacheName); LocalNamedCache lnc = nc.FrontCache as LocalNamedCache; Assert.IsNotNull(lnc); LocalCache lc = lnc.LocalCache; Assert.AreEqual(lc.ExpiryDelay, LocalCache.DEFAULT_EXPIRE); Assert.AreEqual(lc.FlushDelay, 0); Assert.AreEqual(lc.HighUnits, LocalCache.DEFAULT_UNITS); Assert.AreEqual(lc.CalculatorType, LocalCache.UnitCalculatorType.Fixed); Assert.AreEqual(lc.EvictionType, LocalCache.EvictionPolicyType.Hybrid); CacheFactory.Shutdown(); }
public void TestRemoteNamedCacheInstancing() { IConfigurableCacheFactory ccf = CacheFactory.ConfigurableCacheFactory; INamedCache cache = ccf.EnsureCache("dist-extend-direct"); Assert.IsNotNull(cache); Assert.IsInstanceOf(typeof(SafeNamedCache), cache); SafeNamedCache sc = cache as SafeNamedCache; Assert.IsNotNull(sc); Assert.AreEqual(sc.CacheName, "dist-extend-direct"); Assert.IsInstanceOf(typeof(RemoteNamedCache), sc.NamedCache); RemoteNamedCache rc = sc.NamedCache as RemoteNamedCache; Assert.IsNotNull(rc); Assert.AreEqual(rc.CacheName, sc.CacheName); Assert.IsNotNull(sc.CacheService); Assert.IsInstanceOf(typeof(SafeCacheService), sc.CacheService); SafeCacheService scs = (SafeCacheService)sc.CacheService; Assert.AreEqual(scs.CacheNames.Count, 1); Assert.Contains(sc.CacheName, new ArrayList(scs.CacheNames)); List <INamedCache> listCache = new List <INamedCache>(scs.StoreSafeNamedCache.GetAllCaches()); Assert.AreEqual(listCache.Count, 1); Assert.Contains(sc.CacheName, new List <string>(scs.StoreSafeNamedCache.GetNames())); Assert.AreEqual(scs.ServiceType, ServiceType.RemoteCache); Assert.IsInstanceOf(typeof(RemoteCacheService), scs.CacheService); Assert.AreEqual(rc.CacheService, scs.CacheService); RemoteCacheService rcs = scs.CacheService as RemoteCacheService; Assert.IsNotNull(rcs); Assert.AreEqual(rcs.ServiceName, "ExtendTcpCacheService"); Assert.IsTrue(cache.IsActive); Assert.IsTrue(cache.CacheService.IsRunning); CacheFactory.Shutdown(); Assert.IsFalse(cache.IsActive); Assert.IsFalse(cache.CacheService.IsRunning); }