.Net Core of ObjectPool

First, the object pool  

  Using object pooling technique can significantly improve performance, especially when the cost of the initialization process of the higher frequency or larger objects. Here are several classes ObjectPool source involved. When you've seen a lot of .Net Core source code, you will find that many of them are Microsoft's development of this model, constructed by the Provider Policy, create a final class by Provider.

Second, the use

  The main purpose of this component is to save objects to the object pool, use the time to pick up directly, do not need to re-create, implement reuse objects. But there is a problem, if the number is beginning to target objects in the pool or get larger than the number of objects in the pool how to do? When an insufficient number of objects in the pool of the object, this time to pick up an object, the object needs to be created by the Policy, as to how to create an object, you need to implement. Policy above, there is a default implementation   DefaultPooledObjectPolicy  , its creation is the new T ().

DefaultObjectPool<User> defaultPool = new DefaultObjectPool<User>(new DefaultPooledObjectPolicy<User>(), 2);

            var firstUser defaultPool.Get = (); // get an object from the pool, the pool at this time are not subject object, returns Age = 0 Name = null

            var user1 = new User()
            {
                Age = 18,
                Name = "MicroHeart"
            };

            var user2 = new User()
            {
                Age = 19,
                Name = "MicroHeart"
            };

            var user3 = new User()
            {
                Age = 20,
                Name = "MicroHeart"
            };

            defaultPool.Return (user1); // object objects back into the pool, but not necessarily successful. Pooling quantitative restrictions, but also can customize the restrictions, so that some objects can not be placed in the object pool
            defaultPool.Return(user2);
            defaultPool.Return(user3);

            var U1 = defaultPool.Get (); // get an object from the pool, to return into the first user1 
            var U2 = defaultPool.Get (); // get an object from the pool, the second return loaded user2 
            var U3 = defaultPool.Get (); // this time the pool is not the object of the object, obtaining an object Policy Create method call, and to realize the DefaultPooledObjectPolicy Create method is: new T (), the flow returns Age = 0 Name = null

Examples of the above objects in the pool when no object is acquired retrieval method, in fact, new T (). Add the following three objects in the object, but the object pool only two spaces, the third one is not added into the.

 

Now create your own one Policy

    public  class MyUserPolicy: PooledObjectPolicy <the User> // should extend the abstract class
    {
        public override User Create()
        {
            return new User()
            {
                Age = 18,
                Name = "MicroHeart"
            };
        }

        public override bool Return(User user)
        {
            if (user.Age == 18)
                return false;
            return true;
        }
    }
            DefaultObjectPool<User> defaultPool = new DefaultObjectPool<User>(new MyUserPolicy(), 2);

            var firstUser defaultPool.Get = (); // returns Age = 0 Name = null

            var user1 = new User()
            {
                Age = 18,
                Name = "MicroHeart"
            };

            var user2 = new User()
            {
                Age = 19,
                Name = "MicroHeart"
            };

            var user3 = new User()
            {
                Age = 20,
                Name = "MicroHeart"
            };

            defaultPool.Return(user1);
            defaultPool.Return(user2);
            defaultPool.Return(user3);

            var U1 = defaultPool.Get (); // returns because user1, user2 Age = 18, policy filters in Reture returns false, a user can not lead into the first connection pool. 
            var U2 = defaultPool.Get (); // Returns USER3 
            var U3 = defaultPool.Get (); // Returns Age = 18 Name = MicroHeart This was created using the Create Policy

Third, resolve source

 IPooledObjectPolicy <T>:  main role is to create objects and objects into the connection pool

  Create: to create a definition method of an object, when the number taken enough connection pool object is created by this method.

  Return: The objects in the connection pool, if successfully placed, Ture otherwise return False..
  DefaultPooledObjectPolicy <T>: inherited abstract class PooledObjectPolicy <T>, while pumping abstract class that inherits the interface IPooledObjectPolicy <T>. The default Policy implementation class

    Create: Define a default method to create an object of new T ();

    Return: whether or not the connection into the pool, all return True.

ObjectPoolProvider: create an object pool (ObjectPool)

  abstract ObjectPool<T> Create<T>(IPooledObjectPolicy<T> policy):通过Policy创建ObjectPool

  DefaultObjectPoolProvider: The default implementation class ObjectPoolProvider

    ObjectPool <T> Create <T> (IPooledObjectPolicy <T> policy): X2 create a number of processors that can accommodate objects pool.

ObjectPool <T>: Pooling

  T Get (): Gets an object from the pool.

  void Return (T obj): placing the objects in the object pool.

  DefaultObjectPool: default object pool implementation class that implements the Get and Return method. In fact, it is the nature of the object pool in  ObjectWrapper [] _items;  , Return is used when the object is placed into ObjectWrapper array. Methods Get from the array.

 

Guess you like

Origin www.cnblogs.com/MicroHeart/p/11701647.html