Skip to content

Commit 9e1aff0

Browse files
committed
fix: improved logging on Get() 2
1 parent 3084d7a commit 9e1aff0

File tree

4 files changed

+19
-13
lines changed

4 files changed

+19
-13
lines changed

memcache4dalgo/database.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,5 @@ func (v database) QueryAllRecords(ctx context.Context, query dal.Query) (records
5959
}
6060

6161
func (v database) Get(ctx context.Context, record dal.Record) (err error) {
62-
return getRecord(ctx, record, v.isCacheable, v.db.Get)
62+
return getRecord(ctx, record, "db", v.isCacheable, v.db.Get)
6363
}

memcache4dalgo/get.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ package memcache4dalgo
33
import (
44
"context"
55
"encoding/json"
6+
"fmt"
67
"github.com/dal-go/dalgo/dal"
78
"google.golang.org/appengine/memcache"
89
)
910

1011
func getRecord(
1112
ctx context.Context,
1213
record dal.Record,
14+
caller string,
1315
isCacheable func(key *dal.Key) bool,
1416
get func(ctx context.Context, record dal.Record) error,
1517
) (err error) {
@@ -18,22 +20,26 @@ func getRecord(
1820
return get(ctx, record)
1921
}
2022
mk := key.String()
23+
debugf := func(ctx context.Context, format string, args ...any) {
24+
if Debugf != nil {
25+
Debugf(ctx, "memcache4dalgo.getRecord("+caller+"): "+format, args...)
26+
}
27+
}
2128
var item *memcache.Item
2229
if item, err = memcache.Get(ctx, mk); err == nil {
2330
record.SetError(nil) // We must indicate we are going to access data for unmarshalling
2431
if err = json.Unmarshal(item.Value, record.Data()); err == nil {
25-
if Debugf != nil {
26-
Debugf(ctx, "memcache4dalgo.getRecord: hit %s", mk)
27-
}
28-
return
32+
debugf(ctx, "cache hit on key=%s", mk)
33+
return // No need t get the record from the database
2934
} else if Debugf != nil { // Ignore the error and try to get the record from the database
30-
Debugf(ctx, "memcache4dalgo.getRecord: failed to unmarshal value from received from memcache ny key=%s: %v", mk, err)
35+
debugf(ctx, "failed to unmarshal value from memcache, key=%s: %v", mk, err)
3136
}
32-
} else if Debugf != nil { // Ignore the error and try to get the record from the database
33-
Debugf(ctx, "memcache4dalgo.getRecord: memcache.Get(key=%s) returned error: %v", mk, err)
37+
} else {
38+
// Ignore the error and try to get the record from the database
39+
debugf(ctx, "WARNING: memcache.Get(key=%s) returned error: %v", mk, err)
3440
}
3541
if err = get(ctx, record); err == nil {
36-
if err = setRecordToCache(ctx, record, "getRecord"); err != nil {
42+
if err = setRecordToCache(ctx, record, fmt.Sprintf("getRecord(%s)", caller)); err != nil {
3743
return
3844
}
3945
}
@@ -46,7 +52,7 @@ func setRecordToCache(ctx context.Context, record dal.Record, caller string) (er
4652
mk := record.Key().String()
4753
_ = memcache.Set(ctx, &memcache.Item{Value: value, Key: mk})
4854
if Debugf != nil {
49-
Debugf(ctx, "memcache4dalgo.%s(): set record to cache with key=%s", caller, mk)
55+
Debugf(ctx, "memcache4dalgo.%s: set record to cache with key=%s", caller, mk)
5056
}
5157
}
5258
return

memcache4dalgo/logging.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ package memcache4dalgo
22

33
import "context"
44

5-
var Debugf func(ctx context.Context, format string, args ...interface{}) = nil
5+
var Debugf func(ctx context.Context, format string, args ...any) = nil

memcache4dalgo/transaction.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func (t transaction) Options() dal.TransactionOptions {
2424
}
2525

2626
func (t transaction) Get(ctx context.Context, record dal.Record) error {
27-
return getRecord(ctx, record, t.isCacheable, t.ro.Get)
27+
return getRecord(ctx, record, "tx", t.isCacheable, t.ro.Get)
2828
}
2929

3030
func (t transaction) GetMulti(ctx context.Context, records []dal.Record) error {
@@ -79,7 +79,7 @@ func (t transaction) UpdateRecord(ctx context.Context, record dal.Record, update
7979
return
8080
}
8181
if err = t.rw.UpdateRecord(ctx, record, updates, preconditions...); err == nil {
82-
if err = setRecordToCache(ctx, record, "UpdateRecord"); err != nil {
82+
if err = setRecordToCache(ctx, record, "UpdateRecord()"); err != nil {
8383
return
8484
}
8585
}

0 commit comments

Comments
 (0)