站长资讯网
最全最丰富的资讯网站

Golang的memcache如何简单实现

下面由golang教程栏目给大家介绍Golang简单的memcache实现方法,希望对需要的朋友有所帮助!

Golang的memcache如何简单实现

这两天在做项目的过程中遇到了一个访问全局变量的问题场景:编写一个方法,获取id对应的token值,token需要缓存起来(全局变量内存缓存),如果获取不到或者token的时间过期,那么发送http请求到其他端去取,然后缓存起来,然后再返回,那么代码如下:

code.go:

package person  import ( 	"time" )  var gAccId2Token map[int]interface{} = make(map[int]interface{})  func GetTokenByAccountId(accountId uint, acl string) (map[string]interface{}, error) { 	//get token from cache 	if token, ok := gAccId2Token[accountId]; ok { 		if token != nil { 			now := time.Now().Unix() 			if int(now) < int(token.(map[string]interface{})["expireDate"].(float64)) { 				return token.(map[string]interface{}), nil 			} 		} 	}  	token, err := getTokenByHttpUrl(apiUrl) 	if err != nil { 		return map[string]interface{}{}, err 	}  	gAccId2Token[accountId] = token  	return token.(map[string]interface{}), nil }

那么问题来了:

1.由于gAccId2Token变量是全局变量,那么会出现同时读写的情况,则会可能出现读写不一致的情况。

2.就本例来看,获取id=2的token,而缓存的token已经过期了,那么就会发送http请求去获取,之后写缓存,假设写缓存的时间很长,而在这段时间内,又恰好有大量请求来获取id=2的token,由于token都过期了,就会出现大量请求http服务端的问题,不仅没有起到获取缓存的目的,又增大了后端的压力,同时又有多个写缓存的操作,而golang的map应该不是原子的,那么大量写内存也可能会造成crash的问题。

因此,我们需要对读写操作进行加锁:

memcache.go:

package person  import ( 	"sync" 	"time" )  type memoryCache struct { 	lock  *sync.RWMutex 	items map[interface{}]interface{} }  func (mc *memoryCache) set(key interface{}, value interface{}) error { 	mc.lock.Lock() 	defer mc.lock.Unlock() 	mc.items[key] = value 	return nil }  func (mc *memoryCache) get(key interface{}) interface{} { 	mc.lock.RLock() 	defer mc.lock.RUnlock()  	if val, ok := mc.items[key]; ok { 		return val 	} 	return nil }  var gAccId2Token *memoryCache = &memoryCache{ 		lock:  new(sync.RWMutex), 		items: make(map[interface{}]interface{}), 	}  func GetTokenByAccountId(accountId uint, acl string) (map[string]interface{}, error) { 	//get token from cache 	token := gAccId2Token.get(accountId) 	if token != nil { 		now := time.Now().Unix() 		if int(now) < int(token.(map[string]interface{})["expireDate"].(float64)) { 			return token.(map[string]interface{}), nil 		} 	}  	token, err := getTokenByHttpUrl(apiUrl) 	if err != nil { 		return map[string]interface{}{}, err 	}  	gAccId2Token.set(accountId, token)  	return token.(map[string]interface{}), nil }

几点说明:

1.为写操作上了全局锁,一旦Lock()之后,其他lock便不能上锁,直到释放锁Unlock()之后才行,也就是说保证写操作的原子性。

2.而为读操作上了读锁,那么可以有多个线程Rlock()对一个区域枷锁,从而保证区域是可读的,直到所有读锁都RUnlock()之后,才可以上写锁。

3.将map的key和value的类型都定义成为interface{}类型,interface{}可以接收任何类型,就像是Java中的Object。

4.interface{}类型转换的方法,value.(type),即将value转换成为type类型,例如:value.(int),value.(map[string]interface{})等等。

赞(0)
分享到: 更多 (0)