在beego中使用golang单例模式。

huangapple go评论88阅读模式
英文:

golang singletons in beego

问题

我正在尝试使用Golang和Beego。我来自Java/Spring的背景,我在实现一个Web应用程序时遇到了一些困难。我有一个控制器(我希望它是一个单例),控制器中还有一个服务(我也希望它是一个单例)。我以为如果我将我的服务定义为指针,那么我将始终使用相同的地址(一个单例)。但事实证明并非如此。

我的路由看起来像这样:

beego.Router("/", &controllers.SessionController{}, "get:Login")

我的SessionController看起来像这样:

type SessionController struct {
baseController
userService *services.UserService
}

func (this *SessionController) Prepare() {
this.baseController.Prepare()

if this.userService == nil {
    beego.Info("user service was nil")
    this.userService = factories.NewUserService()
}

}

我的日志始终显示每个请求时用户服务为nil。我该如何获得一个控制器的单个实例,并且该实例只初始化一次我的用户服务?

英文:

I'm trying out both Golang and Beego. I come from a Java/Spring background and I'm having a slightly difficult time implementing a singleton pattern for a webapp I'm developing. I have a controller (which I want to be a singleton) and I have a service within my controller (which I also want to be a singleton). I thought if I made my service a pointer, then I'd always use the same address (a singleton). This is not proving true.

My route looks like this<br/>

beego.Router(&quot;/&quot;, &amp;controllers.SessionController{}, &quot;get:Login&quot;)

My SessionController looks like this <br/>

type SessionController struct {
    baseController
    userService *services.UserService
}

func (this *SessionController) Prepare() {
    this.baseController.Prepare()

    if this.userService == nil {
	    beego.Info(&quot;user service was nil&quot;)
	    this.userService = factories.NewUserService()
    }
}

My logs always show that the user service is nil upon each request. How can I get a single instance of my controller with a single (initialized only once) instance of my user service?

答案1

得分: 2

将用户服务设置为单例:

var globalUserService = factories.NewUserService()

type SessionController struct {
    baseController
    userService *services.UserService
}

func (this *SessionController) Prepare() {
    this.baseController.Prepare()

    if this.userService == nil {
        beego.Info("用户服务为空")
        this.userService = globalUserService
    }
}

注意:以上代码是Go语言代码,用于将用户服务设置为单例。在SessionControllerPrepare方法中,首先检查userService是否为空,如果为空,则将其设置为全局的globalUserService

英文:

Make the user Service a singleton:

var globalUserService = factories.NewUserService()

type SessionController struct {
    baseController
    userService *services.UserService
}

func (this *SessionController) Prepare() {
    this.baseController.Prepare()

    if this.userService == nil {
        beego.Info(&quot;user service was nil&quot;)            
        this.userService = globalUserService
    }
}

答案2

得分: 2

Beego框架的其中一位创建者告诉我,该框架每个请求都会创建一个新的控制器实例,所以不太可能创建一个单例 在beego中使用golang单例模式。

英文:

1 of the creators of the Beego framework has mentioned to me that the framework creates a new instance of a controller per request, so it's not really possible to create a singleton 在beego中使用golang单例模式。

答案3

得分: 0

以下是关于在Golang中创建单例模式的一切你需要知道的内容:

在Go中的单例模式

var instance *singleton

func GetInstance() *singleton {
    if instance == nil {
        instance = &singleton{}   // &lt;--- NOT THREAD SAFE
    }
    return instance
}

但这不是线程安全的,你需要使用Sync包中的Once函数。
示例:-

import (
    "sync"
) 

type singleton struct {
}

var instance *singleton
var once sync.Once

func GetInstance() *singleton {
    once.Do(func() {
        instance = &singleton{}
    })
    return instance
}
英文:

Here is everything you need to know about creating singleton in Golang

Singleton Pattern in Go

var instance *singleton

func GetInstance() *singleton {
    if instance == nil {
	    instance = &amp;singleton{}   // &lt;--- NOT THREAD SAFE
    }
    return instance
}

But that is not THREAD SAFE , you will need to use the Sync once
Example :-

import (
    &quot;sync&quot;
) 

type singleton struct {
}

var instance *singleton
var once sync.Once

func GetInstance() *singleton {
    once.Do(func() {
        instance = &amp;singleton{}
    })
    return instance
}

huangapple
  • 本文由 发表于 2014年6月1日 23:19:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/23981330.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定