Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
alicebob committed Oct 11, 2023
1 parent a64b9ef commit dda3a52
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 78 deletions.
4 changes: 2 additions & 2 deletions cmd_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func makeCmdExpire(m *Miniredis, unix bool, d time.Duration) func(*server.Peer,
return
}
db.ttl[opts.key] = newTTL
db.touch(opts.key, true)
db.incr(opts.key)
db.checkTTL(opts.key)
c.WriteInt(1)
})
Expand Down Expand Up @@ -274,7 +274,7 @@ func (m *Miniredis) cmdPersist(c *server.Peer, cmd string, args []string) {
return
}
delete(db.ttl, key)
db.touch(key, true)
db.incr(key)
c.WriteInt(1)
})
}
Expand Down
2 changes: 1 addition & 1 deletion cmd_hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (m *Miniredis) cmdHsetnx(c *server.Peer, cmd string, args []string) {
return
}
db.hashKeys[opts.key][opts.field] = opts.value
db.touch(opts.key, true)
db.incr(opts.key)
c.WriteInt(1)
})
}
Expand Down
8 changes: 4 additions & 4 deletions cmd_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ func (m *Miniredis) cmdLinsert(c *server.Peer, cmd string, args []string) {
}
}
db.listKeys[key] = l
db.touch(key, true)
db.incr(key)
c.WriteInt(len(l))
return
}
Expand Down Expand Up @@ -715,7 +715,7 @@ func (m *Miniredis) cmdLrem(c *server.Peer, cmd string, args []string) {
db.del(opts.key, true)
} else {
db.listKeys[opts.key] = newL
db.touch(opts.key, true)
db.incr(opts.key)
}

c.WriteInt(deleted)
Expand Down Expand Up @@ -769,7 +769,7 @@ func (m *Miniredis) cmdLset(c *server.Peer, cmd string, args []string) {
return
}
l[index] = opts.value
db.touch(opts.key, true)
db.incr(opts.key)

c.WriteOK()
})
Expand Down Expand Up @@ -823,7 +823,7 @@ func (m *Miniredis) cmdLtrim(c *server.Peer, cmd string, args []string) {
db.del(opts.key, true)
} else {
db.listKeys[opts.key] = l
db.touch(opts.key, true)
db.incr(opts.key)
}
c.WriteOK()
})
Expand Down
57 changes: 16 additions & 41 deletions cmd_object_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package miniredis

import (
"strconv"
"testing"
"time"

Expand All @@ -25,51 +24,27 @@ func TestObjectIdletime(t *testing.T) {
"SET", "foo", "bar",
)

{
mustDo(t, c,
"OBJECT", "IDLETIME", "foo",
proto.Int(0),
)
}
mustDo(t, c,
"OBJECT", "IDLETIME", "foo",
proto.Int(0),
)

s.SetTime(start.Add(time.Minute))

{
mustDo(t, c,
"OBJECT", "IDLETIME", "foo",
proto.Int(60),
)
}
mustDo(t, c,
"OBJECT", "IDLETIME", "foo",
proto.Int(60),
)

s.Get("foo")

{
mustDo(t, c,
"object", "idletime", "foo",
proto.Int(0),
)
}
mustDo(t, c,
"object", "idletime", "foo",
proto.Int(0),
)

s.Del("foo")

{
mustDo(t, c,
"OBJECT", "IDLETIME", "foo",
proto.Nil,
)
}
}
}

func objectTime(c *proto.Client, k string) (int, error) {
ret, err := c.Do("OBJECT", "IDLETIME", k)
if err != nil {
return 0, nil
}

if ret == "(nil)" {
return -1, nil
mustDo(t, c,
"OBJECT", "IDLETIME", "foo",
proto.Nil,
)
}

return strconv.Atoi(ret)
}
4 changes: 2 additions & 2 deletions cmd_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (m *Miniredis) cmdXadd(c *server.Peer, cmd string, args []string) {
if minID != "" {
s.trimBefore(minID)
}
db.touch(key, true)
db.incr(key)

c.WriteBulk(newID)
})
Expand Down Expand Up @@ -931,7 +931,7 @@ func (m *Miniredis) cmdXdel(c *server.Peer, cmd string, args []string) {
c.WriteError(err.Error())
return
}
db.touch(stream, true)
db.incr(stream)
c.WriteInt(n)
})
}
Expand Down
50 changes: 25 additions & 25 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,11 @@ var (
errInvalidEntryID = errors.New("stream ID is invalid")
)

func (db *RedisDB) touch(k string, write bool) {
db.lru[k] = db.master.effectiveNow()
if write {
db.keyVersion[k]++
}
}

// exists also updates the lru
func (db *RedisDB) exists(k string) bool {
_, ok := db.keys[k]
if ok {
db.touch(k, false)
db.lru[k] = db.master.effectiveNow()
}
return ok
}
Expand All @@ -33,6 +27,12 @@ func (db *RedisDB) t(k string) string {
return db.keys[k]
}

// incr increases the version and the lru timestamp
func (db *RedisDB) incr(k string) {
db.lru[k] = db.master.effectiveNow()
db.keyVersion[k]++
}

// allKeys returns all keys. Sorted.
func (db *RedisDB) allKeys() []string {
res := make([]string, 0, len(db.keys))
Expand Down Expand Up @@ -89,7 +89,7 @@ func (db *RedisDB) move(key string, to *RedisDB) bool {
if v, ok := db.ttl[key]; ok {
to.ttl[key] = v
}
to.touch(key, true)
to.incr(key)
db.del(key, true)
return true
}
Expand Down Expand Up @@ -118,7 +118,7 @@ func (db *RedisDB) rename(from, to string) {
if v, ok := db.ttl[from]; ok {
db.ttl[to] = v
}
db.touch(to, true)
db.incr(to)

db.del(from, true)
}
Expand Down Expand Up @@ -167,7 +167,7 @@ func (db *RedisDB) stringSet(k, v string) {
db.del(k, false)
db.keys[k] = "string"
db.stringKeys[k] = v
db.touch(k, true)
db.incr(k)
}

// change int key value
Expand Down Expand Up @@ -209,7 +209,7 @@ func (db *RedisDB) listLpush(k, v string) int {
}
l = append([]string{v}, l...)
db.listKeys[k] = l
db.touch(k, true)
db.incr(k)
return len(l)
}

Expand All @@ -223,7 +223,7 @@ func (db *RedisDB) listLpop(k string) string {
} else {
db.listKeys[k] = l
}
db.touch(k, true)
db.incr(k)
return el
}

Expand All @@ -234,7 +234,7 @@ func (db *RedisDB) listPush(k string, v ...string) int {
}
l = append(l, v...)
db.listKeys[k] = l
db.touch(k, true)
db.incr(k)
return len(l)
}

Expand All @@ -246,7 +246,7 @@ func (db *RedisDB) listPop(k string) string {
db.del(k, true)
} else {
db.listKeys[k] = l
db.touch(k, true)
db.incr(k)
}
return el
}
Expand All @@ -255,7 +255,7 @@ func (db *RedisDB) listPop(k string) string {
func (db *RedisDB) setSet(k string, set setKey) {
db.keys[k] = "set"
db.setKeys[k] = set
db.touch(k, true)
db.incr(k)
}

// setadd adds members to a set. Returns nr of new keys.
Expand All @@ -273,7 +273,7 @@ func (db *RedisDB) setAdd(k string, elems ...string) int {
s[e] = struct{}{}
}
db.setKeys[k] = s
db.touch(k, true)
db.incr(k)
return added
}

Expand All @@ -295,7 +295,7 @@ func (db *RedisDB) setRem(k string, fields ...string) int {
} else {
db.setKeys[k] = s
}
db.touch(k, true)
db.incr(k)
return removed
}

Expand Down Expand Up @@ -361,7 +361,7 @@ func (db *RedisDB) hashSet(k string, fv ...string) int {
f, v := fv[idx], fv[idx+1]
_, ok := db.hashKeys[k][f]
db.hashKeys[k][f] = v
db.touch(k, true)
db.incr(k)
if !ok {
new++
}
Expand Down Expand Up @@ -413,7 +413,7 @@ func (db *RedisDB) sortedSet(key string) map[string]float64 {
// ssetSet sets a complete sorted set.
func (db *RedisDB) ssetSet(key string, sset sortedSet) {
db.keys[key] = "zset"
db.touch(key, true)
db.incr(key)
db.sortedsetKeys[key] = sset
}

Expand All @@ -427,7 +427,7 @@ func (db *RedisDB) ssetAdd(key string, score float64, member string) bool {
_, ok = ss[member]
ss[member] = score
db.sortedsetKeys[key] = ss
db.touch(key, true)
db.incr(key)
return !ok
}

Expand Down Expand Up @@ -521,7 +521,7 @@ func (db *RedisDB) ssetIncrby(k, m string, delta float64) float64 {
v, _ := ss.get(m)
v += delta
ss.set(v, m)
db.touch(k, true)
db.incr(k)
return v
}

Expand Down Expand Up @@ -625,7 +625,7 @@ func (db *RedisDB) newStream(key string) (*streamKey, error) {
db.keys[key] = "stream"
s := newStreamKey()
db.streamKeys[key] = s
db.touch(key, true)
db.incr(key)
return s, nil
}

Expand Down Expand Up @@ -677,7 +677,7 @@ func (db *RedisDB) hllAdd(k string, elems ...string) int {
}
}
db.hllKeys[k] = s
db.touch(k, true)
db.incr(k)
return hllAltered
}

Expand Down Expand Up @@ -724,7 +724,7 @@ func (db *RedisDB) hllMerge(keys []string) error {

db.hllKeys[destKey] = destHll
db.keys[destKey] = "hll"
db.touch(destKey, true)
db.incr(destKey)

return nil
}
4 changes: 2 additions & 2 deletions direct.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ func (db *RedisDB) SetTTL(k string, ttl time.Duration) {
defer db.master.signal.Broadcast()

db.ttl[k] = ttl
db.touch(k, true)
db.incr(k)
}

// Type gives the type of a key, or ""
Expand Down Expand Up @@ -506,7 +506,7 @@ func (db *RedisDB) hdel(k, f string) {
return
}
delete(db.hashKeys[k], f)
db.touch(k, true)
db.incr(k)
}

// HIncrBy increases the integer value of a hash field by delta (int).
Expand Down
2 changes: 1 addition & 1 deletion miniredis.go
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ func (m *Miniredis) copy(
panic("missing case")
}
destDB.keys[dst] = srcDB.keys[src]
destDB.touch(dst, true)
destDB.incr(dst)
if v, ok := srcDB.ttl[src]; ok {
destDB.ttl[dst] = v
}
Expand Down

0 comments on commit dda3a52

Please sign in to comment.