/// <summary> /// This is the Sample Application that uses the Infinispan .NET client library. /// </summary> /// <param name="args">string</param> static void Main(string[] args) { //Create new Configuration, overriding the setting in the App.config file. ClientConfig conf = new ClientConfig("127.0.0.1", 11222, "default", 0, false); //Here we are using a custom Serializer Serializer s = new DefaultSerializer(); //Create a new RemoteCacheManager RemoteCacheManager manager = new RemoteCacheManager(conf, s); //Get hold of a cache from the remote cache manager RemoteCache cache = manager.getCache(); //First Check Whether the cache exists Console.WriteLine("Ping Result : " + cache.ping()); //Put a new value "germanium" with key "key 1" into cache cache.put <String, String>("key 1", "germanium", 0, 0); //Get the value of entry with key "key 1" Console.WriteLine("key 1 value : " + cache.get <String>("key 1")); //Put if absent is used to add entries if they are not existing in the cache cache.putIfAbsent <String, String>("key 1", "trinitrotoluene", 0, 0); cache.putIfAbsent <String, String>("key 2", "formaldehyde", 0, 0); Console.WriteLine("key 1 value after PutIfAbsent: " + cache.get <String>("key 1")); Console.WriteLine("Key 2 value after PutIfAbsent: " + cache.get <String>("key 2")); //Replace an existing value with a new one. // cache.replace<String, String>("key 1", "fluoride",0,0); Console.WriteLine("key 1 value after replace: " + cache.get <String>("key 1")); //Check whether a particular key exists Console.WriteLine("key 1 is exist ?: " + cache.containsKey("key 1")); //Remove a particular entry from the cache // cache.remove<String>("key 1"); Console.WriteLine("key 1 is exist after remove?: " + cache.containsKey("key 1")); Console.ReadLine(); }
/// <summary> /// 授权事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public static void AuthenticateRequestEvent <TPrincipal, TIdentity>(object sender, EventArgs e) where TIdentity : class where TPrincipal : IPrincipal { var context = new HttpContextWrapper(HttpContext.Current); var webCookies = new WebCookies(context); if (context.Request.Url != null) { var pathAndQuery = context.Request.Url.PathAndQuery.ToLower(); if (_ignorePaths.Any(pathAndQuery.StartsWith)) { return; } } if (webCookies.HasLogOn) { string message = ""; try { var identity = RemoteCache.Get <TIdentity>(context, "GetIdentity", "GanjiNETSSOIdentityKey_{0}"); if (identity != null) { var principal = (TPrincipal)Activator.CreateInstance(typeof(TPrincipal), identity); context.User = principal; return; } } catch (Exception ep) { message = ep.Message; } webCookies.ClearInternal(); context.Response.ContentEncoding = Encoding.UTF8; context.Response.HeaderEncoding = Encoding.UTF8; context.Response.Write( string.Format( @" <!DOCTYPE html><html><head> <title>请重新登录</title> <meta charset='utf-8' /> </head> <body> 用户认证失败,请重新<a href='" + AuthClient.GetLogOnUrl() + @"'>登录</a>!<br /> {0} </body></html> ", message)); context.Response.End(); } }
private static Cache InitializeCacheInternal(string cacheId, CacheInitParams initParams) { if (cacheId == null) { throw new ArgumentNullException("cacheId"); } if (cacheId == string.Empty) { throw new ArgumentException("cacheId cannot be an empty string"); } CacheMode mode = initParams.Mode; int maxTries = 2; try { CacheServerConfig config = null; if (mode != CacheMode.OutProc) { do { try { config = DirectoryUtil.GetCacheDom(cacheId, mode == CacheMode.InProc); } catch (Exception ex) { if (mode == CacheMode.Default) { mode = CacheMode.OutProc; } else { throw ex; } } if (config != null) { switch (mode) { case CacheMode.InProc: config.InProc = true; break; case CacheMode.OutProc: config.InProc = false; break; } } break; } while (maxTries > 0); } lock (typeof(NCache)) { Cache primaryCache = null; lock (s_webCaches) { if (!s_webCaches.Contains(cacheId)) { CacheImplBase cacheImpl = null; if (config != null && config.InProc) { Alachisoft.NCache.Caching.Cache ncache = null; Cache cache = null; maxTries = 2; do { try { CacheConfig cacheConfig = CacheConfig.FromDom(config); if (Web.Caching.APILogging.DebugAPIConfiguraions.LoggingEnabled) { cache = new WrapperCache(new Cache(null, cacheConfig)); } else { cache = new Cache(null, cacheConfig); } ncache = CacheFactory.CreateFromPropertyString(cacheConfig.PropertyString, config, false, false); cacheImpl = new InprocCache(ncache, cacheConfig, cache); cache.CacheImpl = cacheImpl; if (primaryCache == null) { primaryCache = cache; } break; } catch (Exception) { } } while (maxTries > 0); } else { maxTries = 2; do { try { // does an AddRef() internally. PerfStatsCollector2 perfStatsCollector = new PerfStatsCollector2(cacheId, false); if (Web.Caching.APILogging.DebugAPIConfiguraions.LoggingEnabled) { primaryCache = new WrapperCache(new Cache(null, cacheId, perfStatsCollector)); } else { primaryCache = new Cache(null, cacheId, perfStatsCollector); } cacheImpl = new RemoteCache(cacheId, primaryCache, initParams, perfStatsCollector); perfStatsCollector.InitializePerfCounters(false); primaryCache.CacheImpl = cacheImpl; break; } catch (OperationNotSupportedException ex) { throw ex; } } while (maxTries > 0); } if (primaryCache != null) { primaryCache.InitializeCompactFramework(); s_webCaches.AddCache(cacheId, primaryCache); } } else { lock (s_webCaches.GetCache(cacheId)) { primaryCache = s_webCaches.GetCache(cacheId) as Cache; primaryCache.AddRef(); } } } lock (s_webCache) { // it is first cache instance. if (s_webCache.CacheImpl == null) { primaryCache.ExceptionsEnabled = ExceptionsEnabled; s_webCache = primaryCache; } } return(primaryCache); } } catch (Exception) { throw; } }
private static Cache InitializeCacheInternal(string cacheId, CacheInitParams initParams, bool isRemoveCache = true) { if (cacheId == null) { throw new ArgumentNullException("cacheId"); } if (cacheId == string.Empty) { throw new ArgumentException("cacheId cannot be an empty string"); } CacheMode mode = initParams.Mode; int maxTries = 2; try { CacheServerConfig config = null; if (mode != CacheMode.OutProc) { do { try { config = DirectoryUtil.GetCacheDom(cacheId, mode == CacheMode.InProc); } catch (Exception ex) { if (mode == CacheMode.Default) { mode = CacheMode.OutProc; } else { throw ex; } } if (config != null) { if (config.CacheType.ToLower().Equals("clustered-cache")) { throw new Exception("Cluster cache cannot be initialized in In-Proc mode."); } switch (mode) { case CacheMode.InProc: config.InProc = true; break; case CacheMode.OutProc: config.InProc = false; break; } } break; } while (maxTries > 0); } lock (typeof(NCache)) { Cache primaryCache = null; lock (s_webCaches) { if (!s_webCaches.Contains(cacheId)) { CacheImplBase cacheImpl = null; if (config != null && config.InProc) { Alachisoft.NCache.Caching.Cache ncache = null; Cache cache = null; maxTries = 2; do { CacheConfig cacheConfig = CacheConfig.FromDom(config); cache = new Cache(null, cacheConfig); ncache = CacheFactory.CreateFromPropertyString(cacheConfig.PropertyString, config, false, false); cacheImpl = new InprocCache(ncache, cacheConfig, cache); cache.CacheImpl = cacheImpl; if (primaryCache == null) { primaryCache = cache; } else { primaryCache.AddSecondaryInprocInstance(cache); } break; } while (maxTries > 0); } else { maxTries = 2; do { try { PerfStatsCollector2 perfStatsCollector = new PerfStatsCollector2(cacheId, false); primaryCache = new Cache(null, cacheId, perfStatsCollector); cacheImpl = new RemoteCache(cacheId, primaryCache, initParams, perfStatsCollector); perfStatsCollector.InitializePerfCounters(false); primaryCache.CacheImpl = cacheImpl; break; } catch (OperationNotSupportedException ex) { throw ex; } } while (maxTries > 0); } if (primaryCache != null) { s_webCaches.AddCache(cacheId, primaryCache); } } else { lock (s_webCaches.GetCache(cacheId)) { primaryCache = s_webCaches.GetCache(cacheId) as Cache; primaryCache.AddRef(); } } } lock (s_webCache) { if (s_webCache.CacheImpl == null) { primaryCache.ExceptionsEnabled = ExceptionsEnabled; s_webCache = primaryCache; } } return(primaryCache); } } catch (Exception) { throw; } }
public HybirdCache(string host, long?capacity, int expiration = 60) { _remote = new RemoteCache(host); _local = new LightCache(capacity, expiration); Subscribe(); }
private static Cache GetCacheInternal(string cacheName, CacheConnectionOptions cacheConnectionOptions) { if (cacheName == null) { throw new ArgumentNullException("cacheId"); } if (cacheName == string.Empty) { throw new ArgumentException("cacheId cannot be an empty string"); } IsolationLevel mode = cacheConnectionOptions.Mode.Value; string cacheIdWithAlias = cacheName; int maxTries = 2; try { CacheServerConfig config = null; if (mode != IsolationLevel.OutProc) { do { try { config = DirectoryUtil.GetCacheDom(cacheName, null, null, mode == IsolationLevel.InProc); } catch (Exception ex) { if (mode == IsolationLevel.Default) { mode = IsolationLevel.OutProc; } else { throw ex; } } if (config != null) { if (config.CacheType.ToLower().Equals("clustered-cache")) { throw new OperationFailedException(ErrorCodes.CacheInit.CLUSTER_INIT_IN_INPROC, ErrorMessages.GetErrorMessage(ErrorCodes.CacheInit.CLUSTER_INIT_IN_INPROC)); } switch (mode) { case IsolationLevel.InProc: config.InProc = true; break; case IsolationLevel.OutProc: config.InProc = false; break; } } break; } while (maxTries > 0); } lock (typeof(CacheManager)) { Cache primaryCache = null; lock (_cacheCollection) { if (!_cacheCollection.Contains(cacheIdWithAlias)) { CacheImplBase cacheImpl = null; if (config != null && config.InProc) { NCache.Caching.Cache ncache = null; Cache cache = null; maxTries = 2; do { try { CacheConfig cacheConfig = CacheConfig.FromDom(config); cache = new Cache(null, cacheConfig); ncache = CacheFactory.CreateFromPropertyString(cacheConfig.PropertyString, config, null, null, false, false); cacheImpl = new InprocCache(ncache, cacheConfig, cache, null, null); cache.CacheImpl = cacheImpl; if (primaryCache == null) { primaryCache = cache; } else { primaryCache.AddSecondaryInprocInstance(cache); } break; } catch (SecurityException se) { maxTries--; if (maxTries == 0) { throw se; } } } while (maxTries > 0); } else { maxTries = 2; do { try { StatisticsCounter perfStatsCollector; if (ServiceConfiguration.PublishCountersToCacheHost) { perfStatsCollector = new CustomStatsCollector(cacheName, false); ClientConfiguration clientConfig = new ClientConfiguration(cacheName); clientConfig.LoadConfiguration(); perfStatsCollector.StartPublishingCounters(clientConfig.BindIP); } else { perfStatsCollector = new PerfStatsCollector(cacheName, false); } primaryCache = new Cache(null, cacheName, perfStatsCollector); cacheImpl = new RemoteCache(cacheName, primaryCache, cacheConnectionOptions, perfStatsCollector); perfStatsCollector.InitializePerfCounters(false); primaryCache.CacheImpl = cacheImpl; break; } catch (OperationNotSupportedException ex) { throw ex; } } while (maxTries > 0); } if (primaryCache != null) { primaryCache.InitializeCompactFramework(); _cacheCollection.AddCache(cacheIdWithAlias, primaryCache); } } else { lock (_cacheCollection.GetCache(cacheIdWithAlias, false)) { primaryCache = _cacheCollection.GetCache(cacheIdWithAlias, false) as Cache; primaryCache.AddRef(); } } } lock (_cache) { // it is first cache instance. if (_cache.CacheImpl == null) { primaryCache.ExceptionsEnabled = ExceptionsEnabled; _cache = primaryCache; } } return(primaryCache); } } catch (Exception) { throw; } }