コード例 #1
0
        /// <summary>
        /// Gets the database connection.
        /// </summary>
        public virtual MemcachedClient GetClient()
        {
            if (!_options.IsUseConfigSetting)
            {
                //初始化缓存
                string host    = _options.ConnectionString;
                var    options = new MemcachedClientOptions();
                MemcachedClientConfiguration memConfig = new MemcachedClientConfiguration(_loggerFacotry, options);
                //   IPAddress newaddress = IPAddress.Parse(Dns.GetHostEntry(host).AddressList[0].ToString());//your_ocs_host替换为OCS内网地址
                //   IPEndPoint ipEndPoint = new IPEndPoint(newaddress, 11211);
                DnsEndPoint dnsEndPoint = new DnsEndPoint(host, 11211);

                // 配置文件 - ip
                memConfig.Servers.Add(dnsEndPoint);
                // 配置文件 - 协议
                memConfig.Protocol = MemcachedProtocol.Binary;
                // 配置文件-权限
                memConfig.Authentication.Type = typeof(PlainTextAuthenticator);
                memConfig.Authentication.Parameters["zone"]     = "";
                memConfig.Authentication.Parameters["userName"] = _options.CacheServerUid;
                memConfig.Authentication.Parameters["password"] = _options.CacheServerPwd;
                //下面请根据实例的最大连接数进行设置
                memConfig.SocketPool.MinPoolSize = 5;
                memConfig.SocketPool.MaxPoolSize = 200;
                return(new MemcachedClient(_loggerFacotry, memConfig));
            }
            else
            {
                var options = new MemcachedClientOptions();
                UtilConf.Configuration.GetSection("enyimMemcached").Bind(options);
                return(new MemcachedClient(_loggerFacotry, new MemcachedClientConfiguration(_loggerFacotry, options)));
            }
        }
コード例 #2
0
        internal MemCachedClient()
        {
            LoggerFactory logFactory = new LoggerFactory();

            logFactory.AddConsole(LogLevel.Information, true);
            m_Logger = logFactory.CreateLogger <MemCachedClient>();

            initializeParameters();

            using (m_Logger.BeginScope("Creating client '{0}:{1}'", HostName, Port))
            {
                try
                {
                    MemcachedClientConfiguration baseConfig    = null;
                    MemcachedClientOptions       configOptions = new MemcachedClientOptions();
                    configOptions.AddServer(HostName, Port);
                    configOptions.Protocol = Enyim.Caching.Memcached.MemcachedProtocol.Text;

                    baseConfig = new MemcachedClientConfiguration(new LoggerFactory(), configOptions);

                    m_Client = new MemcachedClient(new LoggerFactory(), baseConfig);

                    m_Logger.LogDebug("Memory cached client created.");
                }
                catch (Exception ex)
                {
                    m_Logger.LogError(ex, "Memory cache client creation failed.");
                }
            }
        }
コード例 #3
0
ファイル: MCached.cs プロジェクト: wwkkww1983/SYCMS1.0
        static void MemClientInit()
        {
            var options = new MemcachedClientOptions();

            UtilConf.Configuration.GetSection("enyimMemcached").Bind(options);

            MemClient = new MemcachedClient(
                _loggerFacotry,
                new MemcachedClientConfiguration(_loggerFacotry, options));
        }
コード例 #4
0
        public static IServiceCollection AddMemcached(this IServiceCollection services, IConfiguration configuration)
        {
            MemcachedConfig = configuration.GetSection("Redis").Get <MemcachedClientOptions>();
            if (MemcachedConfig == null)
            {
                Console.WriteLine("Memcached Configuration: Not found");
                return(services);
            }

            services.AddEnyimMemcached();
            return(services);
        }
コード例 #5
0
 /// <summary>
 /// 构造函数,连接memcachedcore并为KEYS字典开辟储存空间
 /// </summary>
 static MemcachedHelper()
 {
     if (mclient == null)
     {
         lock (mlock)
         {
             var options = new MemcachedClientOptions();
             UtilConf.Configuration.GetSection("enyimMemcached").Bind(options);
             mclient = new MemcachedClient(_loggerFacotry, new MemcachedClientConfiguration(_loggerFacotry, options));
         }
     }
 }
コード例 #6
0
ファイル: MCached.cs プロジェクト: wwkkww1983/SYCMS1.0
        public IMemcachedClientFactory Add(string keyOfConfiguration)
        {
            var options = new MemcachedClientOptions();

            _configuration.GetSection(keyOfConfiguration).Bind(options);

            var memcachedClient = new MemcachedClient(
                _loggerFacotry,
                new MemcachedClientConfiguration(_loggerFacotry, options));

            _clients.Add(keyOfConfiguration, memcachedClient);

            return(this);
        }
コード例 #7
0
 /// <summary>
 /// 构造函数,连接memcachedcore并为KEYS字典开辟储存空间
 /// </summary>
 static MemcachedHelper_Old()
 {
     //mclient = MemCached.getInstance();
     if (mclient == null)
     {
         lock (mlock)
         {
             if (mclient == null)
             {
                 var options = new MemcachedClientOptions();
                 UtilConf.Configuration.GetSection("enyimMemcached").Bind(options);
                 mclient = new MemcachedClient(_loggerFacotry, new MemcachedClientConfiguration(_loggerFacotry, options));
             }
         }
     }
     //在缓存中开辟一个专门用来存储Kyes的字典对象
     MDictionary_SaveDict(new Dictionary <string, List <string> >());
 }
コード例 #8
0
        /// <summary>
        /// Constructor with configuration properties. It supports <c>configuration</c>, which has to be a JSON string
        /// structured like the value part of the <c>"enyimMemcached"</c> property in an appsettings.json file.
        /// </summary>
        /// <param name="properties">The configurations properties.</param>
        public MemcachedFactory(IDictionary <string, string> properties) : this()
        {
            MemcachedClientOptions options;

            if (properties != null && properties.TryGetValue(_configuration, out var configuration) && !string.IsNullOrWhiteSpace(configuration))
            {
                options = JsonConvert.DeserializeObject <MemcachedClientOptions>(configuration);
            }
            else
            {
                Log.Warn("No {0} property provided", _configuration);
                options = new MemcachedClientOptions();
            }

            var loggerFactory = new LoggerFactory();

            _cache = new MemcachedClient(loggerFactory, new MemcachedClientConfiguration(loggerFactory, options));
        }
コード例 #9
0
        /// <summary>
        /// Create a new MemcachedAccess instance.
        /// </summary>
        /// <param name="memcachedConfig">The Memcached configuration.</param>
        public MemcachedAccess(MemcachedConfig memcachedConfig)
        {
            var loggerFactory      = new LoggerFactory();
            var splittedConnstring = memcachedConfig.ConnectionString.Split(":");
            var options            = new MemcachedClientOptions()
            {
                Protocol = MemcachedProtocol.Text,
                Servers  = new List <Server> {
                    new Server
                    {
                        Address = splittedConnstring[0].Trim(),
                        Port    = int.Parse(splittedConnstring[1].Trim())
                    }
                }
            };
            var config = new MemcachedClientConfiguration(loggerFactory, options);

            MemcachedClient = new MemcachedClient <ConnectorEntity>(loggerFactory, config);
        }
コード例 #10
0
        private static string GoMemcached(string cs)
        {
            var parts = cs.Split(':');
            var host  = cs;
            int port  = 11211;

            if (parts.Length == 2)
            {
                host = parts[0];
                Int32.TryParse(parts[1], out port);
            }

            Func <IPAddress, int> order = ip => ip.ToString().IndexOf(".") >= 0 ? 1 : 2;
            IPAddress             address;

            if (!IPAddress.TryParse(host, out address))
            {
                IPAddress[] resolved = Dns.GetHostAddresses(host);
                var         ordered  = resolved.OrderBy(x => order(x)).ToArray();
                address = ordered.FirstOrDefault();
            }

            if (address == null)
            {
                throw new ArgumentException($"Unable to resolve IP Address of {cs}");
            }


            MemcachedClientOptions opts = new MemcachedClientOptions();

            opts.Protocol = MemcachedProtocol.Binary;
            var ipEndPoint        = new DnsEndPoint(host, port);
            var nullLoggerFactory = Microsoft.Extensions.Logging.Abstractions.NullLoggerFactory.Instance;
            MemcachedClientConfiguration config = new MemcachedClientConfiguration(nullLoggerFactory, opts);

            config.Servers.Add(ipEndPoint);
            config.Protocol = MemcachedProtocol.Binary;

            // config.Authentication.Type = typeof(PlainTextAuthenticator);
            // config.Authentication.Parameters["userName"] = "******";
            // config.Authentication.Parameters["password"] = "******";

            var mc = new MemcachedClient(nullLoggerFactory, config);

            mc.Get($"PING_({Guid.NewGuid().ToString("N")})");
            // return "OK. TryGet took " + (sw.ElapsedTicks / Convert.ToDecimal(Stopwatch.Frequency)).ToString("f2") + " msec";

            Stopwatch   sw = Stopwatch.StartNew();
            ServerStats stats;

            try
            {
                stats = mc.Stats();
            }
            catch (NullReferenceException)
            {
                // yes, memcached client is buggy
                throw new Exception("No memcached connection is available to service this operation");
            }
            var rawVersion     = stats.GetRaw("version");
            var rawPointerSize = stats.GetRaw("pointer_size");
            var version        = rawVersion.FirstOrDefault().Value ?? "N/A";
            var pointerSize    = rawPointerSize.FirstOrDefault().Value;

            if (!string.IsNullOrEmpty(pointerSize))
            {
                version += $" [{pointerSize} bits]";
            }

            return(version);
        }