Repository navigation
Expand file tree
/
Copy pathpgsql_z_unit_feature_cache_test.go
More file actions
352 lines (304 loc) · 11.2 KB
/
Copy pathpgsql_z_unit_feature_cache_test.go
File metadata and controls
352 lines (304 loc) · 11.2 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package pgsql_test
import (
"context"
"testing"
"time"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/test/gtest"
)
// Test_Model_Cache_Basic tests basic cache functionality.
func Test_Model_Cache_Basic(t *testing.T) {
table := createInitTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
// First query - cache miss, result from DB
one, err := db.Model(table).Cache(gdb.CacheOption{
Duration: time.Second * 10,
Name: "test_cache_basic",
}).Where("id", 1).One()
t.AssertNil(err)
t.Assert(one["id"], 1)
t.Assert(one["passport"], "user_1")
// Update the record in DB
_, err = db.Model(table).Data(g.Map{"passport": "updated_user"}).Where("id", 1).Update()
t.AssertNil(err)
// Second query - cache hit, still returns old cached value
one, err = db.Model(table).Cache(gdb.CacheOption{
Duration: time.Second * 10,
Name: "test_cache_basic",
}).Where("id", 1).One()
t.AssertNil(err)
t.Assert(one["passport"], "user_1") // cached value, not "updated_user"
// Query without cache - returns updated value from DB
one, err = db.Model(table).Where("id", 1).One()
t.AssertNil(err)
t.Assert(one["passport"], "updated_user")
})
}
// Test_Model_Cache_TTL tests cache TTL expiration.
func Test_Model_Cache_TTL(t *testing.T) {
table := createInitTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
// Cache with short TTL
one, err := db.Model(table).Cache(gdb.CacheOption{
Duration: time.Millisecond * 100, // 100ms TTL
Name: "test_cache_ttl",
}).Where("id", 1).One()
t.AssertNil(err)
t.Assert(one["passport"], "user_1")
// Update record
_, err = db.Model(table).Data(g.Map{"passport": "ttl_test"}).Where("id", 1).Update()
t.AssertNil(err)
// Wait for cache to expire. Serving the cached value before it expires is
// covered by Test_Model_Cache_Basic, whose TTL is wide enough that a slow
// database round trip cannot exhaust it.
time.Sleep(time.Millisecond * 300)
// Query after expiration - should get fresh data
one, err = db.Model(table).Cache(gdb.CacheOption{
Duration: time.Millisecond * 100,
Name: "test_cache_ttl",
}).Where("id", 1).One()
t.AssertNil(err)
t.Assert(one["passport"], "ttl_test") // fresh value from DB
})
}
// Test_Model_Cache_Clear tests clearing cache with negative duration.
func Test_Model_Cache_Clear(t *testing.T) {
table := createInitTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
// Set cache
one, err := db.Model(table).Cache(gdb.CacheOption{
Duration: time.Second * 60,
Name: "test_cache_clear",
}).Where("id", 1).One()
t.AssertNil(err)
t.Assert(one["passport"], "user_1")
// Update record and clear cache
_, err = db.Model(table).Cache(gdb.CacheOption{
Duration: -1,
Name: "test_cache_clear",
}).Data(g.Map{"passport": "cleared"}).Where("id", 1).Update()
t.AssertNil(err)
// Query again - should get fresh data since cache was cleared
one, err = db.Model(table).Cache(gdb.CacheOption{
Duration: time.Second * 60,
Name: "test_cache_clear",
}).Where("id", 1).One()
t.AssertNil(err)
t.Assert(one["passport"], "cleared")
})
}
// Test_Model_Cache_NoExpire tests cache with no expiration (Duration=0).
func Test_Model_Cache_NoExpire(t *testing.T) {
table := createInitTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
// Cache with no expiration
one, err := db.Model(table).Cache(gdb.CacheOption{
Duration: 0, // never expires
Name: "test_cache_no_expire",
}).Where("id", 1).One()
t.AssertNil(err)
t.Assert(one["passport"], "user_1")
// Update record
_, err = db.Model(table).Data(g.Map{"passport": "no_expire_test"}).Where("id", 1).Update()
t.AssertNil(err)
// Wait a bit
time.Sleep(time.Millisecond * 100)
// Query - cache should still be valid
one, err = db.Model(table).Cache(gdb.CacheOption{
Duration: 0,
Name: "test_cache_no_expire",
}).Where("id", 1).One()
t.AssertNil(err)
t.Assert(one["passport"], "user_1") // cached value persists
// Clear the cache with update operation
_, err = db.Model(table).Cache(gdb.CacheOption{
Duration: -1,
Name: "test_cache_no_expire",
}).Data(g.Map{"nickname": "cleared"}).Where("id", 1).Update()
t.AssertNil(err)
})
}
// Test_Model_Cache_Force is a placeholder matching the MariaDB baseline.
// MariaDB version was removed due to cache invalidation semantics on INSERT;
// PgSQL retains the same placeholder for cross-driver parity of test names.
func Test_Model_Cache_Force(t *testing.T) {
table := createInitTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
missingOption := gdb.CacheOption{
Duration: time.Second * 10,
Name: "test_cache_force_empty",
Force: true,
}
one, err := db.Model(table).Cache(missingOption).Where("id", 100).One()
t.AssertNil(err)
t.Assert(one.IsEmpty(), true)
_, err = db.Model(table).Data(g.Map{"id": 100, "passport": "p", "password": "w", "nickname": "n", "create_time": "2018-10-24 10:00:00"}).InsertAndGetId()
t.AssertNil(err)
one, err = db.Model(table).Cache(missingOption).Where("id", 100).One()
t.AssertNil(err)
t.Assert(one.IsEmpty(), true)
valueOption := gdb.CacheOption{
Duration: time.Second * 10,
Name: "test_cache_force_empty_value",
Force: true,
}
_, err = db.Model(table).Data(g.Map{"id": 101, "passport": "", "password": "w", "nickname": "n", "create_time": "2018-10-24 10:00:00"}).InsertAndGetId()
t.AssertNil(err)
value, err := db.Model(table).Cache(valueOption).Where("id", 101).Value("passport")
t.AssertNil(err)
t.Assert(value.String(), "")
_, err = db.Model(table).Data(g.Map{"passport": "updated"}).Where("id", 101).Update()
t.AssertNil(err)
// A named cache entry is not invalidated by writes, so Force still returns the cached value.
value, err = db.Model(table).Cache(valueOption).Where("id", 101).Value("passport")
t.AssertNil(err)
t.Assert(value.String(), "")
arrayOption := gdb.CacheOption{
Duration: time.Second * 10,
Name: "test_cache_force_empty_array",
Force: true,
}
_, err = db.Model(table).Data(g.Map{"id": 102, "passport": "", "password": "w", "nickname": "n", "create_time": "2018-10-24 10:00:00"}).InsertAndGetId()
t.AssertNil(err)
values, err := db.Model(table).Cache(arrayOption).Where("id", 102).Array("passport")
t.AssertNil(err)
t.Assert(len(values), 1)
t.Assert(values[0].String(), "")
_, err = db.Model(table).Data(g.Map{"passport": "updated"}).Where("id", 102).Update()
t.AssertNil(err)
// A named cache entry is not invalidated by writes, so Force still returns the cached value.
values, err = db.Model(table).Cache(arrayOption).Where("id", 102).Array("passport")
t.AssertNil(err)
t.Assert(len(values), 1)
t.Assert(values[0].String(), "")
})
}
// Test_Model_Cache_DisabledInTransaction tests cache is disabled in transactions.
func Test_Model_Cache_DisabledInTransaction(t *testing.T) {
table := createInitTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
err := db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
// First query in transaction
one, err := tx.Model(table).Cache(gdb.CacheOption{
Duration: time.Second * 10,
Name: "test_tx_cache",
}).Where("id", 1).One()
t.AssertNil(err)
t.Assert(one["passport"], "user_1")
// Update in transaction
_, err = tx.Model(table).Data(g.Map{"passport": "tx_update"}).Where("id", 1).Update()
t.AssertNil(err)
// Second query - should see updated value (cache disabled in tx)
one, err = tx.Model(table).Cache(gdb.CacheOption{
Duration: time.Second * 10,
Name: "test_tx_cache",
}).Where("id", 1).One()
t.AssertNil(err)
t.Assert(one["passport"], "tx_update") // not cached, fresh from DB
return nil
})
t.AssertNil(err)
})
}
// Test_Model_PageCache tests pagination cache.
func Test_Model_PageCache(t *testing.T) {
table := createInitTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
// First page query with cache
all, count, err := db.Model(table).PageCache(
gdb.CacheOption{Duration: time.Second * 10, Name: "test_page_count"},
gdb.CacheOption{Duration: time.Second * 10, Name: "test_page_data"},
).Page(1, 3).AllAndCount(false)
t.AssertNil(err)
t.Assert(len(all), 3)
t.Assert(count, 10)
// Insert new record
_, err = db.Model(table).Data(g.Map{
"id": 11,
"passport": "user_11",
}).Insert()
t.AssertNil(err)
// Query again - should return cached results
all, count, err = db.Model(table).PageCache(
gdb.CacheOption{Duration: time.Second * 10, Name: "test_page_count"},
gdb.CacheOption{Duration: time.Second * 10, Name: "test_page_data"},
).Page(1, 3).AllAndCount(false)
t.AssertNil(err)
t.Assert(len(all), 3)
// Count comes from the cache, so it still reports the pre-insert value.
t.Assert(count, 10)
// Clear page cache by updating with Duration=-1
_, err = db.Model(table).Cache(gdb.CacheOption{
Duration: -1,
Name: "test_page_count",
}).Data(g.Map{"nickname": "page_test"}).Where("id", 1).Update()
t.AssertNil(err)
// Query with fresh cache - should return updated count
all, count, err = db.Model(table).PageCache(
gdb.CacheOption{Duration: time.Second * 10, Name: "test_page_count"},
gdb.CacheOption{Duration: time.Second * 10, Name: "test_page_data"},
).Page(1, 3).AllAndCount(false)
t.AssertNil(err)
t.Assert(len(all), 3)
// Count cache was cleared above, so the new row is visible.
t.Assert(count, 11)
// Verify total count increased
totalCount, err := db.Model(table).Count()
t.AssertNil(err)
t.Assert(totalCount, 11)
})
}
// Test_Model_Cache_DifferentNames tests different cache names for same query.
func Test_Model_Cache_DifferentNames(t *testing.T) {
table := createInitTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
// Cache with name1
one, err := db.Model(table).Cache(gdb.CacheOption{
Duration: time.Second * 10,
Name: "cache_name1",
}).Where("id", 1).One()
t.AssertNil(err)
t.Assert(one["passport"], "user_1")
// Cache same query with name2
one, err = db.Model(table).Cache(gdb.CacheOption{
Duration: time.Second * 10,
Name: "cache_name2",
}).Where("id", 1).One()
t.AssertNil(err)
t.Assert(one["passport"], "user_1")
// Update record and clear only cache_name1
_, err = db.Model(table).Cache(gdb.CacheOption{
Duration: -1,
Name: "cache_name1",
}).Data(g.Map{"passport": "diff_name"}).Where("id", 1).Update()
t.AssertNil(err)
// Query with cache_name1 - should get fresh data
one, err = db.Model(table).Cache(gdb.CacheOption{
Duration: time.Second * 10,
Name: "cache_name1",
}).Where("id", 1).One()
t.AssertNil(err)
t.Assert(one["passport"], "diff_name")
// Query with cache_name2 - should still have cached old value
one, err = db.Model(table).Cache(gdb.CacheOption{
Duration: time.Second * 10,
Name: "cache_name2",
}).Where("id", 1).One()
t.AssertNil(err)
t.Assert(one["passport"], "user_1") // still cached
})
}