diff --git a/cache_store.go b/cache_store.go index a77d3903..a91891b2 100644 --- a/cache_store.go +++ b/cache_store.go @@ -6,6 +6,7 @@ import ( "reflect" "sort" "strconv" + "strings" "sync" "time" ) @@ -110,7 +111,7 @@ func (c cache) GetItem(key string, offset int64) (interface{}, error) { item.dataMutex.RLock() defer item.dataMutex.RUnlock() - if (item.created+offset > time.Now().Unix()) || offset == -1 { + if (item.created+(offset*1000000000) > time.Now().UnixNano()) || offset == -1 { fmt.Print("Found \n") c.mutex.RUnlock() return item.data, item.err @@ -208,5 +209,24 @@ func getID(data interface{}) string { d := reflect.ValueOf(data) idField := d.FieldByName("ID") - return fmt.Sprint(idField.Interface()) + if idField.IsValid() { + return fmt.Sprint(idField.Interface()) + } + + // We haven't found an id the easy way so instead go through all of the primary key fields + // From those fields, get the value and concat using / as a seperator + idParts := []string{} + intType := reflect.TypeOf(data) + for i := 0; i < intType.NumField(); i++ { + tag := intType.Field(i).Tag + if strings.Contains(tag.Get("gorm"), "primary_key") { + idParts = append(idParts, d.Field(i).String()) + } + } + + if len(idParts) > 0 { + return strings.Join(idParts, "/") + } + + return "" } diff --git a/callback_query.go b/callback_query.go index 9da29ed4..555e94e4 100644 --- a/callback_query.go +++ b/callback_query.go @@ -79,6 +79,14 @@ func queryCallback(scope *Scope) { if cacheResults != nil { scope.Err(err) // Add any error if exists results.Set(reflect.ValueOf(cacheResults)) + + switch reflect.ValueOf(cacheResults).Type().Kind() { + case reflect.Struct: + scope.db.RowsAffected = 1 + case reflect.Slice: + scope.db.RowsAffected = int64(reflect.ValueOf(cacheResults).Len()) + } + fmt.Println("Cache HIT") readFromDB = false } else {