-
Notifications
You must be signed in to change notification settings - Fork 75
/
cartCache.go
225 lines (190 loc) · 6.39 KB
/
cartCache.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package application
import (
"context"
"encoding/gob"
"fmt"
"strings"
"time"
"flamingo.me/flamingo-commerce/v3/cart/domain/cart"
"flamingo.me/flamingo/v3/core/auth"
"flamingo.me/flamingo/v3/framework/flamingo"
"flamingo.me/flamingo/v3/framework/web"
"github.com/pkg/errors"
)
type (
// CartCache describes a cart caches methods
CartCache interface {
GetCart(context.Context, *web.Session, CartCacheIdentifier) (*cart.Cart, error)
CacheCart(context.Context, *web.Session, CartCacheIdentifier, *cart.Cart) error
Invalidate(context.Context, *web.Session, CartCacheIdentifier) error
Delete(context.Context, *web.Session, CartCacheIdentifier) error
DeleteAll(context.Context, *web.Session) error
BuildIdentifier(context.Context, *web.Session) (CartCacheIdentifier, error)
}
// CartCacheIdentifier identifies Cart Caches
CartCacheIdentifier struct {
GuestCartID string
IsCustomerCart bool
CustomerID string
}
// CartSessionCache defines a Cart Cache
CartSessionCache struct {
logger flamingo.Logger
webIdentityService *auth.WebIdentityService
lifetimeSeconds float64
}
// CachedCartEntry defines a single Cart Cache Entry
CachedCartEntry struct {
IsInvalid bool
Entry cart.Cart
ExpiresOn time.Time
}
)
const (
// CartSessionCacheCacheKeyPrefix is a string prefix for Cart Cache Keys
CartSessionCacheCacheKeyPrefix = "cart.sessioncache."
)
var (
_ CartCache = (*CartSessionCache)(nil)
// ErrCacheIsInvalid sets generalized invalid Cache Error
ErrCacheIsInvalid = errors.New("cache is invalid")
// ErrNoCacheEntry - used if cache is not found
ErrNoCacheEntry = errors.New("cache entry not found")
)
func init() {
gob.Register(CachedCartEntry{})
}
// CacheKey creates a Cache Key Identifier string
func (ci *CartCacheIdentifier) CacheKey() string {
return fmt.Sprintf(
"cart_%v_%v",
ci.CustomerID,
ci.GuestCartID,
)
}
// BuildIdentifierFromCart creates a Cache Identifier from Cart Data
// DEPRICATED
func BuildIdentifierFromCart(cart *cart.Cart) (*CartCacheIdentifier, error) {
if cart == nil {
return nil, errors.New("no cart")
}
if cart.BelongsToAuthenticatedUser {
return &CartCacheIdentifier{
CustomerID: cart.AuthenticatedUserID,
IsCustomerCart: true,
}, nil
}
return &CartCacheIdentifier{
GuestCartID: cart.ID,
CustomerID: cart.AuthenticatedUserID,
IsCustomerCart: false,
}, nil
}
// Inject the dependencies
func (cs *CartSessionCache) Inject(
logger flamingo.Logger,
webIdentityService *auth.WebIdentityService,
config *struct {
LifetimeSeconds float64 `inject:"config:commerce.cart.cacheLifetime"` // in seconds
},
) {
cs.webIdentityService = webIdentityService
cs.logger = logger.WithField(flamingo.LogKeyCategory, "CartSessionCache").WithField(flamingo.LogKeyModule, "cart")
if config != nil {
cs.lifetimeSeconds = config.LifetimeSeconds
}
}
// BuildIdentifier creates a CartCacheIdentifier based on the login state
func (cs *CartSessionCache) BuildIdentifier(ctx context.Context, session *web.Session) (CartCacheIdentifier, error) {
identity := cs.webIdentityService.Identify(ctx, web.RequestFromContext(ctx))
if identity != nil {
return CartCacheIdentifier{
CustomerID: identity.Subject(),
IsCustomerCart: true,
}, nil
}
guestCartID, ok := session.Load(GuestCartSessionKey)
if !ok {
return CartCacheIdentifier{}, errors.New("Fatal - ShouldHaveGuestCart returned true but got no GuestCartSessionKey?")
}
guestCartIDString, ok := guestCartID.(string)
if !ok {
return CartCacheIdentifier{}, errors.New("Fatal - ShouldHaveGuestCart returned true but got no GuestCartSessionKey string")
}
return CartCacheIdentifier{
GuestCartID: guestCartIDString,
}, nil
}
// GetCart fetches a Cart from the Cache
func (cs *CartSessionCache) GetCart(ctx context.Context, session *web.Session, id CartCacheIdentifier) (*cart.Cart, error) {
if cache, ok := session.Load(CartSessionCacheCacheKeyPrefix + id.CacheKey()); ok {
if cachedCartsEntry, ok := cache.(CachedCartEntry); ok {
cs.logger.WithContext(ctx).Debugf("Found cached cart: %v InValid: %v", id.CacheKey(), cachedCartsEntry.IsInvalid)
if cachedCartsEntry.IsInvalid {
return &cachedCartsEntry.Entry, ErrCacheIsInvalid
}
if time.Now().After(cachedCartsEntry.ExpiresOn) {
err := cs.Invalidate(ctx, session, id)
if err != nil {
return nil, err
}
return nil, ErrCacheIsInvalid
}
return &cachedCartsEntry.Entry, nil
}
cs.logger.WithContext(ctx).Error("Cannot Cast Cache Entry %v", id.CacheKey())
return nil, errors.New("cart cache contains invalid data at cache key")
}
cs.logger.WithContext(ctx).Debug("Did not Found cached cart %v", id.CacheKey())
return nil, ErrNoCacheEntry
}
// CacheCart adds a Cart to the Cache
func (cs *CartSessionCache) CacheCart(ctx context.Context, session *web.Session, id CartCacheIdentifier, cartForCache *cart.Cart) error {
if cartForCache == nil {
return errors.New("no cart given to cache")
}
entry := CachedCartEntry{
Entry: *cartForCache,
ExpiresOn: time.Now().Add(time.Duration(cs.lifetimeSeconds * float64(time.Second))),
}
cs.logger.WithContext(ctx).Debug("Caching cart %v", id.CacheKey())
session.Store(CartSessionCacheCacheKeyPrefix+id.CacheKey(), entry)
return nil
}
// Invalidate a Cache Entry
func (cs *CartSessionCache) Invalidate(ctx context.Context, session *web.Session, id CartCacheIdentifier) error {
if cache, ok := session.Load(CartSessionCacheCacheKeyPrefix + id.CacheKey()); ok {
if cachedCartsEntry, ok := cache.(CachedCartEntry); ok {
cachedCartsEntry.IsInvalid = true
session.Store(CartSessionCacheCacheKeyPrefix+id.CacheKey(), cachedCartsEntry)
return nil
}
}
return ErrNoCacheEntry
}
// Delete a Cache entry
func (cs *CartSessionCache) Delete(ctx context.Context, session *web.Session, id CartCacheIdentifier) error {
if _, ok := session.Load(CartSessionCacheCacheKeyPrefix + id.CacheKey()); ok {
session.Delete(CartSessionCacheCacheKeyPrefix + id.CacheKey())
// ok deleted something
return nil
}
return ErrNoCacheEntry
}
// DeleteAll empties the Cache
func (cs *CartSessionCache) DeleteAll(ctx context.Context, session *web.Session) error {
deleted := false
for _, k := range session.Keys() {
if stringKey, ok := k.(string); ok {
if strings.Contains(stringKey, CartSessionCacheCacheKeyPrefix) {
session.Delete(k)
deleted = true
}
}
}
if deleted {
// successfully deleted something
return nil
}
return ErrNoCacheEntry
}