MemoryCacheServiceCollectionExtensions

We found 10 examples in language CSharp for this search. You will see 34 fragments of code.

Other methods

        /// <summary>
        /// 加载此服务
        /// </summary>
        /// <param name="services"></param>
        public static IServiceCollection AddMemoryCache(this IServiceCollection services)
        {
            StartUp.Run();
            MemoryCacheServiceCollectionExtensions.AddMemoryCache(services);
            return services;
        }

// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Runtime.CompilerServices;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;

[assembly: TypeForwardedTo(typeof(MemoryCache))]
[assembly: TypeForwardedTo(typeof(MemoryCacheOptions))]
[assembly: TypeForwardedTo(typeof(MemoryCacheServiceCollectionExtensions))]
[assembly: TypeForwardedTo(typeof(MemoryDistributedCache))]
[assembly: TypeForwardedTo(typeof(MemoryDistributedCacheOptions))]


        public object Get<TValue>(string key)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            byte[] value;

            if (null != _memoryCache)
            {
                _memoryCache.TryGetValue(key, out value);
            }
            else
            {
                value = Get(key);
            }

            if (null != value)
            {
                return JsonConvert.DeserializeObject<TValue>(Encoding.UTF8.GetString(value));
            }

            return ObjectHelper.GetDefaultValue<TValue>();
        }

        public async Task<object> GetAsync<TValue>(string key, CancellationToken token = default)
        {
            return await Task.Run(() => Task.FromResult(Get<TValue>(key)), token);
        }

        public void Set<TValue>(string key, TValue value, DistributedCacheEntryOptions options)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            var valueAsByte = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(value, Formatting.Indented));

            if (null != _memoryCache)
            {
                var memoryCacheEntryOptions = new MemoryCacheEntryOptions
                {
                    AbsoluteExpiration = options?.AbsoluteExpiration,
                    AbsoluteExpirationRelativeToNow = options?.AbsoluteExpirationRelativeToNow,
                    SlidingExpiration = options?.SlidingExpiration,
                    Size = valueAsByte.Length
                };

                _memoryCache.Set(key, valueAsByte, memoryCacheEntryOptions);
            }
            else
            {
                base.Set(key, valueAsByte, options);
            }
        }

        public async Task SetAsync<TValue>(string key, TValue value, DistributedCacheEntryOptions options = default,
            CancellationToken token = default)
        {
            await Task.Run(() => Set(key, value, options), token);
        }

        public static IServiceCollection AddLazyCache(this IServiceCollection services)
        {
            if (services == null) throw new ArgumentNullException(nameof(services));

            services.AddOptions();
            services.TryAdd(ServiceDescriptor.Singleton<IMemoryCache, MemoryCache>());
            services.TryAdd(ServiceDescriptor.Singleton<ICacheProvider, MemoryCacheProvider>());

            services.TryAdd(ServiceDescriptor.Singleton<IAppCache, CachingService>(serviceProvider => 
                new CachingService(
                    new Lazy<ICacheProvider>(serviceProvider.GetRequiredService<ICacheProvider>))));

            return services;
        }

        public static IServiceCollection AddLazyCache(this IServiceCollection services,
            Func<IServiceProvider, CachingService> implementationFactory)
        {
            if (services == null) throw new ArgumentNullException(nameof(services));
            if (implementationFactory == null) throw new ArgumentNullException(nameof(implementationFactory));

            services.AddOptions();
            services.TryAdd(ServiceDescriptor.Singleton<IMemoryCache, MemoryCache>());
            services.TryAdd(ServiceDescriptor.Singleton<ICacheProvider, MemoryCacheProvider>());

            services.TryAdd(ServiceDescriptor.Singleton<IAppCache>(implementationFactory));

            return services;
        }

        /// <summary>
        /// Adds a non distributed in memory implementation of <see cref="IMemoryCache"/> to the
        /// <see cref="IServiceCollection" />.
        /// </summary>
        /// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
        /// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
        public static IServiceCollection AddMemoryCache<TKey, TValue>(this IServiceCollection services, ServiceLifetime lifetime = ServiceLifetime.Singleton) where TKey : notnull
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            services.AddOptions();
            services.TryAdd(new ServiceDescriptor(typeof(IMemoryCache<TKey, TValue>), typeof(MemoryCache<TKey, TValue>), lifetime));

            return services;
        }

        /// <summary>
        /// Adds a non distributed in memory implementation of <see cref="IMemoryCache"/> to the
        /// <see cref="IServiceCollection" />.
        /// </summary>
        /// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
        /// <param name="setupAction">
        /// The <see cref="Action{MemoryCacheOptions}"/> to configure the provided <see cref="MemoryCacheOptions{TKey, TValue}"/>.
        /// </param>
        /// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
        public static IServiceCollection AddMemoryCache<TKey, TValue>(this IServiceCollection services, Action<MemoryCacheOptions<TKey, TValue>> setupAction, ServiceLifetime lifetime = ServiceLifetime.Singleton) where TKey : notnull
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            if (setupAction == null)
            {
                throw new ArgumentNullException(nameof(setupAction));
            }

            services.AddMemoryCache<TKey, TValue>(lifetime);
            services.Configure(setupAction);

            return services;
        }

                    public System.Byte[] Get(string key) => throw null;
                    public System.Threading.Tasks.Task<System.Byte[]> GetAsync(string key, System.Threading.CancellationToken token = default(System.Threading.CancellationToken)) => throw null;
                    public System.Threading.Tasks.Task RefreshAsync(string key, System.Threading.CancellationToken token = default(System.Threading.CancellationToken)) => throw null;
                    public System.Threading.Tasks.Task RemoveAsync(string key, System.Threading.CancellationToken token = default(System.Threading.CancellationToken)) => throw null;
                    public void Set(string key, System.Byte[] value, Microsoft.Extensions.Caching.Distributed.DistributedCacheEntryOptions options) => throw null;
                    public System.Threading.Tasks.Task SetAsync(string key, System.Byte[] value, Microsoft.Extensions.Caching.Distributed.DistributedCacheEntryOptions options, System.Threading.CancellationToken token = default(System.Threading.CancellationToken)) => throw null;

                    public System.Byte[] Get(string key) => throw null;
                    public System.Threading.Tasks.Task<System.Byte[]> GetAsync(string key, System.Threading.CancellationToken token = default(System.Threading.CancellationToken)) => throw null;
                    public System.Threading.Tasks.Task RefreshAsync(string key, System.Threading.CancellationToken token = default(System.Threading.CancellationToken)) => throw null;
                    public System.Threading.Tasks.Task RemoveAsync(string key, System.Threading.CancellationToken token = default(System.Threading.CancellationToken)) => throw null;
                    public void Set(string key, System.Byte[] value, Microsoft.Extensions.Caching.Distributed.DistributedCacheEntryOptions options) => throw null;
                    public System.Threading.Tasks.Task SetAsync(string key, System.Byte[] value, Microsoft.Extensions.Caching.Distributed.DistributedCacheEntryOptions options, System.Threading.CancellationToken token = default(System.Threading.CancellationToken)) => throw null;

        public System.Threading.Tasks.Task<byte[]> GetAsync(string key, System.Threading.CancellationToken token = default(System.Threading.CancellationToken)) { throw null; }
        public System.Threading.Tasks.Task RefreshAsync(string key, System.Threading.CancellationToken token = default(System.Threading.CancellationToken)) { throw null; }
        public System.Threading.Tasks.Task RemoveAsync(string key, System.Threading.CancellationToken token = default(System.Threading.CancellationToken)) { throw null; }
        public void Set(string key, byte[] value, Microsoft.Extensions.Caching.Distributed.DistributedCacheEntryOptions options) { }
        public System.Threading.Tasks.Task SetAsync(string key, byte[] value, Microsoft.Extensions.Caching.Distributed.DistributedCacheEntryOptions options, System.Threading.CancellationToken token = default(System.Threading.CancellationToken)) { throw null; }
        public Microsoft.Extensions.Caching.Memory.ICacheEntry CreateEntry(object key) { throw null; }


        /// <summary>
        ///     Check public static IServiceCollection AddNetAppCommonDistributedMemoryCache is added as Singleton
        /// </summary>
        /// <returns>
        ///     True if IServiceCollection AddNetAppCommonDistributedMemoryCache is run, else false
        /// </returns>
        public static bool IsAdded()
        {
            return _isAdded;
        }

        /// <summary>
        ///     Adds a default implementation of <see cref="DistributedCache" /> that stores items in memory
        ///     to the <see cref="IServiceCollection" />. Frameworks that require a distributed cache to work
        ///     can safely add this dependency as part of their dependency list to ensure that there is at least
        ///     one implementation available.
        /// </summary>
        /// <remarks>
        ///     <see cref="AddDistributedMemoryCache(IServiceCollection)" /> should only be used in single
        ///     server scenarios as this cache stores items in memory and doesn't expand across multiple machines.
        ///     For those scenarios it is recommended to use a proper distributed cache that can expand across
        ///     multiple machines.
        /// </remarks>
        /// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
        /// <returns>The <see cref="IServiceCollection" /> so that additional calls can be chained.</returns>
        public static IServiceCollection AddNetAppCommonDistributedMemoryCache(this IServiceCollection services)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            services.AddOptions();
            //services.TryAdd(ServiceDescriptor.Singleton<IDistributedCache, MemoryDistributedCache>());
            services.TryAdd(ServiceDescriptor.Singleton<ICommonDistributedCache, CommonMemoryDistributedCache>());
            _isAdded = true;

            return services;
        }

        /// <summary>
        /// Adds a non distributed in memory implementation of <see cref="IMemoryCache"/> to the
        /// <see cref="IServiceCollection" />.
        /// </summary>
        /// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
        /// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
        public static IServiceCollection AddMemoryCache(this IServiceCollection services)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            services.AddOptions();
            services.TryAdd(ServiceDescriptor.Singleton<IMemoryCache, MemoryCache>());

            return services;
        }

        /// <summary>
        /// Adds a non distributed in memory implementation of <see cref="IMemoryCache"/> to the
        /// <see cref="IServiceCollection" />.
        /// </summary>
        /// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
        /// <param name="setupAction">
        /// The <see cref="Action{MemoryCacheOptions}"/> to configure the provided <see cref="MemoryCacheOptions"/>.
        /// </param>
        /// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
        public static IServiceCollection AddMemoryCache(this IServiceCollection services, Action<MemoryCacheOptions> setupAction)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            if (setupAction == null)
            {
                throw new ArgumentNullException(nameof(setupAction));
            }

            services.AddMemoryCache();
            services.Configure(setupAction);

            return services;
        }

        /// <summary>
        /// Adds a default implementation of <see cref="IDistributedCache"/> that stores items in memory
        /// to the <see cref="IServiceCollection" />. Frameworks that require a distributed cache to work
        /// can safely add this dependency as part of their dependency list to ensure that there is at least
        /// one implementation available.
        /// </summary>
        /// <remarks>
        /// <see cref="AddDistributedMemoryCache(IServiceCollection)"/> should only be used in single
        /// server scenarios as this cache stores items in memory and doesn't expand across multiple machines.
        /// For those scenarios it is recommended to use a proper distributed cache that can expand across
        /// multiple machines.
        /// </remarks>
        /// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
        /// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
        public static IServiceCollection AddDistributedMemoryCache(this IServiceCollection services)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            services.AddOptions();
            services.TryAdd(ServiceDescriptor.Singleton<IDistributedCache, MemoryDistributedCache>());

            return services;
        }

        /// <summary>
        /// Adds a default implementation of <see cref="IDistributedCache"/> that stores items in memory
        /// to the <see cref="IServiceCollection" />. Frameworks that require a distributed cache to work
        /// can safely add this dependency as part of their dependency list to ensure that there is at least
        /// one implementation available.
        /// </summary>
        /// <remarks>
        /// <see cref="AddDistributedMemoryCache(IServiceCollection)"/> should only be used in single
        /// server scenarios as this cache stores items in memory and doesn't expand across multiple machines.
        /// For those scenarios it is recommended to use a proper distributed cache that can expand across
        /// multiple machines.
        /// </remarks>
        /// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
        /// <param name="setupAction">
        /// The <see cref="Action{MemoryDistributedCacheOptions}"/> to configure the provided <see cref="MemoryDistributedCacheOptions"/>.
        /// </param>
        /// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
        public static IServiceCollection AddDistributedMemoryCache(this IServiceCollection services, Action<MemoryDistributedCacheOptions> setupAction)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            if (setupAction == null)
            {
                throw new ArgumentNullException(nameof(setupAction));
            }

            services.AddDistributedMemoryCache();
            services.Configure(setupAction);

            return services;
        }

Microsoft.Extensions.DependencyInjection.MemoryCacheServiceCollectionExtensions : Object

Methods :

public static IServiceCollection AddMemoryCache(IServiceCollection services = )
public static IServiceCollection AddMemoryCache(IServiceCollection services = , Action<MemoryCacheOptions> setupAction = )
public static IServiceCollection AddDistributedMemoryCache(IServiceCollection services = )
public Type GetType()
public String ToString()
public Boolean Equals(Object obj = )
public Int32 GetHashCode()

Other methods