Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

if registered dataload, Value() not thread safe #35

Open
winxxp opened this issue Jul 22, 2019 · 2 comments
Open

if registered dataload, Value() not thread safe #35

winxxp opened this issue Jul 22, 2019 · 2 comments

Comments

@winxxp
Copy link
Contributor

winxxp commented Jul 22, 2019

// Value returns an item from the cache and marks it to be kept alive. You can
// pass additional arguments to your DataLoader callback function.
func (table *CacheTable) Value(key interface{}, args ...interface{}) (*CacheItem, error) {
	table.RLock()
	r, ok := table.items[key]
	loadData := table.loadData
	table.RUnlock()

	if ok {
		// Update access counter and timestamp.
		r.KeepAlive()
		return r, nil
	}

	// Item doesn't exist in cache. Try and fetch it with a data-loader.
	if loadData != nil {
		item := loadData(key, args...)
		if item != nil {
			table.Add(key, item.lifeSpan, item.data)
			return item, nil
		}

		return nil, ErrKeyNotFoundOrLoadable
	}

	return nil, ErrKeyNotFound
}
@winxxp winxxp changed the title dataload not thread safe if register dataload, Value() not thread safe Jul 22, 2019
@winxxp winxxp changed the title if register dataload, Value() not thread safe if registered dataload, Value() not thread safe Jul 22, 2019
@muesli
Copy link
Owner

muesli commented Jul 22, 2019

Can you elaborate a bit more?

@winxxp
Copy link
Contributor Author

winxxp commented Jul 23, 2019

if value is file handle, key is filename, so kV is filename -> file handle.
eg. thread 1,2,... call Value(filename) , and filename is not in cache, so all thread will call loadData to open same filename.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants