From 5b795b1e93b99cc1f026dd8693f5078bd0253196 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Fri, 11 May 2018 17:11:51 -0500 Subject: [PATCH] Cache results --- model.go | 16 +++++++++++----- model_timeentry.go | 1 + 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/model.go b/model.go index 7f95acc..5714851 100644 --- a/model.go +++ b/model.go @@ -16,7 +16,8 @@ type GimeDB struct { path string filename, arch string - AllTypes []int + AllTypes []int + TypeCollections map[int]*TimeEntryCollection } const ( @@ -49,10 +50,11 @@ func LoadDatabase(path, name, archName string) (*GimeDB, error) { path = path + "/" } gdb := GimeDB{ - path: path, - filename: name, - arch: archName, - AllTypes: []int{TypeCurrent, TypeRecent, TypeArchive}, + path: path, + filename: name, + arch: archName, + AllTypes: []int{TypeCurrent, TypeRecent, TypeArchive}, + TypeCollections: make(map[int]*TimeEntryCollection), } if err := gdb.initDatabase(); err != nil { fmt.Println(err.Error()) @@ -63,11 +65,15 @@ func LoadDatabase(path, name, archName string) (*GimeDB, error) { // Load a TimeEntry collection from a database func (gdb *GimeDB) LoadTimeEntryCollection(tp int) *TimeEntryCollection { + if v, ok := gdb.TypeCollections[tp]; ok { + return v + } ret := new(TimeEntryCollection) entries := gdb.dbGetAllTimeEntries(tp) for i := range entries { ret.Push(&entries[i]) } + gdb.TypeCollections[tp] = ret return ret } diff --git a/model_timeentry.go b/model_timeentry.go index dd3ef34..d6177df 100644 --- a/model_timeentry.go +++ b/model_timeentry.go @@ -225,6 +225,7 @@ func (gdb *GimeDB) dbGetAllTimeEntries(tp int) []TimeEntry { ret = append(ret, *te) } } + return ret }