public static INetCacheBuilder AddCacheType <T>(this INetCacheBuilder builder,
                                                        Func <IServiceProvider, object[]>?getParameters = null,
                                                        ServiceLifetime lifetime = ServiceLifetime.Scoped) where T : class
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            var proxy = typeof(T).GetNestedType("@Proxy");

            if (proxy != null && typeof(T).IsAssignableFrom(proxy))
            {
                builder.Services.Add(getParameters == null
                    ? new ServiceDescriptor(typeof(T), proxy, lifetime)
                    : new ServiceDescriptor(typeof(T), provider =>
                                            ActivatorUtilities.CreateInstance(provider, proxy, getParameters(provider)), lifetime));
            }
            else
            {
                builder.Services.Add(getParameters == null && builder is NetCacheBuilder ncb
                    ? new ServiceDescriptor(typeof(T), ncb.Generator.CreateProxyType <T>(), lifetime)
                    : new ServiceDescriptor(typeof(T), provider =>
                                            ActivatorUtilities.CreateInstance(provider,
                                                                              provider.GetRequiredService <ICacheProxyGenerator>().CreateProxyType <T>(),
#if NET45
                                                                              getParameters == null ? new object[0] : getParameters(provider)),
#else
                                                                              getParameters == null ? Array.Empty <object>() : getParameters(provider)),
#endif
                                            lifetime));
            }

            return(builder);
        }
예제 #2
0
        public static INetCacheBuilder UseDistributedLockFactory <T>(this INetCacheBuilder builder) where T : class, IDistributedLockFactory
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            builder.Services.Replace(ServiceDescriptor.Scoped <IDistributedLockFactory, T>());

            return(builder);
        }
예제 #3
0
        public static INetCacheBuilder UseNewtonsoftJsonSerializer(this INetCacheBuilder builder, JsonSerializerSettings?settings = null)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            builder.Services.Replace(new ServiceDescriptor(typeof(IValueSerializer), new NewtonsoftJsonSerializer(settings)));

            return(builder);
        }
예제 #4
0
        public static INetCacheBuilder UseKeyFormatter <T>(this INetCacheBuilder builder) where T : class, IKeyFormatter
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            builder.Services.Replace(ServiceDescriptor.Scoped <IKeyFormatter, T>());

            return(builder);
        }
예제 #5
0
        public static INetCacheBuilder UseSystemTextJsonSerializer(this INetCacheBuilder builder, JsonSerializerOptions?options = null)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            builder.Services.Replace(new ServiceDescriptor(typeof(IValueSerializer), new SystemTextJsonSerializer(options)));

            return(builder);
        }
예제 #6
0
        public static INetCacheBuilder UseMessagePack(this INetCacheBuilder builder, MessagePackSerializerOptions?options = null)
#endif
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }
#if NETFRAMEWORK
            builder.Services.Replace(new ServiceDescriptor(typeof(IValueSerializer), new MessagePackSerializer()));
#else
            builder.Services.Replace(new ServiceDescriptor(typeof(IValueSerializer), new MessagePackSerializer(options)));
#endif
            return(builder);
        }
예제 #7
0
 public static INetCacheBuilder UseMessagePack(this INetCacheBuilder builder)
 public static INetCacheBuilder UseRuntimeCaching(this INetCacheBuilder builder) =>
 builder.UseCacheProviderFactory <RuntimeCachingProviderFactory>()
 .UseDistributedLockFactory <LocalLockFactory>();
 public static INetCacheBuilder UseProtobufNetSerializer(this INetCacheBuilder builder) =>
 builder.UseValueSerializer <ProtobufNetSerializer>();
 public static INetCacheBuilder UseStackExchangeRedis(this INetCacheBuilder builder) =>
 builder.UseCacheProviderFactory <StackExchangeRedisProviderFactory>()
 .UseDistributedLockFactory <StackExchangeRedisLockFactory>();
 public static INetCacheBuilder UseMemoryCache(this INetCacheBuilder builder) =>
 builder.UseCacheProviderFactory <MemoryCacheProviderFactory>()
 .UseDistributedLockFactory <LocalLockFactory>();