Skip to content

Allow the caller to lock access #86

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ type baseCache struct {
deserializeFunc DeserializeFunc
serializeFunc SerializeFunc
expiration *time.Duration
mu sync.RWMutex
mu Locker
loadGroup Group
*stats
}
Expand All @@ -73,6 +73,19 @@ type (
SerializeFunc func(interface{}, interface{}) (interface{}, error)
)

type Locker interface {
RLock()
RUnlock()
sync.Locker
}

type noopLocker struct{}

func (noopLocker) RLock() {}
func (noopLocker) RUnlock() {}
func (noopLocker) Lock() {}
func (noopLocker) Unlock() {}

type CacheBuilder struct {
clock Clock
tp string
Expand All @@ -84,16 +97,23 @@ type CacheBuilder struct {
expiration *time.Duration
deserializeFunc DeserializeFunc
serializeFunc SerializeFunc
locker Locker
}

func New(size int) *CacheBuilder {
return &CacheBuilder{
clock: NewRealClock(),
tp: TYPE_SIMPLE,
size: size,
clock: NewRealClock(),
tp: TYPE_SIMPLE,
size: size,
locker: &sync.RWMutex{},
}
}

func (cb *CacheBuilder) NoLock() *CacheBuilder {
cb.locker = noopLocker{}
return cb
}

func (cb *CacheBuilder) Clock(clock Clock) *CacheBuilder {
cb.clock = clock
return cb
Expand Down Expand Up @@ -202,6 +222,7 @@ func buildCache(c *baseCache, cb *CacheBuilder) {
c.evictedFunc = cb.evictedFunc
c.purgeVisitorFunc = cb.purgeVisitorFunc
c.stats = &stats{}
c.mu = cb.locker
}

// load a new value using by specified key.
Expand Down