Skip to content

Commit b23bbb2

Browse files
committed
make ver optional
1 parent 32983db commit b23bbb2

File tree

10 files changed

+824
-50
lines changed

10 files changed

+824
-50
lines changed

dockertest.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ go install honnef.co/go/tools/cmd/staticcheck@latest
1515
# -U1000 unused check in mock package
1616
staticcheck -checks "all,-ST1000,-ST1003,-ST1012,-ST1016,-ST1020,-ST1021,-U1000" ./... | (grep -v "_test.go:" && exit 1 || exit 0)
1717

18-
trap "docker compose down -v" EXIT
19-
docker compose up -d
18+
trap "docker compose --progress plain down -v" EXIT
19+
docker compose --progress plain up -d
2020
sleep 5
2121
go install gotest.tools/[email protected]
2222
gotestsum --format standard-verbose --junitfile unit-tests.xml -- -coverprofile=coverage.out -race -timeout 30m "$@"

om/go.mod

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@ toolchain go1.23.4
77
replace github.com/redis/rueidis => ../
88

99
require (
10+
github.com/kr/pretty v0.1.0
1011
github.com/oklog/ulid/v2 v2.1.0
1112
github.com/redis/rueidis v1.0.61
1213
)
1314

14-
require golang.org/x/sys v0.31.0 // indirect
15+
require (
16+
github.com/kr/text v0.2.0 // indirect
17+
golang.org/x/sys v0.31.0 // indirect
18+
)

om/go.sum

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1+
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
12
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
23
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
4+
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
5+
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
6+
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
7+
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
8+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
9+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
310
github.com/oklog/ulid/v2 v2.1.0 h1:+9lhoxAP56we25tyYETBBY1YLA2SaoLvUFgrP2miPJU=
411
github.com/oklog/ulid/v2 v2.1.0/go.mod h1:rcEKHmBBKfef9DhnvX7y1HZBYxjXb0cP5ExxNsTT1QQ=
512
github.com/onsi/gomega v1.36.2 h1:koNYke6TVk6ZmnyHrCXba/T/MoLBXFjeC1PtvYgw0A8=
613
github.com/onsi/gomega v1.36.2/go.mod h1:DdwyADRjrc825LhMEkD76cHR5+pUnjhUN8GlHlRPHzY=
714
github.com/pborman/getopt v0.0.0-20170112200414-7148bc3a4c30/go.mod h1:85jBQOZwpVEaDAr341tbn15RS4fCAsIst0qp7i8ex1o=
8-
golang.org/x/net v0.37.0 h1:1zLorHbz+LYj7MQlSf1+2tPIIgibq2eL5xkrGk6f+2c=
9-
golang.org/x/net v0.37.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8=
15+
golang.org/x/net v0.38.0 h1:vRMAPTMaeGqVhG5QyLJHqNDwecKTomGeqbnfZyKlBI8=
16+
golang.org/x/net v0.38.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8=
1017
golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=
1118
golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
1219
golang.org/x/text v0.23.0 h1:D71I7dUrlY+VX0gQShAThNGHFxZ13dGLBHQLVl1mJlY=

om/hash.go

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,19 @@ func (r *HashRepository[T]) FetchCache(ctx context.Context, id string, ttl time.
6565
return v, err
6666
}
6767

68-
func (r *HashRepository[T]) toExec(entity *T) (val reflect.Value, exec rueidis.LuaExec) {
69-
val = reflect.ValueOf(entity).Elem()
68+
func (r *HashRepository[T]) toExec(entity *T) (verf reflect.Value, exec rueidis.LuaExec) {
69+
val := reflect.ValueOf(entity).Elem()
70+
if !r.schema.verless {
71+
verf = val.Field(r.schema.ver.idx)
72+
} else {
73+
verf = reflect.ValueOf(int64(0)) // verless, set verf to a dummy value
74+
}
7075
fields := r.factory.NewConverter(val).ToHash()
7176
keyVal := fields[r.schema.key.name]
72-
verVal := fields[r.schema.ver.name]
77+
var verVal string
78+
if !r.schema.verless {
79+
verVal = fields[r.schema.ver.name]
80+
}
7381
extVal := int64(0)
7482
if r.schema.ext != nil {
7583
if ext, ok := val.Field(r.schema.ext.idx).Interface().(time.Time); ok && !ext.IsZero() {
@@ -96,34 +104,42 @@ func (r *HashRepository[T]) toExec(entity *T) (val reflect.Value, exec rueidis.L
96104
// Save the entity under the redis key of `{prefix}:{id}`.
97105
// It also uses the `redis:",ver"` field and lua script to perform optimistic locking and prevent lost update.
98106
func (r *HashRepository[T]) Save(ctx context.Context, entity *T) (err error) {
99-
val, exec := r.toExec(entity)
107+
verf, exec := r.toExec(entity)
100108
str, err := hashSaveScript.Exec(ctx, r.client, exec.Keys, exec.Args).ToString()
101109
if rueidis.IsRedisNil(err) {
110+
if r.schema.verless {
111+
return nil
112+
}
102113
return ErrVersionMismatch
103-
}
104-
if err == nil {
114+
} else if err == nil {
105115
ver, _ := strconv.ParseInt(str, 10, 64)
106-
val.Field(r.schema.ver.idx).SetInt(ver)
116+
verf.SetInt(ver)
107117
}
108118
return err
109119
}
110120

111121
// SaveMulti batches multiple HashRepository.Save at once
112122
func (r *HashRepository[T]) SaveMulti(ctx context.Context, entities ...*T) []error {
113123
errs := make([]error, len(entities))
114-
vals := make([]reflect.Value, len(entities))
124+
verf := make([]reflect.Value, len(entities))
115125
exec := make([]rueidis.LuaExec, len(entities))
116126
for i, entity := range entities {
117-
vals[i], exec[i] = r.toExec(entity)
127+
verf[i], exec[i] = r.toExec(entity)
118128
}
119129
for i, resp := range hashSaveScript.ExecMulti(ctx, r.client, exec...) {
120-
if str, err := resp.ToString(); err != nil {
121-
if errs[i] = err; rueidis.IsRedisNil(err) {
122-
errs[i] = ErrVersionMismatch
130+
str, err := resp.ToString()
131+
if rueidis.IsRedisNil(err) {
132+
if r.schema.verless {
133+
continue
123134
}
124-
} else {
135+
errs[i] = ErrVersionMismatch
136+
continue
137+
}
138+
if err == nil {
125139
ver, _ := strconv.ParseInt(str, 10, 64)
126-
vals[i].Field(r.schema.ver.idx).SetInt(ver)
140+
verf[i].SetInt(ver)
141+
} else {
142+
errs[i] = err
127143
}
128144
}
129145
return errs
@@ -200,6 +216,15 @@ func (r *HashRepository[T]) fromFields(fields map[string]string) (*T, error) {
200216
}
201217

202218
var hashSaveScript = rueidis.NewLuaScript(`
219+
if (ARGV[1] == '')
220+
then
221+
local e = (#ARGV % 2 == 1) and table.remove(ARGV) or nil
222+
if redis.call('HSET',KEYS[1],unpack(ARGV))
223+
then
224+
if e then redis.call('PEXPIREAT',KEYS[1],e) end
225+
end
226+
return nil
227+
end
203228
local v = redis.call('HGET',KEYS[1],ARGV[1])
204229
if (not v or v == ARGV[2])
205230
then

0 commit comments

Comments
 (0)