Skip to content

Commit

Permalink
New caching
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Andersen committed Oct 11, 2018
1 parent 3b84128 commit 1202753
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 25 deletions.
29 changes: 16 additions & 13 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 17 additions & 8 deletions engine/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,30 @@ import (
)

var cachemu sync.RWMutex
var entityCache map[[32]byte]*ecacheItem
var entityCache map[ecacheKey]*ecacheItem
var rvkCache map[string]time.Time

var entityCacheTime = 3 * time.Hour

type ecacheKey struct {
Hash [32]byte
Location string
}

type ecacheItem struct {
Val *iapi.Entity
CheckExpiry time.Time
}

func init() {
entityCache = make(map[[32]byte]*ecacheItem)
entityCache = make(map[ecacheKey]*ecacheItem)
rvkCache = make(map[string]time.Time)
}

func cacheEntity(e *iapi.Entity) {
k := e.ArrayKeccak256()
func cacheEntity(e *iapi.Entity, loc iapi.LocationSchemeInstance) {
ar := e.ArrayKeccak256()
l := loc.(*iapi.LocationSchemeInstanceURL).SerdesForm.Value
k := ecacheKey{ar, l}
cachemu.Lock()
entityCache[k] = &ecacheItem{
Val: e,
Expand All @@ -33,11 +40,13 @@ func cacheEntity(e *iapi.Entity) {
cachemu.Unlock()
}

func getCachedEntity(h iapi.HashSchemeInstance) *iapi.Entity {
ak := [32]byte{}
copy(ak[:], h.Value())
func getCachedEntity(h iapi.HashSchemeInstance, loc iapi.LocationSchemeInstance) *iapi.Entity {
l := loc.(*iapi.LocationSchemeInstanceURL).SerdesForm.Value
ar := [32]byte{}
copy(ar[:], h.Value())
k := ecacheKey{ar, l}
cachemu.RLock()
rez, ok := entityCache[ak]
rez, ok := entityCache[k]
cachemu.RUnlock()
if ok && rez.CheckExpiry.After(time.Now()) {
return rez.Val
Expand Down
6 changes: 3 additions & 3 deletions engine/external.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,14 +450,14 @@ func (e *Engine) CheckEntity(ctx context.Context, ent *iapi.Entity) (*Validity,

func (e *Engine) LookupEntity(ctx context.Context, hash iapi.HashSchemeInstance, loc iapi.LocationSchemeInstance) (*iapi.Entity, *Validity, error) {
var err error
ent := getCachedEntity(hash)
ent := getCachedEntity(hash, loc)
if ent == nil {
ent, err = e.ws.GetEntityByHashSchemeInstanceG(ctx, hash)
if err != nil {
return nil, nil, err
}
if ent != nil {
cacheEntity(ent)
cacheEntity(ent, loc)
}
}

Expand All @@ -484,7 +484,7 @@ func (e *Engine) LookupEntity(ctx context.Context, hash iapi.HashSchemeInstance,
return nil, nil, err
}

cacheEntity(ent)
cacheEntity(ent, loc)

val, err := e.CheckEntity(ctx, ent)
//fmt.Printf("validity in lookup: %v\n", val)
Expand Down
2 changes: 1 addition & 1 deletion engine/states.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ nextlocation:
for _, loc := range locs {
hashscheme, err := e.st.HashSchemeFor(loc)
if err != nil {
panic(err)
continue //problem with this location
}
for {
// get current token
Expand Down

0 comments on commit 1202753

Please sign in to comment.