-
Notifications
You must be signed in to change notification settings - Fork 0
/
dao.go
86 lines (69 loc) · 2.03 KB
/
dao.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package data
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"github.com/PacktPublishing/Hands-On-Dependency-Injection-in-Go/ch12/04_new_service/01_data_with_cache/internal/cache"
)
// DAO is a data access object that provides an abstraction over our database interactions.
type DAO struct {
cfg Config
db *sql.DB
cache *cache.Cache
}
// Load will attempt to load and return a person.
// It will return ErrNotFound when the requested person does not exist.
// Any other errors returned are caused by the underlying database or our connection to it.
func (d *DAO) Load(ctx context.Context, ID int) (*Person, error) {
// load from cache
out := d.loadFromCache(ID)
if out != nil {
return out, nil
}
// load from database
row := d.db.QueryRowContext(ctx, sqlLoadByID, ID)
// retrieve columns and populate the person object
out, err := populatePerson(row.Scan)
if err != nil {
if err == sql.ErrNoRows {
d.cfg.Logger().Warn("failed to load requested person '%d'. err: %s", ID, err)
return nil, ErrNotFound
}
d.cfg.Logger().Error("failed to convert query result. err: %s", err)
return nil, err
}
// save person into the cache
d.saveToCache(ID, out)
return out, nil
}
func (d *DAO) loadFromCache(ID int) *Person {
payload, err := d.cache.Get(d.buildCacheKey(ID))
if err != nil {
d.cfg.Logger().Error("failed to load requested person from cache with error: %s", err)
return nil
}
if payload == nil {
return nil
}
out := &Person{}
err = json.Unmarshal(payload, out)
if err != nil {
d.cfg.Logger().Error("failed to decode cache response with error: %s", err)
}
return out
}
func (d *DAO) saveToCache(ID int, person *Person) {
payload, err := json.Marshal(person)
if err != nil {
d.cfg.Logger().Error("failed to encode person to JSON with error: %s", err)
return
}
err = d.cache.Set(d.buildCacheKey(ID), payload)
if err != nil {
d.cfg.Logger().Error("failed to save person into cache with error: %s", err)
}
}
func (d *DAO) buildCacheKey(ID int) string {
return fmt.Sprintf("person-%d", ID)
}