feat: make sure of cache thread-safety
This commit is contained in:
		| @@ -1,6 +1,8 @@ | |||||||
| package debian | package debian | ||||||
|  |  | ||||||
| import ( | import ( | ||||||
|  | 	"sync" | ||||||
|  |  | ||||||
| 	"github.com/rapidloop/skv" | 	"github.com/rapidloop/skv" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| @@ -8,7 +10,12 @@ type Cache struct { | |||||||
| 	store *skv.KVStore | 	store *skv.KVStore | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // cache is not thread-safe, so make sure there are only one user | ||||||
|  | var mu sync.Mutex | ||||||
|  |  | ||||||
| func NewCache(path string) (c *Cache, err error) { | func NewCache(path string) (c *Cache, err error) { | ||||||
|  | 	mu.Lock() | ||||||
|  |  | ||||||
| 	c = &Cache{} | 	c = &Cache{} | ||||||
| 	c.store, err = skv.Open(path) | 	c.store, err = skv.Open(path) | ||||||
| 	return | 	return | ||||||
| @@ -32,6 +39,8 @@ func (c Cache) GetVersions() (versions []string, err error) { | |||||||
| 	return | 	return | ||||||
| } | } | ||||||
|  |  | ||||||
| func (c Cache) Close() error { | func (c Cache) Close() (err error) { | ||||||
| 	return c.store.Close() | 	err = c.store.Close() | ||||||
|  | 	mu.Unlock() | ||||||
|  | 	return | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user