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

Add ShouldUpdateDeep #14

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
47 changes: 47 additions & 0 deletions core.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,34 @@ func (mn *MnemosyneInstance) get(ctx context.Context, key string) (*cachableRet,
return nil, &ErrCacheMiss{message: "Miss"} // FIXME: better Error combination
}

// get from all layers and replace older data with new one
func (mn *MnemosyneInstance) getAndSyncLayers(ctx context.Context, key string) (*cachableRet, error) {
cacheResults := make([]*cachableRet, len(mn.cacheLayers))
var result *cachableRet
var resultLayer int
for i, layer := range mn.cacheLayers {
cacheResults[i], _ = layer.withContext(ctx).get(key)
if cacheResults[i] != nil &&
(result == nil ||
cacheResults[i].Time.After(result.Time)) {
result = cacheResults[i]
resultLayer = i
}
}
if result == nil {
go mn.cacheWatcher.Inc(mn.name, "miss")
return nil, &ErrCacheMiss{message: "Miss"} // FIXME: better Error combination
}
for i, layer := range mn.cacheLayers {
if cacheResults[i] == nil || cacheResults[i].Time.Before(result.Time) {
go layer.set(key, *result)
}
}

go mn.cacheWatcher.Inc(mn.name, fmt.Sprintf("layer%d", resultLayer))
return result, nil
}

// Get retrieves the value for key
func (mn *MnemosyneInstance) Get(ctx context.Context, key string, ref interface{}) error {
cachableObj, err := mn.get(ctx, key)
Expand Down Expand Up @@ -193,6 +221,25 @@ func (mn *MnemosyneInstance) ShouldUpdate(ctx context.Context, key string) (bool
return shouldUpdate, nil
}

// ShouldUpdateDeep checks all layers for newer result and will sync older cache layers
func (mn *MnemosyneInstance) ShouldUpdateDeep(ctx context.Context, key string) (bool, error) {
cachableObj, err := mn.getAndSyncLayers(ctx, key)
if err == redis.Nil {
return true, err
} else if err != nil {
return false, err
}

if cachableObj == nil || cachableObj.CachedObject == nil {
logrus.Errorf("nil object found in cache %s ! %v", key, cachableObj)
return false, errors.New("nil found")
}

shouldUpdate := time.Now().Sub(cachableObj.Time) > mn.softTTL

return shouldUpdate, nil
}

// Set sets the value for a key in all layers of the cache instance
func (mn *MnemosyneInstance) Set(ctx context.Context, key string, value interface{}) error {
if value == nil {
Expand Down