@@ -3,13 +3,15 @@ package memcache4dalgo
3
3
import (
4
4
"context"
5
5
"encoding/json"
6
+ "fmt"
6
7
"github.com/dal-go/dalgo/dal"
7
8
"google.golang.org/appengine/memcache"
8
9
)
9
10
10
11
func getRecord (
11
12
ctx context.Context ,
12
13
record dal.Record ,
14
+ caller string ,
13
15
isCacheable func (key * dal.Key ) bool ,
14
16
get func (ctx context.Context , record dal.Record ) error ,
15
17
) (err error ) {
@@ -18,22 +20,26 @@ func getRecord(
18
20
return get (ctx , record )
19
21
}
20
22
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
+ }
21
28
var item * memcache.Item
22
29
if item , err = memcache .Get (ctx , mk ); err == nil {
23
30
record .SetError (nil ) // We must indicate we are going to access data for unmarshalling
24
31
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
29
34
} 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 )
31
36
}
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 )
34
40
}
35
41
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 {
37
43
return
38
44
}
39
45
}
@@ -46,7 +52,7 @@ func setRecordToCache(ctx context.Context, record dal.Record, caller string) (er
46
52
mk := record .Key ().String ()
47
53
_ = memcache .Set (ctx , & memcache.Item {Value : value , Key : mk })
48
54
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 )
50
56
}
51
57
}
52
58
return
0 commit comments