Why is my static object created multiple times in my asp.net code?

I think a static object is shared between multiple threads. Nevertheless, I got a high problem with the processor on one of my sites, so I took the windbg dump and was very surprised, I see the following:

enter image description here

We see that there are 10 instances of the ConnectionMultiplexer class. But my code creates ConnectionMultiplexer as a static object. This should mean that only one instance is created for all threads. So how does windbg show multiple instances?

This is my code to create a redis connection

public static class CacheConnection
    {
        private static StackExchangeRedisCacheClient _newconnectionDb;

        public static StackExchangeRedisCacheClient NewConnectionDb
            => _newconnectionDb ?? (_newconnectionDb = NewRedisConnection());

        private static IDatabase _connectionDb;

        public static IDatabase ConnectionDb => _connectionDb ?? (_connectionDb = RedisConnection());

        private static StackExchangeRedisCacheClient NewRedisConnection()
        {
            var serializer = new NewtonsoftSerializer();
            return new StackExchangeRedisCacheClient(Connection, serializer);
        }

        private static IDatabase RedisConnection()
        {
            var cacheDatabase = Connection.GetDatabase();
            return cacheDatabase;
        }

        public static ConnectionMultiplexer Connection => LazyConnection.Value;

        private static readonly Lazy<ConnectionMultiplexer> LazyConnection = new Lazy<ConnectionMultiplexer>(() => ConnectionMultiplexer.Connect(
            System.Configuration.ConfigurationManager.AppSettings["CacheConnectionString"]), LazyThreadSafetyMode.PublicationOnly);
    }
+4
source share
1 answer

ConnectionMultiplexer readonly (get) # 7 = > , LazyConnection.Value , .

LazyThreadSafetyMode.PublicationOnly, MSDN (https://msdn.microsoft.com/en-us/library/system.threading.lazythreadsafetymode(v=vs.110).aspx)

Lazy , ( , ). Lazy. , , . T, , . thread Lazy.Value . . IsValueCreated , , , , , . Value Lazy, .

, , , , ( ).

LazyThreadSafetyMode.ExecutionAndPublication .

, Lazy, singleton, # In Depth

http://csharpindepth.com/Articles/General/Singleton.aspx

+5

Source: https://habr.com/ru/post/1686555/


All Articles