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

Simplies logic to just forward to other methods, makes stats tracking consistent. #46

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
13 changes: 4 additions & 9 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const (
// segmentCount represents the number of segments within a freecache instance.
segmentCount = 256
// segmentAndOpVal is bitwise AND applied to the hashVal to find the segment id.
segmentAndOpVal = 255
segmentAndOpVal = segmentCount - 1
minBufSize = 512 * 1024
)

Expand Down Expand Up @@ -57,11 +57,7 @@ func (cache *Cache) Set(key, value []byte, expireSeconds int) (err error) {

// Get returns the value or not found error.
func (cache *Cache) Get(key []byte) (value []byte, err error) {
hashVal := hashFunc(key)
segID := hashVal & segmentAndOpVal
cache.locks[segID].Lock()
value, _, err = cache.segments[segID].get(key, hashVal)
cache.locks[segID].Unlock()
value, _, err = cache.GetWithExpiration(key)
return
}

Expand Down Expand Up @@ -104,9 +100,8 @@ func (cache *Cache) SetInt(key int64, value []byte, expireSeconds int) (err erro

// GetInt returns the value for an integer within the cache or a not found error.
func (cache *Cache) GetInt(key int64) (value []byte, err error) {
var bKey [8]byte
binary.LittleEndian.PutUint64(bKey[:], uint64(key))
return cache.Get(bKey[:])
value, _, err = cache.GetIntWithExpiration(key)
return
}

// GetIntWithExpiration returns the value and expiration or a not found error.
Expand Down
16 changes: 7 additions & 9 deletions segment.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,11 +369,12 @@ func (seg *segment) lookupByOff(slot []entryPtr, hash16 uint16, offset int64) (i
}

func (seg *segment) resetStatistics() {
atomic.StoreInt64(&seg.totalEvacuate, 0)
atomic.StoreInt64(&seg.totalExpired, 0)
atomic.StoreInt64(&seg.overwrites, 0)
atomic.StoreInt64(&seg.entryCount, 0)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not stats.

atomic.StoreInt64(&seg.hitCount, 0)
atomic.StoreInt64(&seg.missCount, 0)
atomic.StoreInt64(&seg.overwrites, 0)
atomic.StoreInt64(&seg.totalEvacuate, 0)
atomic.StoreInt64(&seg.totalExpired, 0)
}

func (seg *segment) clear() {
Expand All @@ -386,12 +387,9 @@ func (seg *segment) clear() {
seg.slotLens[i] = 0
}

atomic.StoreInt64(&seg.hitCount, 0)
atomic.StoreInt64(&seg.missCount, 0)
atomic.StoreInt64(&seg.entryCount, 0)
// These fields are not stats.
atomic.StoreInt64(&seg.totalCount, 0)
atomic.StoreInt64(&seg.totalTime, 0)
atomic.StoreInt64(&seg.totalEvacuate, 0)
atomic.StoreInt64(&seg.totalExpired, 0)
atomic.StoreInt64(&seg.overwrites, 0)

seg.resetStatistics()
}