github.com/gogf/gf/v2@v2.7.4/internal/instance/instance.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  // Package instance provides instances management.
     8  //
     9  // Note that this package is not used for cache, as it has no cache expiration.
    10  package instance
    11  
    12  import (
    13  	"github.com/gogf/gf/v2/container/gmap"
    14  	"github.com/gogf/gf/v2/encoding/ghash"
    15  )
    16  
    17  const (
    18  	groupNumber = 64
    19  )
    20  
    21  var (
    22  	groups = make([]*gmap.StrAnyMap, groupNumber)
    23  )
    24  
    25  func init() {
    26  	for i := 0; i < groupNumber; i++ {
    27  		groups[i] = gmap.NewStrAnyMap(true)
    28  	}
    29  }
    30  
    31  func getGroup(key string) *gmap.StrAnyMap {
    32  	return groups[int(ghash.DJB([]byte(key))%groupNumber)]
    33  }
    34  
    35  // Get returns the instance by given name.
    36  func Get(name string) interface{} {
    37  	return getGroup(name).Get(name)
    38  }
    39  
    40  // Set sets an instance to the instance manager with given name.
    41  func Set(name string, instance interface{}) {
    42  	getGroup(name).Set(name, instance)
    43  }
    44  
    45  // GetOrSet returns the instance by name,
    46  // or set instance to the instance manager if it does not exist and returns this instance.
    47  func GetOrSet(name string, instance interface{}) interface{} {
    48  	return getGroup(name).GetOrSet(name, instance)
    49  }
    50  
    51  // GetOrSetFunc returns the instance by name,
    52  // or sets instance with returned value of callback function `f` if it does not exist
    53  // and then returns this instance.
    54  func GetOrSetFunc(name string, f func() interface{}) interface{} {
    55  	return getGroup(name).GetOrSetFunc(name, f)
    56  }
    57  
    58  // GetOrSetFuncLock returns the instance by name,
    59  // or sets instance with returned value of callback function `f` if it does not exist
    60  // and then returns this instance.
    61  //
    62  // GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f`
    63  // with mutex.Lock of the hash map.
    64  func GetOrSetFuncLock(name string, f func() interface{}) interface{} {
    65  	return getGroup(name).GetOrSetFuncLock(name, f)
    66  }
    67  
    68  // SetIfNotExist sets `instance` to the map if the `name` does not exist, then returns true.
    69  // It returns false if `name` exists, and `instance` would be ignored.
    70  func SetIfNotExist(name string, instance interface{}) bool {
    71  	return getGroup(name).SetIfNotExist(name, instance)
    72  }
    73  
    74  // Clear deletes all instances stored.
    75  func Clear() {
    76  	for i := 0; i < groupNumber; i++ {
    77  		groups[i].Clear()
    78  	}
    79  }