How to create a Singleton Cache in Go

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

How to create a Singleton Cache in Go

问题

我正在使用Golang进行工作,这是我不熟悉的领域,我找到了两篇有趣的文章:

https://hackernoon.com/in-memory-caching-in-golang

这篇来自hackernoon的文章非常好,其中的第一个例子(Simple Map)正是我想要创建缓存的方式,因为它提供了一个在缓存中设置过期值的示例。我在理解这篇文章时遇到了困难,它没有说明这个实现是创建了一个缓存的实例还是多个副本,如果是多个副本的话,可能会发生冲突,即一个副本中有一个值,另一个副本中有另一个值,这样查找操作就无法正常工作。

在另一个链接https://thedevelopercafe.com/articles/singleton-in-golang-839d8610958b中,它讨论了创建一个缓存的实例。

所以,我的问题是,这两篇文章中都使用了sync,所以我可以请教一些在Golang方面有经验的人,确认一下Hackernoon中的newlocalcache函数是否设置了一个单例,如果没有,我需要做什么来添加它?

英文:

I am working in Golang, which I am new to, and I have come across two interesting articles:

https://hackernoon.com/in-memory-caching-in-golang

The one from hackernoon is really good and the first example (Simple Map) is precisely what I am for to create a cache as it gives an example for expiring values in a cache. Where I am struggling to understand, is that it does not say whether the implementation creates just one instantation of the cache and not multiple copies, which would conflict or you have one value in one copy and one in another, and the look ups won't work properly.

In another link https://thedevelopercafe.com/articles/singleton-in-golang-839d8610958b it talks about instantation of one cache.

So, my question in both they use sync and so can I ask someone who has experience in Golang to advise me whether the example from Hackernoon in the function called newlocalcache sets up a singleton and if not what do I need to do to add it?

答案1

得分: 1

函数newlocalcache设置了一个单例。

不,它每次被调用时都会构建并返回一个新的本地缓存。

如果不是这样,我需要做什么来添加它?

只需调用它一次即可。例如:

var localCacheSingleton *localCache

var newLocalCacheOnce sync.Once

func newLocalCache(cleanupInterval time.Duration) *localCache {
    newLocalCacheOnce.Do(func() {
		lc := &localCache{
			users: make(map[int64]cachedUser),
			stop:  make(chan struct{}),
		}

		lc.wg.Add(1)
		go func(cleanupInterval time.Duration) {
			defer lc.wg.Done()
			lc.cleanupLoop(cleanupInterval)
		}(cleanupInterval)

		localCacheSingleton = lc
    })
    return localCacheSingleton
}
英文:

> the function called newlocalcache sets up a singleton

No, it constructs and returns a new local cache every time it's called.

> if not what do I need to do to add it?

Call it just once. For example:

var localCacheSingleton *localCache

var newLocalCacheOnce sync.Once

func newLocalCache(cleanupInterval time.Duration) *localCache {
    newLocalCacheOnce.Do(func() {
		lc := &localCache{
			users: make(map[int64]cachedUser),
			stop:  make(chan struct{}),
		}

		lc.wg.Add(1)
		go func(cleanupInterval time.Duration) {
			defer lc.wg.Done()
			lc.cleanupLoop(cleanupInterval)
		}(cleanupInterval)

		localCacheSingleton = lc
    })
    return localCacheSingleton
}

huangapple
  • 本文由 发表于 2022年10月29日 00:11:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/74238098.html
匿名

发表评论

匿名网友

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

确定