/// <summary>
        /// Adds the default memcached.
        /// </summary>
        /// <returns>The default memcached.</returns>
        /// <param name="services">Services.</param>
        /// <param name="options">Options.</param>
        /// <param name="providerAction">Provider action.</param>
        public static IServiceCollection AddDefaultMemcached(
            this IServiceCollection services,
            Action <EasyCachingMemcachedClientOptions> options,
            Action <MemcachedOptions> providerAction)
        {
            ArgumentCheck.NotNull(services, nameof(services));
            ArgumentCheck.NotNull(options, nameof(options));
            ArgumentCheck.NotNull(providerAction, nameof(providerAction));

            services.AddOptions();
            services.Configure(options);

            var providerOptioin = new MemcachedOptions();

            providerAction(providerOptioin);
            services.AddSingleton(providerOptioin);

            services.TryAddTransient <IMemcachedClientConfiguration, EasyCachingMemcachedClientConfiguration>();
            services.TryAddSingleton <MemcachedClient, MemcachedClient>();
            services.TryAddSingleton <IMemcachedClient>(factory => factory.GetService <MemcachedClient>());

            services.TryAddSingleton <ITranscoder, EasyCachingTranscoder>();
            services.TryAddSingleton <IEasyCachingSerializer, DefaultBinaryFormatterSerializer>();
            services.TryAddSingleton <IMemcachedKeyTransformer, DefaultKeyTransformer>();

            services.TryAddSingleton <IEasyCachingProvider, DefaultMemcachedCachingProvider>();

            return(services);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="T:EasyCaching.Memcached.DefaultMemcachedCachingProvider"/> class.
        /// </summary>
        /// <param name="name">Name.</param>
        /// <param name="memcachedClients">Memcached client.</param>
        /// <param name="options">Options.</param>
        /// <param name="loggerFactory">Logger factory.</param>
        public DefaultMemcachedCachingProvider(
            string name,
            IEnumerable <EasyCachingMemcachedClient> memcachedClients,
            MemcachedOptions options,
            ILoggerFactory loggerFactory = null)
        {
            this._name            = name;
            this._memcachedClient = memcachedClients.Single(x => x.Name.Equals(this._name));
            this._options         = options;

            if (options.EnableLogging)
            {
                this.Logger = loggerFactory.CreateLogger <DefaultMemcachedCachingProvider>();
            }

            this._cacheStats = new CacheStats();

            this.ProviderName        = this._name;
            this.ProviderType        = CachingProviderType.Memcached;
            this.ProviderStats       = this._cacheStats;
            this.ProviderMaxRdSecond = _options.MaxRdSecond;

            _info = new ProviderInfo
            {
                CacheStats     = _cacheStats,
                EnableLogging  = options.EnableLogging,
                LockMs         = options.LockMs,
                MaxRdSecond    = options.MaxRdSecond,
                ProviderName   = ProviderName,
                ProviderType   = ProviderType,
                SerializerName = options.SerializerName,
                SleepMs        = options.SleepMs,
                CacheNulls     = options.CacheNulls,
            };
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="T:EasyCaching.Memcached.DefaultMemcachedCachingProvider"/> class.
 /// </summary>
 /// <param name="memcachedClient">Memcached client.</param>
 public DefaultMemcachedCachingProvider(
     IMemcachedClient memcachedClient,
     MemcachedOptions options)
 {
     this._memcachedClient = memcachedClient;
     this._options         = options;
 }
Exemplo n.º 4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="T:EasyCaching.Memcached.DefaultMemcachedCachingProvider"/> class.
 /// </summary>
 /// <param name="name">Name.</param>
 /// <param name="memcachedClients">Memcached client.</param>
 /// <param name="options">Options.</param>
 /// <param name="loggerFactory">Logger factory.</param>
 public DefaultMemcachedCachingProvider(
     string name,
     IEnumerable <EasyCachingMemcachedClient> memcachedClients,
     MemcachedOptions options,
     ILoggerFactory loggerFactory = null)
     : this(name, memcachedClients, options, null, loggerFactory)
 {
 }
Exemplo n.º 5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="T:EasyCaching.Memcached.DefaultMemcachedCachingProvider"/> class.
 /// </summary>
 /// <param name="memcachedClient">Memcached client.</param>
 public DefaultMemcachedCachingProvider(
     IMemcachedClient memcachedClient,
     MemcachedOptions options,
     ILoggerFactory loggerFactory = null)
 {
     this._memcachedClient = memcachedClient;
     this._options         = options;
     this._logger          = loggerFactory?.CreateLogger <DefaultMemcachedCachingProvider>();
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="T:EasyCaching.Memcached.DefaultMemcachedCachingProvider"/> class.
        /// </summary>
        /// <param name="memcachedClient">Memcached client.</param>
        public DefaultMemcachedCachingProvider(
            IMemcachedClient memcachedClient,
            IOptionsMonitor <MemcachedOptions> options,
            ILoggerFactory loggerFactory = null)
        {
            this._memcachedClient = memcachedClient;
            this._options         = options.CurrentValue;
            this._logger          = loggerFactory?.CreateLogger <DefaultMemcachedCachingProvider>();

            this._cacheStats = new CacheStats();
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="T:EasyCaching.Memcached.DefaultMemcachedCachingProvider"/> class.
        /// </summary>
        /// <param name="memcachedClients">Memcached client.</param>
        /// <param name="options">Options.</param>
        /// <param name="loggerFactory">Logger factory.</param>
        public DefaultMemcachedCachingProvider(
            IEnumerable <EasyCachingMemcachedClient> memcachedClients,
            IOptionsMonitor <MemcachedOptions> options,
            ILoggerFactory loggerFactory = null)
        {
            this._name            = EasyCachingConstValue.DefaultMemcachedName;
            this._memcachedClient = memcachedClients.FirstOrDefault(x => x.Name.Equals(this._name));
            this._options         = options.CurrentValue;
            this._logger          = loggerFactory?.CreateLogger <DefaultMemcachedCachingProvider>();

            this._cacheStats = new CacheStats();
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="T:EasyCaching.Memcached.DefaultMemcachedCachingProvider"/> class.
        /// </summary>
        /// <param name="name">Name.</param>
        /// <param name="memcachedClients">Memcached client.</param>
        /// <param name="options">Options.</param>
        /// <param name="loggerFactory">Logger factory.</param>
        public DefaultMemcachedCachingProvider(
            string name,
            IEnumerable <EasyCachingMemcachedClient> memcachedClients,
            MemcachedOptions options,
            ILoggerFactory loggerFactory = null)
        {
            this._name            = name;
            this._memcachedClient = memcachedClients.FirstOrDefault(x => x.Name.Equals(this._name));
            this._options         = options;
            this._logger          = loggerFactory?.CreateLogger <DefaultMemcachedCachingProvider>();

            this._cacheStats = new CacheStats();
        }
        /// <summary>
        /// Adds the default memcached.
        /// </summary>
        /// <returns>The default redis cache.</returns>
        /// <param name="services">Services.</param>
        /// <param name="options">Options.</param>
        public static IServiceCollection AddDefaultMemcached(
            this IServiceCollection services,
            Action <EasyCachingMemcachedClientOptions> options)
        {
            var providerOptioin = new MemcachedOptions();

            return(services.AddDefaultMemcached(options, x =>
            {
                x.CachingProviderType = providerOptioin.CachingProviderType;
                x.MaxRdSecond = providerOptioin.MaxRdSecond;
                x.Order = providerOptioin.Order;
            }));
        }
Exemplo n.º 10
0
        /// <summary>
        /// Initializes a new instance of the <see cref="T:EasyCaching.Memcached.EasyCachingTranscoder"/> class.
        /// </summary>
        /// <param name="name">Name.</param>
        /// <param name="options">Options.</param>
        /// <param name="serializers">Serializers.</param>
        public EasyCachingTranscoder(string name, MemcachedOptions options, IEnumerable <IEasyCachingSerializer> serializers)
        {
            if (string.IsNullOrWhiteSpace(options.SerializerName))
            {
                this._name       = name;
                this._serializer = serializers.FirstOrDefault(x => x.Name.Equals(_name)) ?? serializers.Single(x => x.Name.Equals(EasyCachingConstValue.DefaultSerializerName));
            }
            else
            {
                this._name       = options.SerializerName;
                this._serializer = serializers.Single(x => x.Name.Equals(options.SerializerName));
            }

            //this._serializer = !string.IsNullOrWhiteSpace(options.SerializerName)
            //    ? serializers.Single(x => x.Name.Equals(options.SerializerName))
            //    : serializers.FirstOrDefault(x => x.Name.Equals(_name)) ?? serializers.Single(x => x.Name.Equals(EasyCachingConstValue.DefaultSerializerName));
        }
        /// <summary>
        /// Uses the memcached.
        /// </summary>
        /// <returns>The memcached.</returns>
        /// <param name="options">Options.</param>
        /// <param name="configuration">Configuration.</param>
        /// <param name="name">Name.</param>
        /// <param name="sectionName">Section name.</param>
        public static EasyCachingOptions UseMemcached(this EasyCachingOptions options, IConfiguration configuration, string name = "", string sectionName = EasyCachingConstValue.MemcachedSection)
        {
            var dbConfig = configuration.GetSection(sectionName);
            var mOptions = new MemcachedOptions();

            dbConfig.Bind(mOptions);

            void configure(MemcachedOptions x)
            {
                x.EnableLogging = mOptions.EnableLogging;
                x.MaxRdSecond   = mOptions.MaxRdSecond;
                x.DBConfig      = mOptions.DBConfig;
            }

            options.RegisterExtension(new MemcachedOptionsExtension(name, configure));
            return(options);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="T:EasyCaching.Memcached.DefaultMemcachedCachingProvider"/> class.
        /// </summary>
        /// <param name="name">Name.</param>
        /// <param name="memcachedClients">Memcached client.</param>
        /// <param name="options">Options.</param>
        /// <param name="loggerFactory">Logger factory.</param>
        public DefaultMemcachedCachingProvider(
            string name,
            IEnumerable <EasyCachingMemcachedClient> memcachedClients,
            MemcachedOptions options,
            ILoggerFactory loggerFactory = null)
        {
            this._name            = name;
            this._memcachedClient = memcachedClients.Single(x => x.Name.Equals(this._name));
            this._options         = options;
            this._logger          = loggerFactory?.CreateLogger <DefaultMemcachedCachingProvider>();
            this._cacheStats      = new CacheStats();

            this.ProviderName          = this._name;
            this.ProviderType          = CachingProviderType.Memcached;
            this.ProviderStats         = this._cacheStats;
            this.ProviderMaxRdSecond   = _options.MaxRdSecond;
            this.IsDistributedProvider = true;
        }
Exemplo n.º 13
0
        /// <summary>
        /// Initializes a new instance of the <see cref="T:EasyCaching.Memcached.DefaultMemcachedCachingProvider"/> class.
        /// </summary>
        /// <param name="memcachedClients">Memcached client.</param>
        /// <param name="options">Options.</param>
        /// <param name="loggerFactory">Logger factory.</param>
        public DefaultMemcachedCachingProvider(
            IEnumerable <EasyCachingMemcachedClient> memcachedClients,
            IOptionsMonitor <MemcachedOptions> options,
            ILoggerFactory loggerFactory = null)
        {
            this._name            = EasyCachingConstValue.DefaultMemcachedName;
            this._memcachedClient = memcachedClients.FirstOrDefault(x => x.Name.Equals(this._name));
            this._options         = options.CurrentValue;
            this._logger          = loggerFactory?.CreateLogger <DefaultMemcachedCachingProvider>();

            this._cacheStats = new CacheStats();

            this.ProviderName          = this._name;
            this.ProviderStats         = this._cacheStats;
            this.ProviderType          = _options.CachingProviderType;
            this.ProviderOrder         = _options.Order;
            this.ProviderMaxRdSecond   = _options.MaxRdSecond;
            this.IsDistributedProvider = true;
        }
Exemplo n.º 14
0
        public EasyCachingMemcachedClientConfiguration(
            string name,
            ILoggerFactory loggerFactory,
            MemcachedOptions optionsAccessor,
            IEnumerable <EasyCachingTranscoder> transcoders = null,
            IMemcachedKeyTransformer keyTransformer         = null)
        {
            this._name = name;
            if (optionsAccessor == null)
            {
                throw new ArgumentNullException(nameof(optionsAccessor));
            }

            _logger = loggerFactory.CreateLogger <EasyCachingMemcachedClientConfiguration>();

            var options = optionsAccessor.DBConfig;

            ConfigureServers(options);

            SocketPool = new SocketPoolConfiguration();
            if (options.SocketPool != null)
            {
                options.SocketPool.CheckPoolSize();
                options.SocketPool.CheckTimeout();

                SocketPool.MinPoolSize = options.SocketPool.MinPoolSize;
                _logger.LogInformation($"{nameof(SocketPool.MinPoolSize)}: {SocketPool.MinPoolSize}");

                SocketPool.MaxPoolSize = options.SocketPool.MaxPoolSize;
                _logger.LogInformation($"{nameof(SocketPool.MaxPoolSize)}: {SocketPool.MaxPoolSize}");

                SocketPool.ConnectionTimeout = options.SocketPool.ConnectionTimeout;
                _logger.LogInformation($"{nameof(SocketPool.ConnectionTimeout)}: {SocketPool.ConnectionTimeout}");

                SocketPool.ReceiveTimeout = options.SocketPool.ReceiveTimeout;
                _logger.LogInformation($"{nameof(SocketPool.ReceiveTimeout)}: {SocketPool.ReceiveTimeout}");

                SocketPool.DeadTimeout = options.SocketPool.DeadTimeout;
                _logger.LogInformation($"{nameof(SocketPool.DeadTimeout)}: {SocketPool.DeadTimeout}");

                SocketPool.QueueTimeout = options.SocketPool.QueueTimeout;
                _logger.LogInformation($"{nameof(SocketPool.QueueTimeout)}: {SocketPool.QueueTimeout}");

                SocketPool.InitPoolTimeout = options.SocketPool.InitPoolTimeout;
            }

            Protocol = options.Protocol;

            if (options.Authentication != null && !string.IsNullOrEmpty(options.Authentication.Type))
            {
                try
                {
                    var authenticationType = Type.GetType(options.Authentication.Type);
                    if (authenticationType != null)
                    {
                        _logger.LogDebug($"Authentication type is {authenticationType}.");

                        Authentication      = new AuthenticationConfiguration();
                        Authentication.Type = authenticationType;
                        foreach (var parameter in options.Authentication.Parameters)
                        {
                            Authentication.Parameters[parameter.Key] = parameter.Value;
                            _logger.LogDebug($"Authentication {parameter.Key} is '{parameter.Value}'.");
                        }
                    }
                    else
                    {
                        _logger.LogError($"Unable to load authentication type {options.Authentication.Type}.");
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError(new EventId(), ex, $"Unable to load authentication type {options.Authentication.Type}.");
                }
            }

            if (keyTransformer != null)
            {
                this.KeyTransformer = keyTransformer;
                _logger.LogDebug($"Use KeyTransformer Type : '{keyTransformer.ToString()}'");
            }

            if (NodeLocator == null)
            {
                NodeLocator = options.Servers.Count > 1 ? typeof(DefaultNodeLocator) : typeof(SingleNodeLocator);
            }

            if (transcoders != null)
            {
                EasyCachingTranscoder coder = null;

                if (string.IsNullOrWhiteSpace(optionsAccessor.SerializerName))
                {
                    coder = transcoders.FirstOrDefault(x => x.Name.Equals(_name));
                }
                else
                {
                    coder = transcoders.FirstOrDefault(x => x.Name.Equals(optionsAccessor.SerializerName));
                }

                if (coder != null)
                {
                    this._transcoder = coder;
                    _logger.LogDebug($"Use Transcoder Type : '{coder.ToString()}'");
                }
            }

            if (options.NodeLocatorFactory != null)
            {
                NodeLocatorFactory = options.NodeLocatorFactory;
            }
        }