Skip to content

Commit

Permalink
Syncing (schollz#106)
Browse files Browse the repository at this point in the history
* Preparing cache for syncing

* Cacheing works for psCache
  • Loading branch information
schollz authored Jul 21, 2016
1 parent a9a1d30 commit 5d63154
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 10 deletions.
34 changes: 30 additions & 4 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@

package main

import "time"
import (
"sync"
"time"
)

var psCache = struct {
sync.RWMutex
m map[string]FullParameters
}{m: make(map[string]FullParameters)}

var psCache map[string]FullParameters
var usersCache map[string][]string
var userPositionCache map[string]UserPositionJSON
var isLearning map[string]bool
Expand All @@ -19,10 +26,29 @@ func init() {

func clearCache() {
for {
Debug.Println("Resetting cache")
isLearning = make(map[string]bool)
psCache = make(map[string]FullParameters)
psCache.Lock()
psCache.m = make(map[string]FullParameters)
psCache.Unlock()
usersCache = make(map[string][]string)
userPositionCache = make(map[string]UserPositionJSON)
time.Sleep(time.Minute * 10)
time.Sleep(time.Second * 10)
}
}

func getPsCache(group string) (FullParameters, bool) {
Debug.Println("Getting pscache")
psCache.RLock()
psCached, ok := psCache.m[group]
psCache.RUnlock()
return psCached, ok
}

func setPsCache(group string, ps FullParameters) {
Debug.Println("Setting pscache")
psCache.Lock()
psCache.m[group] = ps
psCache.Unlock()
return
}
8 changes: 5 additions & 3 deletions parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,9 @@ func saveParameters(group string, res FullParameters) error {
}

func openParameters(group string) (FullParameters, error) {
if _, ok := psCache[group]; ok {
return psCache[group], nil
psCached, ok := getPsCache(group)
if ok {
return psCached, nil
}

var ps = *NewFullParameters()
Expand All @@ -152,7 +153,8 @@ func openParameters(group string) (FullParameters, error) {
ps = loadParameters(v)
return nil
})
psCache[group] = ps

setPsCache(group, ps)
return ps, err
}

Expand Down
2 changes: 1 addition & 1 deletion priors.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func optimizePriors(group string) {
}

go saveParameters(group, ps)
psCache[group] = ps
go setPsCache(group, ps)
}

func regenerateEverything(group string) {
Expand Down
4 changes: 2 additions & 2 deletions priorsThreaded.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func optimizePriorsThreaded(group string) error {
usersCache = make(map[string][]string)
// Debug.Println(getUsers(group))
go saveParameters(group, ps)
psCache[group] = ps
go setPsCache(group, ps)

return nil
}
Expand Down Expand Up @@ -369,6 +369,6 @@ func optimizePriorsThreadedNot(group string) {
crossValidation(group, n, &ps, fingerprintsInMemory, fingerprintsOrdering)
}
go saveParameters(group, ps)
psCache[group] = ps
go setPsCache(group, ps)
// Debug.Println("Analyzed ", totalJobs, " fingerprints")
}

0 comments on commit 5d63154

Please sign in to comment.